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