test_02.bmx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. SuperStrict
  2. Framework Database.MariaDB
  3. Import BRL.filesystem
  4. Import BRL.StandardIO
  5. Local db:TDBConnection = LoadDatabase("MARIADB", "maxtest", Null, 0, "brucey", "brucey")
  6. If Not db Then
  7. Print("Didn't work...")
  8. End
  9. End If
  10. If db.hasError() Then
  11. errorAndClose(db)
  12. End If
  13. Local names:String[][] = [ ..
  14. [ "Alfred", "Aho" ], ..
  15. [ "Brian", "Kernighan" ], ..
  16. [ "Peter", "Weinberger" ] ]
  17. If db.isOpen() Then
  18. db.executeQuery("DROP TABLE if exists person")
  19. ' Create a new table
  20. Local s:String = "CREATE TABLE if not exists person (id integer primary key AUTO_INCREMENT, " + ..
  21. " forename varchar(30)," + ..
  22. " surname varchar(30), stamp datetime, num integer )"
  23. db.executeQuery(s)
  24. If db.hasError() Then
  25. errorAndClose(db)
  26. End If
  27. ' get a new query object
  28. Local query:TDatabaseQuery = TDatabaseQuery.Create(db)
  29. ' prepare the insert statement
  30. ' by preparing it once, the database can reuse it on succesive inserts which is more efficient.
  31. query.prepare("INSERT INTO person values (NULL, ?, ?, ?, ?)")
  32. If db.hasError() Then
  33. errorAndClose(db)
  34. End If
  35. ' iterate round the array inserting new entries
  36. For Local i:Int = 0 Until 3
  37. query.bindValue(0, TDBString.Set(names[i][0]))
  38. query.bindValue(1, TDBString.Set(names[i][1]))
  39. query.bindValue(2, TDBDateTime.Set(2007, 4 + i, 30, 10, 4, 16))
  40. query.bindValue(3, TDBInt.Set(i + 10))
  41. query.execute()
  42. If db.hasError() Then
  43. errorAndClose(db)
  44. End If
  45. Next
  46. ' select
  47. query = db.executeQuery("SELECT * from person")
  48. If db.hasError() Then
  49. errorAndClose(db)
  50. End If
  51. While query.nextRow()
  52. Local record:TQueryRecord = query.rowRecord()
  53. Print("Name = " + record.getString(1) + " " + record.getString(2))
  54. Print TDBDateTime(record.value(3)).format()
  55. Print record.getInt(4)
  56. Wend
  57. db.close()
  58. End If
  59. Function errorAndClose(db:TDBConnection)
  60. Print(db.error().toString())
  61. db.close()
  62. End
  63. End Function