test_04.bmx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. SuperStrict
  2. Framework Database.SQLite
  3. Import BRL.FileSystem
  4. Import BRL.RandomDefault
  5. Import brl.standardio
  6. Type TPersonStuff
  7. Field forename:String
  8. Field surname:String
  9. Field dataInt:Int
  10. Field dataFloat:Float
  11. Field dataDouble:Double
  12. Field dataLong:Long
  13. End Type
  14. ' delete the db file if it already exists
  15. DeleteFile("maxtest.db")
  16. ' create and open a new SQLite database
  17. Local db:TDBConnection = LoadDatabase("SQLITE", "maxtest.db")
  18. If Not db Then
  19. Print("Didn't work...")
  20. End
  21. End If
  22. If db.hasError() Then
  23. errorAndClose(db)
  24. End If
  25. Local names:String[][] = [ ..
  26. [ "Alfred", "Aho" ], ..
  27. [ "Brian", "Kernighan" ], ..
  28. [ "Peter", "Weinberger" ] ]
  29. Local pstuff:TPersonStuff[] = New TPersonStuff[names.length]
  30. For Local i:Int = 0 Until names.length
  31. pstuff[i] = New TPersonStuff
  32. pstuff[i].forename = names[i][0]
  33. pstuff[i].surname = names[i][1]
  34. pstuff[i].dataInt = Rnd(1, 10)
  35. pstuff[i].dataFloat = Rnd(1, 10)
  36. pstuff[i].dataDouble = Rnd(1, 10)
  37. pstuff[i].dataLong = Rnd(1, 100000000000:Long)
  38. Next
  39. If db.isOpen() Then
  40. db.executeQuery("DROP TABLE if exists person")
  41. 'DebugStop
  42. ' Create a new table
  43. Local s:String = "CREATE TABLE if not exists person (id integer primary key AUTOINCREMENT, " + ..
  44. " forename varchar(30), surname varchar(30), dataint integer, datafloat float, datadouble double, datalong bigint )"
  45. db.executeQuery(s)
  46. If db.hasError() Then
  47. errorAndClose(db)
  48. End If
  49. ' get a new query object
  50. Local query:TDatabaseQuery = TDatabaseQuery.Create(db)
  51. ' prepare the insert statement
  52. ' by preparing it once, the database can reuse it on succesive inserts which is more efficient.
  53. query.prepare("INSERT INTO person values (NULL, ?, ?, ?, ?, ?, ?)")
  54. If db.hasError() Then
  55. errorAndClose(db)
  56. End If
  57. ' iterate around the array inserting new entries
  58. For Local i:Int = 0 Until names.length
  59. query.bindValue(0, TDBString.Set(pstuff[i].forename))
  60. query.bindValue(1, TDBString.Set(pstuff[i].surname))
  61. query.bindValue(2, TDBInt.Set(pstuff[i].dataInt))
  62. query.bindValue(3, TDBFloat.Set(pstuff[i].dataFloat))
  63. query.bindValue(4, TDBDouble.Set(pstuff[i].datadouble))
  64. query.bindValue(5, TDBLong.Set(pstuff[i].dataLong))
  65. query.execute()
  66. If db.hasError() Then
  67. errorAndClose(db)
  68. End If
  69. Print "id = " + query.lastInsertedId()
  70. Next
  71. Local qr:TDatabaseQuery = db.executeQuery("SELECT count(forename) FROM person ")
  72. While qr.nextRow()
  73. Local record:TQueryRecord = qr.rowRecord()
  74. Print ("count = " + record.value(0).getInt())
  75. Wend
  76. ' NOTE : There is a bug in SQLite. The following should return TWO rows.
  77. ' If you replace the prepare/execute statement with the commented out one, it works.
  78. ' prepare select
  79. query.prepare("SELECT * FROM person WHERE surname LIKE ?")
  80. If db.hasError() Then
  81. errorAndClose(db)
  82. End If
  83. query.bindValue(0, TDBString.Set("%n%"))
  84. query.execute()
  85. 'query = db.execute("SELECT * FROM person WHERE surname LIKE '%n%'")
  86. If db.hasError() Then
  87. errorAndClose(db)
  88. End If
  89. While query.nextRow()
  90. Local record:TQueryRecord = query.rowRecord()
  91. ' auto_increment starts at 1...
  92. Local i:Int = record.value(0).getInt() - 1
  93. ' compare what went in, to what went out.
  94. Print(" IN - " + pstuff[i].forename + " : " + pstuff[i].surname + " : " + pstuff[i].dataInt + ..
  95. " : " + pstuff[i].dataFloat + " : " + pstuff[i].dataDouble + " : " + pstuff[i].dataLong)
  96. Print(" OUT - " + record.value(1).getString() + " : " + record.value(2).getString() + ..
  97. " : " + record.value(3).getInt() + " : " + record.value(4).getFloat() + ..
  98. " : " + record.value(5).getDouble() + " : " + record.value(6).getLong() )
  99. Wend
  100. db.close()
  101. If db.hasError() Then
  102. errorAndClose(db)
  103. End If
  104. End If
  105. Function errorAndClose(db:TDBConnection)
  106. Print(db.error().toString())
  107. db.close()
  108. End
  109. End Function