C language for iOS sqlite

Source: Internet
Author: User

C language for iOS sqlite

I took a look at sqlite knowledge on Saturday and recorded it here. A video of Chuanzhi podcast

The data operation is basically adding, deleting, modifying, and querying:

Static sqlite3 * db; // declare a Database @ implementation XSDBOperator + (void) initialize {NSString * filename = [[declare (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "student. sqlite "]; // database file path // if the file exists, open the database file. If the file does not exist, create it and open int result = sqlite3_open (filename. UTF8String, & db); // determines whether the database is successfully opened. if (result = SQLITE_ OK) {NSLog (@ "");} else {NSLog (@ "failed to open") ;}# pragma mark-create database table + (BOOL) creatTable {char * SQL = "create table if not exists t_student (id integer primary key autoincrement, name text, age integer, score integer);"; char * errorMesg = NULL; int result = sqlite3_exec (db, SQL, NULL, NULL, & errorMesg); // if (result = SQLITE_ OK) {return YES ;} else {NSLog (@ "Table creation failed, cause of failure: % s", errorMesg); return NO ;}}+ (BOOL) insert {char * SQL = "insert into t_table (name, age) values ('jack', 20)"; char * errorMesg = NULL; int result = sqlite3_exec (db, SQL, NULL, NULL, & errorMesg); if (result = SQLITE_ OK) {return YES;} else {NSLog (@ "failed to insert data, cause of failure: % s", errorMesg ); return NO ;}+ (BOOL) updata {char * SQL = "updata t_table set age = 12"; char * errorMesg = NULL; int result = sqlite3_exec (db, SQL, NULL, NULL, & errorMesg); if (result = SQLITE_ OK) {return YES;} else {NSLog (@ "failed to update data, cause of failure: % s", errorMesg ); return NO ;}+ (void) query {char * SQL = "select id, name, age from t_student;"; // set sqlite3_stmt * stmt = NULL to save the query result; int result = sqlite3_prepare_v2 (db, SQL,-1, & stmt, NULL); if (result = SQLITE_ OK) {NSLog (@ "the query statement is valid "); while (sqlite3_step (stmt) = SQLITE_ROW) {// traverse by row until the result is obtained. int sid = sqlite3_column_int (stmt, 0 ); // obtain the value of the column const unsigned char * sname = sqlite3_column_text (stmt, 1); int sage = sqlite3_column_int (stmt, 2 ); NSLog (@ "% d, % s, % d", sid, sname, sage) ;}} else {NSLog (@ "query statement invalid") ;}} @ end


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.