How to Use APIs to operate BLOB fields in SQLite?

Source: Internet
Author: User
Tags psql

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;
}

  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.