2
0

blob_test.bmx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. SuperStrict
  2. Framework Database.MariaDB
  3. Import BRL.filesystem
  4. Import BRL.StandardIO
  5. Import BRL.RamStream
  6. Import BRL.JPGLoader
  7. Local db:TDBConnection = LoadDatabase("MARIADB", "maxtest", Null, 0, "brucey", "brucey")
  8. If Not db Then
  9. Print "Didn't work..."
  10. End
  11. End If
  12. If db.hasError() Then
  13. errorAndClose(db)
  14. End If
  15. Local names:String[][] = [ ..
  16. [ "Alfred", "Aho", "alfred-aho.jpg" ], ..
  17. [ "Brian", "Kernighan", "brian-kernighan.jpg" ], ..
  18. [ "Peter", "Weinberger", "pjw.jpg" ] ]
  19. If db.isOpen() Then
  20. db.executeQuery("DROP TABLE if exists person")
  21. Local s:String = "CREATE TABLE if not exists person (id integer primary key AUTO_INCREMENT, " + ..
  22. " forename varchar(30)," + ..
  23. " surname varchar(30), stamp datetime, image blob )"
  24. db.executeQuery(s)
  25. If db.hasError() Then
  26. errorAndClose(db)
  27. End If
  28. ' get a new query object
  29. Local query:TDatabaseQuery = TDatabaseQuery.Create(db)
  30. ' prepare the insert statement
  31. ' by preparing it once, the database can reuse it on succesive inserts which is more efficient.
  32. query.prepare("INSERT INTO person values (NULL, ?, ?, ?, ?)")
  33. If db.hasError() Then
  34. errorAndClose(db)
  35. End If
  36. ' iterate round the array inserting new entries
  37. For Local i:Int = 0 Until 3
  38. query.bindValue(0, TDBString.Set(names[i][0]))
  39. query.bindValue(1, TDBString.Set(names[i][1]))
  40. query.bindValue(2, TDBDateTime.Set(2007, 4 + i, 30, 10, 4, 16))
  41. ' load some data (raw jpg data)
  42. Local b:Byte[] = LoadByteArray("images/" + names[i][2])
  43. ' bind it (copying the data)
  44. query.bindValue(3, TDBBlob.Set(b, b.length))
  45. query.execute()
  46. If db.hasError() Then
  47. errorAndClose(db)
  48. End If
  49. Next
  50. query = db.executeQuery("SELECT * from person")
  51. If db.hasError() Then
  52. errorAndClose(db)
  53. End If
  54. While query.nextRow()
  55. Local record:TQueryRecord = query.rowRecord()
  56. Print (record.GetInt(0) + ". Name = " + record.GetString(1) + " " + record.GetString(2))
  57. Print TDBDateTime(record.value(3)).format()
  58. ' get the blob data
  59. Local b:TDBBlob = TDBBlob(record.value(4))
  60. ' create a pixmap (from the raw jpg data)
  61. Local pix:TPixmap = LoadPixmap(CreateRamStream(b.getBlob(), b.size(), True, False))
  62. If pix Then
  63. Print "Dimension = " + pix.width + ", " + pix.height
  64. End If
  65. Wend
  66. db.close()
  67. End If
  68. Function errorAndClose(db:TDBConnection)
  69. Print db.error().toString()
  70. db.close()
  71. End
  72. End Function