test_03.bmx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. SuperStrict
  2. Framework Database.MariaDB
  3. Import BRL.filesystem
  4. Import BRL.StandardIO
  5. Import BRL.RandomDefault
  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. Local db:TDBConnection = LoadDatabase("MARIADB", "maxtest", Null, 0, "brucey", "brucey")
  15. If Not db Then
  16. Print("Didn't work...")
  17. End
  18. End If
  19. If db.hasError() Then
  20. errorAndClose(db)
  21. End If
  22. If db.isOpen() Then
  23. ' Load up some data for insertion...
  24. Local names:String[][] = [ ..
  25. [ "Alfred", "Aho" ], ..
  26. [ "Brian", "Kernighan" ], ..
  27. [ "Peter", "Weinberger" ] ]
  28. Local pstuff:TPersonStuff[] = New TPersonStuff[names.length]
  29. For Local i:Int = 0 Until names.length
  30. pstuff[i] = New TPersonStuff
  31. pstuff[i].forename = names[i][0]
  32. pstuff[i].surname = names[i][1]
  33. pstuff[i].dataInt = Rnd(1, 10)
  34. pstuff[i].dataFloat = Rnd(1, 10)
  35. pstuff[i].dataDouble = Rnd(1, 10)
  36. pstuff[i].dataLong = Rnd(1, 100000000000:Long)
  37. Next
  38. db.executeQuery("DROP TABLE if exists person")
  39. 'DebugStop
  40. ' Create a new table
  41. Local s:String = "CREATE TABLE if not exists person (id integer primary key AUTO_INCREMENT, " + ..
  42. " forename varchar(30), surname varchar(30), dataint integer, datafloat float, datadouble double, datalong bigint )"
  43. db.executeQuery(s)
  44. If db.hasError() Then
  45. errorAndClose(db)
  46. End If
  47. ' get a new query object
  48. Local query:TDatabaseQuery = TDatabaseQuery.Create(db)
  49. ' prepare the insert statement
  50. ' by preparing it once, the database can reuse it on succesive inserts which is more efficient.
  51. query.prepare("INSERT INTO person values (NULL, ?, ?, ?, ?, ?, ?)")
  52. If db.hasError() Then
  53. errorAndClose(db)
  54. End If
  55. ' iterate round the array inserting new entries
  56. For Local i:Int = 0 Until names.length
  57. query.bindValue(0, TDBString.Set(pstuff[i].forename))
  58. query.bindValue(1, TDBString.Set(pstuff[i].surname))
  59. query.bindValue(2, TDBInt.Set(pstuff[i].dataInt))
  60. query.bindValue(3, TDBFloat.Set(pstuff[i].dataFloat))
  61. query.bindValue(4, TDBDouble.Set(pstuff[i].datadouble))
  62. query.bindValue(5, TDBLong.Set(pstuff[i].dataLong))
  63. 'DebugStop
  64. query.execute()
  65. If db.hasError() Then
  66. errorAndClose(db)
  67. End If
  68. Next
  69. ' select
  70. query = db.executeQuery("SELECT * FROM person")
  71. If db.hasError() Then
  72. errorAndClose(db)
  73. End If
  74. ' While query.nextRow()
  75. ' Local record:TQueryRecord = query.rowRecord()
  76. For Local record:TQueryRecord = EachIn query
  77. Local i:Int = record.value(0).getInt() - 1
  78. Print(" IN - " + pstuff[i].forename + " : " + pstuff[i].surname + " : " + pstuff[i].dataInt + ..
  79. " : " + pstuff[i].dataFloat + " : " + pstuff[i].dataDouble + " : " + pstuff[i].dataLong)
  80. Print(" OUT - " + record.getString(1) + " : " + record.getString(2) + ..
  81. " : " + record.getInt(3) + " : " + record.getFloat(4) + ..
  82. " : " + record.getDouble(5) + " : " + record.getLong(6) )
  83. ' Wend
  84. Next
  85. db.close()
  86. End If
  87. Function errorAndClose(db:TDBConnection)
  88. Print(db.error().toString())
  89. db.close()
  90. End
  91. End Function