(113) Operation of SQLite3 database using the system's own frame

Source: Internet
Author: User
Tags stmt

The framework of the system is based on C language, the use is more cumbersome.

Here are the steps to use:

First import the Libsqlite3.0.dylib.

① opens the database in the document directory, if it is not created.

NSString *sqlitepath = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Test.sqlite"];//database does not exist automatically created sqlite3 *db = null;int state = Sqlite3_open ( Sqlitepath.utf8string, &db), if (state = = SQLITE_OK) {    NSLog (@ "Open database succeeded");} else{    NSLog (@ "Open database Failed");}
② Create a table and insert data.

const char *sql = "CREATE TABLE IF not EXISTS t_product (ID integer PRIMARY KEY autoincrement, Name text not NULL, price R EAL); "; Char *err = null;sqlite3_exec (db, SQL, NULL, NULL, &ERR); sql = "INSERT into t_product (name,price) VALUES (' drinks ',"); s QLITE3_EXEC (DB, SQL, NULL, NULL, &ERR);

③ query data.

Query data by executing step let stmt crawl data from front to back, get the data can be converted to OC string re-processing.

Query data//stmt will be automatically backwards sqlite3_stmt *stmt = NULL; stmt is used to fetch the result of the query sql = "SELECT * from t_product"; state = Sqlite3_prepare (db, SQL,-1, &stmt, NULL); if (state = = Sqlite_ OK) {    NSLog (@ "Ready for Success");    while (Sqlite3_step (stmt) = = Sqlite_row) {//successfully take out a data        const char *cname = (const char*) sqlite3_column_text (stmt, 1); /No. 0 column is ID, take out 1th column data name        const CHAR *cprice = (const char*) sqlite3_column_text (stmt, 2);//Remove 2nd Column data price        NSString * name = [NSString stringwithutf8string:cname];        NSString *price = [NSString stringwithutf8string:cprice];        NSLog (@ "%@%@", name,price);    }    } else{    NSLog (@ "Ready to Fail");}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(113) Operation of SQLite3 database using the system's own frame

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.