access_test.bmx 3.2 KB

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