Note the point:
In a select query, you cannot step into the following form, or a dead loop will occur:
int Stepresult=sqlite3_step (statement); while (Stepresult==sqlite_row) {}
Can only be combined into one sentence to write:
while (Sqlite3_step (statement) ==sqlite_row) { }
Because, the sqlite3_step is executed as follows:
* * ^if The SQL statement being executed returns any data and then [Sqlite_row]
* * is returned each time a new row of data are ready for processing by the
* * caller. The values may accessed using the [column access functions].
* * SQLITE3_STEP () is called again to retrieve the next row of data.
#import "ViewController.h" #import <sqlite3.h> @interface Viewcontroller ()-(ibaction) Insert: (ID) sender;-( ibaction) Delete: (ID) sender;-(ibaction) Update: (ID) sender;-(ibaction) Select: (ID) sender; @end @implementation Viewcontrollerstatic sqlite3 *_db;-(void) viewdidload {[Self setupdatabaseandtable]; [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(void) setupdatabaseandtable{nsstring *filename=[[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject] stringbyappendingpathcomponent:@ "Ios.sql"]; NSLog (@ "%@", filename); int Opendbresult=sqlite3_open (filename. Utf8string, &_db); if (OPENDBRESULT==SQLITE_OK) {//CREATE TABLE NSString *[email protected] "CREATE TABLE IF not EXISTS user (ID in Teger PRIMARY KEY autoincrement,name text,age INTEGER);"; Char *error=nil; int Createtableresult=sqlite3_exec (_db, createtablesql.utf8string, NULL, 0, &error); if (CREATETABLERESULT==SQLITE_OK) {NSLog (@ "CREATE table succeeded"); }else{NSLog (@ "CREATE TABLE failed"); } NSLog (@ "Open database succeeded"); }else{NSLog (@ "Failed to open database"); }}-(ibaction) Insert: (ID) sender {for (int i=0; i<50; i++) {nsstring *name=[nsstring stringwithformat:@ "Jack %d ", Arc4random ()%100]; int age=arc4random ()%100; NSString *inserttablesql=[nsstring stringwithformat:@ "INSERT into User (name,age) VALUES ('%@ ',%d);", Name,age]; Char *error=nil; int Insertresult=sqlite3_exec (_db, inserttablesql.utf8string, NULL, 0, &error); if (INSERTRESULT==SQLITE_OK) {NSLog (@ "%d%@%d", i,name,age); }else{NSLog (@ "Insert data failed"); }}}-(Ibaction) Delete: (ID) Sender {nsstring *[email protected] "Delete from user WHERE id=1;"; Char *error=nil; int Deleteresult=sqlIte3_exec (_db, deleterowsql.utf8string, NULL, 0, &error); if (DELETERESULT==SQLITE_OK) {NSLog (@ "Delete data succeeded"); }else{NSLog (@ "Delete data failed"); }}-(ibaction) Update: (ID) Sender {nsstring *[email protected] "Update user SET age=130 WHERE id=3;"; Char *error=nil; int Updateresult=sqlite3_exec (_db, updaterowsql.utf8string, NULL, 0, &error); if (UPDATERESULT==SQLITE_OK) {NSLog (@ "Modify database Success"); }else{NSLog (@ "Failed to modify database"); }}-(ibaction) Select: (ID) Sender {nsstring *[email protected] "Select id,name,age from User WHERE id=5;"; Sqlite3_stmt *statement; int Stmtresult=sqlite3_prepare_v2 (_db, Selectrowsql.utf8string,-1, &statement, nil); if (STMTRESULT==SQLITE_OK) {NSLog (@ "stmtresult success"); The following statement will appear in a dead loop while (Sqlite3_step (statement) ==sqlite_row) {NSLog (@ "Stepresult succeeded"); int Uid=sqlite3_column_int (statement, 0); Const unsigned char *uname=sqlite3_column_text (statement, 1); int Uage=sqlite3_column_int (statement, 2); NSLog (@ "%d%s%d", uid,uname,uage); }}else{NSLog (@ "Stmtresult failed"); }} @end
"iOS Dev-104" SQLite uses: Note Query time-of-step write while there is a dead loop