date_test_02.bmx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. SuperStrict
  2. Framework Database.ODBC
  3. Import BRL.StandardIO
  4. ' database dsn name
  5. Local db:TDBConnection = LoadDatabase("ODBC", "maxtest", Null, Null, "brucey", "brucey", "maxtest")
  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. ' we don't care if the drop table fails, since it might not exist yet...
  19. db.executeQuery("DROP TABLE person")
  20. Local s:String = "CREATE TABLE person (id integer primary key AUTO_INCREMENT, " + ..
  21. " forename varchar(30)," + ..
  22. " surname varchar(30), bday date, thetime time, exact datetime )"
  23. db.executeQuery(s)
  24. If db.hasError() Then
  25. errorAndClose(db)
  26. End If
  27. For Local i:Int = 0 Until names.length
  28. db.executeQuery("INSERT INTO person values (NULL, '" + names[i][0] + "', '" + ..
  29. names[i][1] + "', '200" + i + "-01-01', '04:01:45', '2007-03-04 13:20:13')")
  30. If db.hasError() Then
  31. errorAndClose(db)
  32. End If
  33. Next
  34. Local query:TDatabaseQuery = TDatabaseQuery.Create(db)
  35. ' prepare select
  36. query.prepare("SELECT * FROM person WHERE bday = ? and thetime > ? and exact < ?")
  37. If db.hasError() Then
  38. errorAndClose(db)
  39. End If
  40. query.bindValue(0, TDBDate.Set(2001, 1, 1))
  41. query.bindValue(1, TDBTime.Set(4, 0 ,0))
  42. query.bindValue(2, TDBDateTime.Set(2008, 1, 1, 10, 0, 0))
  43. query.execute()
  44. If db.hasError() Then
  45. errorAndClose(db)
  46. End If
  47. While query.nextRow()
  48. Local record:TQueryRecord = query.rowRecord()
  49. Print("Name = " + record.getString(1) + " " + record.getString(2) + " - " + ..
  50. record.getString(3) + " - " + record.getString(4) + " - " + record.getString(5))
  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