test_03.bmx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. If db.isOpen() Then
  26. ' Load up some data for insertion...
  27. Local names:String[][] = [ ..
  28. [ "Alfred", "Aho" ], ..
  29. [ "Brian", "Kernighan" ], ..
  30. [ "Peter", "Weinberger" ] ]
  31. Local pstuff:TPersonStuff[] = New TPersonStuff[names.length]
  32. For Local i:Int = 0 Until names.length
  33. pstuff[i] = New TPersonStuff
  34. pstuff[i].forename = names[i][0]
  35. pstuff[i].surname = names[i][1]
  36. pstuff[i].dataInt = Rnd(1, 10)
  37. pstuff[i].dataFloat = Rnd(1, 10)
  38. pstuff[i].dataDouble = Rnd(1, 10)
  39. pstuff[i].dataLong = Rnd(1, 100000000000:Long)
  40. Next
  41. db.executeQuery("DROP TABLE if exists person")
  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 round 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. Next
  70. ' select
  71. query = db.executeQuery("SELECT * FROM person")' WHERE surname LIKE 'Z'")
  72. If db.hasError() Then
  73. errorAndClose(db)
  74. End If
  75. For Local record:TQueryRecord = EachIn query
  76. ' While query.nextRow()
  77. ' Local record:TQueryRecord = query.rowRecord()
  78. Local i:Int = record.value(0).getInt() - 1
  79. Print(" IN - " + pstuff[i].forename + " : " + pstuff[i].surname + " : " + pstuff[i].dataInt + ..
  80. " : " + pstuff[i].dataFloat + " : " + pstuff[i].dataDouble + " : " + pstuff[i].dataLong)
  81. Print(" OUT - " + record.value(1).getString() + " : " + record.value(2).getString() + ..
  82. " : " + record.value(3).getInt() + " : " + record.value(4).getFloat() + ..
  83. " : " + record.value(5).getDouble() + " : " + record.value(6).getLong() )
  84. ' Wend
  85. Next
  86. db.close()
  87. End If
  88. Function errorAndClose(db:TDBConnection)
  89. Print(db.error().toString())
  90. db.close()
  91. End
  92. End Function