test_02.bmx 1.7 KB

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