2
0

test_03.bmx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. SuperStrict
  2. Framework Database.PostgreSQL
  3. Import BRL.Random
  4. Import BRL.StandardIO
  5. Type TPersonStuff
  6. Field forename:String
  7. Field surname:String
  8. Field dataInt:Int
  9. Field dataFloat:Float
  10. Field dataDouble:Double
  11. Field dataLong:Long
  12. End Type
  13. Local db:TDBConnection = LoadDatabase("POSTGRESQL", "maxtest", "localhost", 0, "user", "pass")
  14. If Not db Then
  15. Print "Didn't work..."
  16. End
  17. End If
  18. If db.hasError() Then
  19. errorAndClose(db)
  20. End If
  21. If db.isOpen() Then
  22. ' Load up some data for insertion...
  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, 100000000000:Long)
  36. Next
  37. db.executeQuery("DROP TABLE person")
  38. db.executeQuery("DROP SEQUENCE person_id")
  39. ' create the auto-incrementing field
  40. db.executeQuery("CREATE SEQUENCE person_id INCREMENT 1 START 1")
  41. ' Create a new table
  42. Local s:String = "CREATE TABLE person (id integer primary key DEFAULT NEXTVAL('person_id'), " + ..
  43. " forename varchar(30), surname varchar(30), dataint integer, datafloat real, datadouble double precision, datalong bigint )"
  44. db.executeQuery(s)
  45. If db.hasError() Then
  46. errorAndClose(db)
  47. End If
  48. ' get a new query object
  49. Local query:TDatabaseQuery = TDatabaseQuery.Create(db)
  50. ' prepare the insert statement
  51. ' by preparing it once, the database can reuse it on succesive inserts which is more efficient.
  52. query.prepare("INSERT INTO person (forename, surname, dataint, datafloat, datadouble, datalong)" + ..
  53. " values ($1, $2, $3, $4, $5, $6)")
  54. If db.hasError() Then
  55. errorAndClose(db)
  56. End If
  57. ' iterate round the array inserting new entries
  58. For Local i:Int = 0 Until names.length
  59. query.bindValue(0, TDBString.Set(pstuff[i].forename))
  60. query.bindValue(1, TDBString.Set(pstuff[i].surname))
  61. query.bindValue(2, TDBInt.Set(pstuff[i].dataInt))
  62. query.bindValue(3, TDBFloat.Set(pstuff[i].dataFloat))
  63. query.bindValue(4, TDBDouble.Set(pstuff[i].datadouble))
  64. query.bindValue(5, TDBLong.Set(pstuff[i].dataLong))
  65. query.execute()
  66. If db.hasError() Then
  67. errorAndClose(db)
  68. End If
  69. Next
  70. ' select
  71. query = db.executeQuery("SELECT * FROM person")
  72. If db.hasError() Then
  73. errorAndClose(db)
  74. End If
  75. For Local record:TQueryRecord = EachIn query
  76. Local i:Int = record.value(0).getInt() - 1
  77. Print " IN - " + pstuff[i].forename + " : " + pstuff[i].surname + " : " + pstuff[i].dataInt + ..
  78. " : " + pstuff[i].dataFloat + " : " + pstuff[i].dataDouble + " : " + pstuff[i].dataLong
  79. Print " OUT - " + record.getString(1) + " : " + record.getString(2) + ..
  80. " : " + record.getInt(3) + " : " + record.getFloat(4) + ..
  81. " : " + record.getDouble(5) + " : " + record.getLong(6)
  82. Next
  83. db.close()
  84. End If
  85. Function errorAndClose(db:TDBConnection)
  86. Print db.error().toString()
  87. db.close()
  88. End
  89. End Function