test_04.bmx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. Local names:String[][] = [ ..
  22. [ "Alfred", "Aho" ], ..
  23. [ "Brian", "Kernighan" ], ..
  24. [ "Peter", "Weinberger" ] ]
  25. Local pstuff:TPersonStuff[] = New TPersonStuff[names.length]
  26. For Local i:Int = 0 Until names.length
  27. pstuff[i] = New TPersonStuff
  28. pstuff[i].forename = names[i][0]
  29. pstuff[i].surname = names[i][1]
  30. pstuff[i].dataInt = Rnd(1, 10)
  31. pstuff[i].dataFloat = Rnd(1, 10)
  32. pstuff[i].dataDouble = Rnd(1, 10)
  33. pstuff[i].dataLong = Rnd(1, 100000000000:Long)
  34. Next
  35. If db.isOpen() Then
  36. db.executeQuery("DROP TABLE person")
  37. db.executeQuery("DROP SEQUENCE person_id")
  38. ' create the auto-incrementing field
  39. db.executeQuery("CREATE SEQUENCE person_id INCREMENT 1 START 1")
  40. ' Create a new table
  41. Local s:String = "CREATE TABLE person (id integer primary key DEFAULT NEXTVAL('person_id'), " + ..
  42. " forename varchar(30), surname varchar(30), dataint integer, datafloat float, datadouble float8, datalong bigint )"
  43. db.executeQuery(s)
  44. If db.hasError() Then
  45. errorAndClose(db)
  46. End If
  47. ' get a new query object
  48. Local query:TDatabaseQuery = TDatabaseQuery.Create(db)
  49. ' prepare the insert statement
  50. ' by preparing it once, the database can reuse it on succesive inserts which is more efficient.
  51. query.prepare("INSERT INTO person (forename, surname, dataint, datafloat, datadouble, datalong)" + ..
  52. " values ($1, $2, $3, $4, $5, $6)")
  53. If db.hasError() Then
  54. errorAndClose(db)
  55. End If
  56. ' iterate around the array inserting new entries
  57. For Local i:Int = 0 Until names.length
  58. query.bindValue(0, TDBString.Set(pstuff[i].forename))
  59. query.bindValue(1, TDBString.Set(pstuff[i].surname))
  60. query.bindValue(2, TDBInt.Set(pstuff[i].dataInt))
  61. query.bindValue(3, TDBFloat.Set(pstuff[i].dataFloat))
  62. query.bindValue(4, TDBDouble.Set(pstuff[i].datadouble))
  63. query.bindValue(5, TDBLong.Set(pstuff[i].dataLong))
  64. query.execute()
  65. If db.hasError() Then
  66. errorAndClose(db)
  67. End If
  68. Next
  69. query = TDatabaseQuery.Create(db)
  70. ' prepare select
  71. query.prepare("SELECT * FROM person WHERE surname LIKE $1")
  72. If db.hasError() Then
  73. errorAndClose(db)
  74. End If
  75. query.bindValue(0, TDBString.Set("%n%"))
  76. query.execute()
  77. If db.hasError() Then
  78. errorAndClose(db)
  79. End If
  80. While query.nextRow()
  81. Local record:TQueryRecord = query.rowRecord()
  82. ' auto_increment starts at 1...
  83. Local i:Int = record.value(0).getInt() - 1
  84. ' compare what went in, to what went out.
  85. Print " IN - " + pstuff[i].forename + " : " + pstuff[i].surname + " : " + pstuff[i].dataInt + ..
  86. " : " + pstuff[i].dataFloat + " : " + pstuff[i].dataDouble + " : " + pstuff[i].dataLong
  87. Print " OUT - " + record.getString(1) + " : " + record.getString(2) + ..
  88. " : " + record.getInt(3) + " : " + record.getFloat(4) + ..
  89. " : " + record.getDouble(5) + " : " + record.getLong(6)
  90. Wend
  91. db.close()
  92. End If
  93. Function errorAndClose(db:TDBConnection)
  94. Print db.error().toString()
  95. db.close()
  96. End
  97. End Function