如何将图片存入sqlite
如何将图片存入sqlite?
sqlite中有一种数据类型叫做Blob,可以满足我们的要求。
数据库中照片字段Photo BLOB
写入:
var
SqliteDB: TSQLiteDatabase
FS: TFileStream
begin
SqliteDB:=TSQLiteDatabase.Create(database.db)
FS := TFileStream.Create(test.jpeg,fmOpenRead)
SqliteDB.UpdateBlob(UPDATE PhotoLib set Photo = ? WHERE ID = 1,FS)
FS.free
SqliteDB.Free
end
读取至TImage控件显示:
SqliteDB:=TSQLiteDatabase.Create(database.db)
SqliteTB:=SqliteDB.GetTable(Select * From PhotoLib Where Id=1)
MS := SqliteTB.FieldAsBlob(SqliteTB.FieldIndex[photo])
if (MS = nil) then
begin
showmessage(无照片记录.)
exit
end
MS.Position := 0
PIC := TJPEGImage.Create
PIC.LoadFromStream(ms)
self.Image2.Picture.Graphic := PIC
MS.free
PIC.Free