使用
sqlite3 访问Blob字段 2012-06-13 18:47:19|
分类:
数据库 |字号
订阅 原文地址: http://blog.csdn.net/blueblood7/article/details/6576716
sqlite 是一个免费的
文件型
数据库。 1、 下载地址。 http://www.
sqlite.org/download.html。 2、 编译。 C++: 建空的动态库或静态库,加入
sqlite3.c,
sqlite3.h和
sqlite3ext.h,编译即可。 需注意的是动态库要把
sqlite3.c 中的 #ifndef
sqlITE_API # define
sqlITE_API #endif 改为 #ifndef
sqlITE_API # define
sqlITE_API __declspec(dllexport) #endif 3、 建表。 包含字段 ID(图像ID,自增),FILENAME(图像名),DATA(图像数据)。 BOOL Ascii2Utf8(const char* pcszAscii,char* pszUtf8,int* pnUtf8Size) { if (!pcszAscii || !pszUtf8 || !pnUtf8Size) return FALSE; int nWCharCount = MultiByteToWideChar(CP_ACP,pcszAscii,-1,NULL,0); wchar_t* pwszStr = new wchar_t[nWCharCount]; MultiByteToWideChar(CP_ACP,pwszStr,nWCharCount); int nUtf8Count = WideCharToMultiByte(CP_UTF8,nWCharCount,NULL); if (nUtf8Count > *pnUtf8Size) { *pnUtf8Size = nUtf8Count; return FALSE; } WideCharToMultiByte(CP_UTF8,pszUtf8,nUtf8Count,NULL); delete[] pwszStr; *pnUtf8Size = nUtf8Count; return TRUE; } BOOL Utf82Ascii(const char* pcszUtf8,char* pszAscii,int* pnAsciiSize) { if (!pcszUtf8 || !pszAscii || !pnAsciiSize) return FALSE; int nWCharCount = MultiByteToWideChar(CP_UTF8,pcszUtf8,0); wchar_t* pwszStr = new wchar_t[nWCharCount]; MultiByteToWideChar(CP_UTF8,nWCharCount); int nAsciiCount = WideCharToMultiByte(CP_ACP,NULL); if (nAsciiCount > *pnAsciiSize) { *pnAsciiSize = nAsciiCount; return FALSE; } WideCharToMultiByte(CP_ACP,pszAscii,nAsciiCount,NULL); delete[] pwszStr; *pnAsciiSize = nAsciiCount; return TRUE; } BOOL OpenDatabase(const char* pcszDBFileName,int nFlag,
sqlite3** ppDb) { char szUtf8DBFileName[MAX_PATH]; int nSize = MAX_PATH; if (!Ascii2Utf8(pcszDBFileName,szUtf8DBFileName,&nSize)) return FALSE;
sqlite3* pDb; int nRtn =
sqlite3_open_v2(szUtf8DBFileName,&pDb,nFlag,NULL); if (nRtn !=
sqlITE_OK) return FALSE; *ppDb = pDb; return TRUE; } void CDemoDlg::OnCreatetable() { // TODO: Add your control notification handler code here const char* pcszDBFileName = "d://测试
中文//demo.db";
sqlite3* pDb; if (!OpenDatabase(pcszDBFileName,
sqlITE_OPEN_READWRITE|
sqlITE_OPEN_CREATE,&pDb)) return; const char* pcsz
sql = "create table images(ID integer primary key,FILENAME varchar(50),DATA blob);"; char* pszErrorInfo = NULL; int nRtn =
sqlite3_exec(pDb,pcsz
sql,&pszErrorInfo); if (nRtn !=
sqlITE_OK) { TRACE("
sqlite3_exec error: %s/n",pszErrorInfo);
sqlite3_free(pszErrorInfo); }
sqlite3_close(pDb); return; } 4、 插入数据。 BYTE* GetFileData(const char* pcszFileName,int* pnFileLen) { FILE* fp = fopen(pcszFileName,"rb"); if (!fp) return NULL; fseek(fp,SEEK_END); int nFileLen = ftell(fp); fseek(fp,SEEK_SET); BYTE* pbyData = new BYTE[nFileLen]; if (!pbyData) { fclose(fp); return NULL; } fread(pbyData,nFileLen,1,fp); fclose(fp); if (pnFileLen) *pnFileLen = nFileLen; return pbyData; } void FreeFileData(void* pData) { delete[] pData; } void CDemoDlg::OnInsert() { // TODO: Add your control notification handler code here const char* pcszDBFileName = "d://测试
中文//demo.db";
sqlite3* pDb; if (!OpenDatabase(pcszDBFileName,
sqlITE_OPEN_READWRITE,&pDb)) return; const char* pcszFileName = "d://测试
图片//
图片1.jpg"; int nFileLen; BYTE* pbyData = GetFileData(pcszFileName,&nFileLen); if (!pbyData) return; char sz
sql[100]; sprintf(sz
sql,"insert into images values(NULL,'%s',?);",pcszFileName); char szUtf8
sql[200]; int nSize = 200; if (!Ascii2Utf8(sz
sql,szUtf8
sql,&nSize)) { FreeFileData(pbyData); return; }
sqlite3_stmt* pStmt = NULL; int nRtn =
sqlite3_prepare_v2(pDb,&pStmt,NULL); if (nRtn ==
sqlITE_OK) { nRtn =
sqlite3_bind_blob(pStmt,pbyData,NULL); if (nRtn ==
sqlITE_OK) { nRtn =
sqlite3_step(pStmt); if (nRtn ==
sqlITE_DONE) { TRACE("insert succ!/n"); } }
sqlite3_finalize(pStmt); } FreeFileData(pbyData);
sqlite3_close(pDb); return; } 5、
查询。 void CDemoDlg::OnQuery() { // TODO: Add your control notification handler code here const char* pcszDBFileName = "d://测试
中文//demo.db";
sqlite3* pDb; if (!OpenDatabase(pcszDBFileName,
sqlITE_OPEN_READONLY,&pDb)) return; const char* pcsz
sql = "select * from images;";
sqlite3_stmt* pStmt = NULL; int nRtn =
sqlite3_prepare_v2(pDb,NULL); if (nRtn ==
sqlITE_OK) { nRtn =
sqlite3_step(pStmt); while (nRtn ==
sqlITE_ROW) { int nID =
sqlite3_column_int(pStmt,0); const char* pcszUtf8FileName = (const char*)
sqlite3_column_text(pStmt,1); char szFileName[MAX_PATH]; int nSize = MAX_PATH; Utf82Ascii(pcszUtf8FileName,szFileName,&nSize); const BYTE* pbyData = (const BYTE*)
sqlite3_column_blob(pStmt,2); int nDataLen =
sqlite3_column_bytes(pStmt,2); TRACE("id=%d,filename=%s,data len=%d/n",nID,nDataLen); char szNewFileName[MAX_PATH]; strcpy(szNewFileName,szFileName); char* pszExtension = strrchr(szNewFileName,'.'); *pszExtension = 0; strcat(szNewFileName,"__"); pszExtension = strrchr(szFileName,'.'); strcat(szNewFileName,pszExtension); FILE* fp = fopen(szNewFileName,"wb"); if (fp) { fwrite(pbyData,nDataLen,fp); fclose(fp); } nRtn =
sqlite3_step(pStmt); }
sqlite3_finalize(pStmt); }
sqlite3_close(pDb); return; }