SQLite Fifth Lesson Application case

Source: Internet
Author: User
Tags sprintf sqlite stmt

1 Open the database file

sqlite3* m_db = NULL;

int ret = SQLITE3_OPEN_V2 ("Test.db", &db, Sqlite_open_readwrite, NULL);

if (ret! = SQLITE_OK)

{

Return

}


2 If the table does not exist, create a table

Char szcreateuserdatasql[1024] = "CREATE table if not exists Tb_user (id INTEGER, \

Type integer,\

Kind INTEGER) ";


3 Creating a unique primary key ID

Char szcreateuserdatasql[1024] = "CREATE table if not exists Tb_user (ID INTEGER PRIMARY KEY autoincrement,\

Type integer,\

Kind INTEGER) ";


4 queries

Char szsql[1024] = {0};

sprintf (szSQL, "SELECT DISTINCT * from Tb_test");

sqlite3_stmt* stmt = NULL;

Sqlite3_prepare (m_db, szSQL, 1, &stmt, 0);

while (Sqlite3_step (stmt) = = Sqlite_row)

{

Const unsigned char* szpoiname = sqlite3_column_text (stmt, 0);

Char szname[128] = {0};

if (szpoiname)

{

sprintf (SzName, "%s", szpoiname);

}

int kx = Sqlite3_column_int (stmt, 3);

float x = sqlite3_column_double (stmt, 5);

}

Sqlite3_finalize (stmt);


5 get error message for SQL execution failure

char* errmsg = NULL;

char* szSQL = "SELECT * From address";

Nret = Sqlite3_exec (PDB, szsql, NULL, NULL, &ERRMSG);

if (nret! = SQLITE_OK)

{

cout<<errmsg<<endl;

Sqlite3_free (errmsg);

}

Note: Release the memory that ErrMsg points to

6 Closing the database

Sqlite3_close (DB);


SQLite Fifth Lesson Application case

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.