date_test_02.bmx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. SuperStrict
  2. Framework Database.SQLite
  3. Import BRL.filesystem
  4. Import brl.standardio
  5. DeleteFile("maxtest.db")
  6. Local db:TDBConnection = LoadDatabase("SQLITE", "maxtest.db")
  7. If db.hasError() Then
  8. errorAndClose(db)
  9. End If
  10. Local names:String[][] = [ ..
  11. [ "Alfred", "Aho" ], ..
  12. [ "Brian", "Kernighan" ], ..
  13. [ "Peter", "Weinberger" ] ]
  14. If db.isOpen() Then
  15. Local s:String = "CREATE TABLE person (id integer primary key AUTOINCREMENT, " + ..
  16. " forename varchar(30)," + ..
  17. " surname varchar(30), bday date, thetime time, exact datetime )"
  18. db.executeQuery(s)
  19. If db.hasError() Then
  20. errorAndClose(db)
  21. End If
  22. For Local i:Int = 0 Until names.length
  23. db.executeQuery("INSERT INTO person values (NULL, '" + names[i][0] + "', '" + ..
  24. names[i][1] + "', '200" + i + "-01-01', '04:01:45', '2007-03-04 13:20:13')")
  25. If db.hasError() Then
  26. errorAndClose(db)
  27. End If
  28. Next
  29. Local query:TDatabaseQuery = TDatabaseQuery.Create(db)
  30. ' prepare select
  31. query.prepare("SELECT * FROM person WHERE bday = ? And thetime > ? And exact < ?")
  32. If db.hasError() Then
  33. errorAndClose(db)
  34. End If
  35. query.bindValue(0, TDBDate.Set(2001, 1, 1))
  36. query.bindValue(1, TDBTime.Set(4, 0 ,0))
  37. query.bindValue(2, TDBDateTime.Set(2008, 1, 1, 10, 0, 0))
  38. query.execute()
  39. If db.hasError() Then
  40. errorAndClose(db)
  41. End If
  42. While query.nextRow()
  43. Local record:TQueryRecord = query.rowRecord()
  44. Print("Name = " + record.getString(1) + " " + record.getString(2) + " - " + ..
  45. TDBDate.SetFromString(record.getString(3)).getString() + " - " + ..
  46. TDBTime.SetFromString(record.getString(4)).getString() + " - " + ..
  47. TDBDateTime.SetFromString(record.getString(5)).getString())
  48. Wend
  49. db.close()
  50. End If
  51. Function errorAndClose(db:TDBConnection)
  52. Print(db.error().toString())
  53. db.close()
  54. End
  55. End Function