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