2
0

example_01.bmx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ' Basic querying
  2. SuperStrict
  3. Framework BaH.DBSQLite
  4. Import BRL.filesystem
  5. Import BRL.StandardIO
  6. ' delete the db file if it already exists (just to tidy up the examples!)
  7. DeleteFile("maxtest.db")
  8. ' load the database driver, creating and opening the database
  9. Local db:TDBConnection = LoadDatabase("SQLITE", "maxtest.db")
  10. If Not db Then
  11. Print "No valid driver!"
  12. End
  13. End If
  14. ' check for errors
  15. If db.hasError() Then
  16. errorAndClose(db)
  17. End If
  18. ' if the connection is open, do something with it...
  19. If db.isOpen() Then
  20. Local names:String[][] = [ ..
  21. [ "Alfred", "Aho" ], ..
  22. [ "Brian", "Kernighan" ], ..
  23. [ "Peter", "Weinberger" ] ]
  24. ' Create a new table
  25. Local s:String = "CREATE TABLE person (id integer primary key AUTOINCREMENT, " + ..
  26. " forename varchar(30)," + ..
  27. " surname varchar(30) )"
  28. ' execute the SQL
  29. db.executeQuery(s)
  30. If db.hasError() Then
  31. errorAndClose(db)
  32. End If
  33. ' insert some data into the database
  34. For Local i:Int = 0 Until names.length
  35. ' hard-coding the SQLs with data...
  36. db.executeQuery("INSERT INTO person values (NULL, '" + names[i][0] + "', '" + names[i][1] + "')")
  37. If db.hasError() Then
  38. errorAndClose(db)
  39. End If
  40. Next
  41. ' retrieve data from the database
  42. Local query:TDatabaseQuery = db.executeQuery("SELECT * from person")
  43. If db.hasError() Then
  44. errorAndClose(db)
  45. End If
  46. ' iterate over the retrieved rows
  47. While query.nextRow()
  48. Local record:TQueryRecord = query.rowRecord()
  49. Print "Name = " + record.value(1).getString() + " " + record.value(2).getString()
  50. Wend
  51. ' close the connection!
  52. db.close()
  53. End If
  54. Function errorAndClose(db:TDBConnection)
  55. Print db.error().toString()
  56. db.close()
  57. End
  58. End Function