test_04.bmx 3.2 KB

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