2
0

blob_test.bmx 2.5 KB

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