看了下sqlITE3的操作类,对于blob的字段存/取操作已经会用了。
但是只能一次性的读取文件长度,然后分配内存。对于分批次写入,我现在还没有弄明白,需要进一步的学习。
写在这里记录一下吧。
Cppsqlite3DB db; LPCSTR pstrFileName ="d:\\test.s3db"; try { db.open(pstrFileName); unsigned char* bin ; Cppsqlite3Binary blob; HANDLE hFile = CreateFile("c:\\test.bmp",GENERIC_READ,FILE_SHARE_READ,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); DWORD dwFileSize=0; dwFileSize =GetFileSize(hFile,&dwFileSize); bin = new unsigned char[dwFileSize];//一次性分配内存 ,好郁闷啊。 //写入数据库 DWORD dwRead = 0 ; DWORD dwSize = dwFileSize; ReadFile(hFile,bin,dwSize,&dwRead,0); blob.setBinary(bin,dwSize); Cppsqlite3Buffer bufsql; bufsql.format("insert into bindata values ('testing',%Q);",blob.getEncoded()); db.execDML(bufsql); CloseHandle(hFile); delete bin; //读取blob字段,并创建生成新的文件 hFile = CreateFile("c:\\TEST2.bmp",GENERIC_WRITE,OPEN_ALWAYS,0); Cppsqlite3Query q ; q = db.execQuery("select data from bindata where desc = 'testing';"); //读到内存中 if (!q.eof()) { blob.setEncoded((unsigned char*)q.fieldValue("data")); } const unsigned char* pbin = blob.getBinary(); DWORD dwWrite; WriteFile(hFile,pbin,blob.getBinaryLength(),&dwWrite,0);//写入文件 CloseHandle(hFile); } catch(Cppsqlite3Exception &e) { m_list.AddString(e.errorMessage()); }sqlITE3的C语言api没有完全理解,现在只能这么办了,也是没办法。 原文链接:https://www.f2er.com/sqlite/202290.html