In actual programming and development, we often need to process the storage of large-capacity binary data, such as slices or music. For these binary data (blob fields), we cannot simply insert or query the data as if we were dealing with common text. Therefore, SQLite provides a set of functions to process this BLOB field type. The following code demonstrates how to use these API functions.
SQLite3 installation and basic operations
Simple Application of SQLite databases in Ubuntu 12.04
How to install SQLite in Ubuntu 12.04
First create a database and then create a data table:
NsqSt = sqlite3_exec (handle, "create table if not exists parameters (fullname text primary key, vt integer, length integer, value blob, usefunc integer)", NULL, NULL,
NULL );
InsertParaWithValue demonstrates inserting blob Data
DbHandle: Call this function to obtain sqlite3_open_v2 (DM_DB_FILENAME, & dbHandle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
Int insertParaWithValue (DBHANDLE dbHandle, const char * pszParaName, int type, const void * pData, int cbData)
{
Const char * pTmp = "insert into parameters (FULLNAME, VT, LENGTH, VALUE, USEFUNC) VALUES ('% s', % d, % d ,?, 0 )";
Size_t nTmp = strlen (pTmp );
Char * pSql = DMMalloc (nTmp + strlen (pszParaName) + 11 + 11 + 1);/* 32bit decimal int max length is 11 */
If (pSql = NULL)
Return ERROR;
Sprintf (pSql, pTmp, pszParaName, type, cbData );
Sqlite3_stmt * pstmt = NULL;
Int nRet = sqlite3_prepare_v2 (dbHandle, pSql,-1, & pstmt, NULL );
If (nRet! = SQLITE_ OK)
{
Goto ERR;
}
DMFree (pSql );
PSql = NULL;
NRet = sqlite3_bind_blob (pstmt, 1, pData, cbData, NULL );
If (nRet! = SQLITE_ OK)
{
Goto ERR;
}
NRet = sqlite3_step (pstmt );
If (nRet! = SQLITE_DONE)
{
Goto ERR;
}
NRet = SQLITE_ OK;
Sqlite3_finalize (pstmt );
Return nRet;
ERR:
If (pstmt)
Sqlite3_finalize (pstmt );
If (pSql)
DMFree (pSql );
Return nRet;
}