实现功能:1.将图片文件(.jpg)以二进制形式存储到sqlite3里面
2.从sqlite3里面读取二进制形式的图片数据并保存到图片文件(.jpg)
*注:本人超级菜鸟 此博客是为了总结用 仅仅说明了函数作用 可能直接编译不过 见谅 文中有何不妥之处 还望各位读者不吝赐教
一、创建/打开 sqlite3数据库 并 创建/打开 表
C++
#define TABLENAME _T("tb")
CppSQLite3DB g_db;
CString strDbPath;
CString strDbName(_T("test1.db"));
GetCurrentDirectory( strDbPath );
strDbPath += strDbName;
try{
g_db.open(strDbPath);
}
catch(CppSQLite3Exception ex)
{
g_db.execDML(L"rollback transaction;");
}
CString strTbSql;
CString strTbName(TABLENAME);
try{
if(!g_db.tableExists(strTbName))
{
strTbSql.Format(_T("CREATE TABLE %s(ID INTEGER PRIMARY KEY, image BLOB);"), strTbName);
g_db.execDML(strTbSql);
strTbSql.Empty();
strTbSql.Format(_T("表%s创建成功!"), strTbName);
::AfxMessageBox(strTbSql);
}
else
{
strTbSql.Format(_T("表%s已经存在!"), strTbName);
::AfxMessageBox(strTbSql);
}
}
catch(CppSQLite3Exception ex)
{
g_db.execDML(L"create table exception!");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
二、然后拿到图片文件 打开图片文件并保存到sqlite3数据库中
C++
CString strFilePath(_T("C:\\pic1.jpg"));
CFile file;
file.Open(strFilePath, CFile::typeBinary | CFile::modeRead);
int nFileSize = file.GetLength();
BYTE *pbytBuffer = new BYTE[nFileSize];
file.Read(pbytBuffer, nFileSize);
CppSQLite3Statement stmt;
CString strSqlInsert;
strSqlInsert.Format(
_T("INSERT INTO %s values(1, ?)"), TABLENAME);
stmt = g_db.compileStatement(strSqlInsert);
stmt.bind(1, pbytBuffer, nFileSize);
stmt.execDML();
stmt.finalize();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
三、从sqlite3中读取二进制数据 并将其保存为jpg文件
C++
sqlite3 *db;
if( SQLITE_OK != sqlite3_open("test1.db", &db) )
{
if(IDOK == ::AfxMessageBox(_T("db open error!")))
return;
}
sqlite3_stmt *pStmt;
CString strSqlSelect;
sqlite3_prepare( db, "select * from tb1 where ID=1;", -1, &pStmt, 0 );
sqlite3_step(pStmt);
ULONG ulImageSize = sqlite3_column_bytes(pStmt, 1);
char *pBufferDst = new char[ulImageSize];
memset(pBuffer, 0, ulImageSize);
char *pBuffer =
(char *)sqlite3_column_blob(pStmt,1);
memcpy(pBufferDst, pBuffer, ulImageSize);
sqlite3_finalize(pStmt);
sqlite3_close(db);
CFile file;
file.Open(_T("C:\\222.jpg"), CFile::typeBinary | CFile::modeWrite | CFile::modeCreate);
file.Write(pBufferDst, ulImageSize);
delete[] pBufferDst;
pbytBuffer = NULL;
file.Close();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
四、最后在提供不用CppSQLite3U封装类的存储方式 – 简写了
C++
sqlite3 * db;
int result;
char buf[64];
char *errmsg;
errmsg = buf;
result = sqlite3_open("test.db", &db );
char *sql = "CREATE TABLE tb(ID INTEGER, binData BLOB);";
result = sqlite3_exec( db, sql, NULL, NULL, &errmsg);
if(result != SQLITE_OK)
{
printf("Error : %s\n", errmsg);
}
sqlite3_stmt *stmt;
sqlite3_prepare( db, "insert into tb( ID, binData) values( 20, ? );", -1, &stmt, 0 );
result=sqlite3_step( stmt );
sqlite3_step( stmt );
sqlite3_finalize( stmt );
sqlite3_close( db );
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
*注:本人超级菜鸟 此博客是为了总结用 仅仅说明了函数作用 可能直接编译不过 见谅 文中有何不妥之处 还望各位读者不吝赐教