access_date_test.bmx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. SuperStrict
  2. Framework Database.ODBC
  3. Import BRL.RandomDefault
  4. Import BRL.FileSystem
  5. Import BRL.StandardIO
  6. Type TPersonStuff
  7. Field forename:String
  8. Field surname:String
  9. Field dataInt:Int
  10. Field dataFloat:Float
  11. Field dataDouble:Double
  12. Field dataLong:Long
  13. Field dataDate:String
  14. End Type
  15. ' database dsn name - seems to need full path!
  16. Local db:TDBConnection = LoadDatabase("ODBC", "maxtest", Null, Null, "brucey", "brucey", CurrentDir() + "/maxtest.dsn")
  17. If Not db Then
  18. Print("Didn't work...")
  19. End
  20. End If
  21. If db.hasError() Then
  22. errorAndClose(db)
  23. End If
  24. Local names:String[][] = [ ..
  25. [ "Alfred", "Aho" ], ..
  26. [ "Brian", "Kernighan" ], ..
  27. [ "Peter", "Weinberger" ] ]
  28. Local pstuff:TPersonStuff[] = New TPersonStuff[names.length]
  29. For Local i:Int = 0 Until names.length
  30. pstuff[i] = New TPersonStuff
  31. pstuff[i].forename = names[i][0]
  32. pstuff[i].surname = names[i][1]
  33. pstuff[i].dataInt = Rnd(1, 10)
  34. pstuff[i].dataFloat = Rnd(1, 10)
  35. pstuff[i].dataDouble = Rnd(1, 10)
  36. pstuff[i].dataLong = Rnd(1, 1000000000:Long) ' this isn't really a long long... access can't do more than 4 bytes!
  37. pstuff[i].dataDate = "200" + i + "-04-03 10:03:04"
  38. Next
  39. If db.isOpen() Then
  40. Local t:String[]= db.getTables()
  41. Print t.Length
  42. For Local i:Int = 0 To t.Length - 1
  43. Print(t[i])
  44. Next
  45. ' we don't care if the drop table fails, since it might not exist yet...
  46. db.executeQuery("DROP TABLE person")
  47. ' Create a new table
  48. Local s:String = "CREATE TABLE person (id counter constraint pk primary key, " + ..
  49. " forename text(30), surname text(30), dataint integer, datafloat float, datadouble double, datalong long, datadate date )"
  50. db.executeQuery(s)
  51. If db.hasError() Then
  52. errorAndClose(db)
  53. End If
  54. ' we need to wait for access to catch up... (or table 'person' does not exist)
  55. Delay 100
  56. For Local i:Int = 0 Until names.length
  57. Local sql:String = "INSERT INTO person (forename, surname, dataint, datafloat, datadouble, datalong, datadate) " + ..
  58. " values ('" + pstuff[i].forename + "', '" + ..
  59. pstuff[i].surname + "', " + pstuff[i].dataInt + ", " + ..
  60. pstuff[i].dataFloat + ", " + pstuff[i].datadouble + ", " + ..
  61. pstuff[i].dataLong + ", '" + pstuff[i].dataDate + "')"
  62. db.executeQuery(sql)
  63. If db.hasError() Then
  64. errorAndClose(db)
  65. End If
  66. Next
  67. If db.hasError() Then
  68. errorAndClose(db)
  69. End If
  70. ' prepare select
  71. Local query:TDatabaseQuery = db.executeQuery("SELECT * FROM person WHERE surname LIKE '%n%'")
  72. If db.hasError() Then
  73. errorAndClose(db)
  74. End If
  75. While query.nextRow()
  76. Local record:TQueryRecord = query.rowRecord()
  77. ' auto_increment starts at 1...
  78. Local i:Int = record.value(0).getInt() - 1
  79. ' compare what went in, to what went out.
  80. Print(" IN - " + pstuff[i].forename + " : " + pstuff[i].surname + " : " + pstuff[i].dataInt + ..
  81. " : " + pstuff[i].dataFloat + " : " + pstuff[i].dataDouble + " : " + pstuff[i].dataLong + " : " + pstuff[i].dataDate)
  82. Print(" OUT - " + record.value(1).getString() + " : " + record.value(2).getString() + ..
  83. " : " + record.value(3).getInt() + " : " + record.value(4).getFloat() + ..
  84. " : " + record.value(5).getDouble() + " : " + record.value(6).getLong() + " : " + record.value(7).getString())
  85. Wend
  86. db.close()
  87. End If
  88. Function errorAndClose(db:TDBConnection)
  89. Print(db.error().toString())
  90. db.close()
  91. End
  92. End Function