Hello,

I am having trouble figuring out how to successfully write an image to the 
SQLite database as a Blob, using C++.

I have an HBITMAP that I would like to be able to store to and retrieve from 
the DB. If I understand what I have read correctly, I am supposed to write out 
the actual byte data to the DB. Here is what I have done so far.

I pass in pbyBitmap as the bmBytes parameter to this function:
ImportPageImage(CPage* pPage, BYTE* bmBytes, DWORD bmSize), which calls the 
code below. . .

//prepare query
static const WCHAR tblInsertBlob[] = _T("Insert into [tblBlob] ([Data]) values 
(?)");
SQL_HANDLE blobHandle = m_pSqliteDB->PrepareQuery16(tblInsertBlob); //calls 
sqlite3_prepare16()

//bind blob
m_pSqliteDB->BindBlob(blobHandle, 1, (void *)bmBytes, bmSize); //calls 
sqlite3_bind_blob (bmBytes is the pData param)

Then I call StepQuery to execute it, and then I close the query. 
sqlite3_bind_blob() returns SQLITE_OK when I run it, so it does not seem to be 
encountering an error there.

Something apparently gets written to the database, but it does not seem to be 
correct. When I try to retrieve and display my image, it is all black (which is 
how bitmaps look when there is no data).

I think that the problem is coming from writing the BLOB to the database, but I 
am not entirely sure. Just in case it is getting written properly and I am not 
reading it from the database correctly, I will show you how I pulled it from 
the DB.

//prepare blob
sqlite3_blob* pBlob = NULL;
sqlite3_blob_open(m_sqliteDB, "main", "tblBlob", "Data", iBlobID, FALSE, 
&pBlob);

BLOB_HANDLE hBlob = pBlob;
int size = sqlite3_blob_bytes(hBlob); //works correctly, returns 998058
BYTE* pBuffer = g_MemMgr.AllocDataBuffer(size);

sqlite3_blob_read(hBlob, pBuffer, size, 0); //returns SQLITE_OK
sqlite3_blob_close(hBlob);

I then try to make a bitmap out of the bytes in pBuffer, but when I do, it 
turns out all black (as I mentioned earlier).

Do I have the concept correct? And if so, what part of my current code should I 
change to be able to use my DB to store image data? 

Thanks a lot.



_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to