This example for you to share the iOS SQLite specific operation methods for your reference, the specific content as follows
#import <sqlite3.h> @interface Viewcontroller () {sqlite3 *_sqldb;}
@end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];
Do no additional setup after loading the view, typically from a nib.
[Self opendb];
[Self createtable];
[Self insertdata];
[Self FindData]; }//Open database-(void) opendb{Nsarray *arrs= nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, Y
ES);
Create a database, if the database exists directly open, does not exist on the Create open NSString *path=[arrs lastobject];
NSString *documentpath= [path stringbyappendingpathcomponent:@ "sql.db"];
int reslut= Sqlite3_open ([documentpath utf8string], &_sqldb);
if (RESLUT==SQLITE_OK) {NSLog (@ "database is open"); Create table through database instance-(void) createtable{//without parameters SQL statement Const char* sql= "CREATE table if not exists T_person (ID integer pri
Mary key Autoincrement,name text,age integer); ";
Char *error; Sqlite3_exec can execute all SQL statements with no parameters.
If it is best not to take parameters, prevent SQL injection vulnerabilities attack int resutl= sqlite3_exec (_sqldb, SQL, NULL, NULL, &ERROR); if (rESUTL==SQLITE_OK) {NSLog (@ "CREATE table succeeded");
} else{NSLog (@ "CREATE TABLE failed--"%s ", error);} Insert data-(void) insertdata{///SQL statement with parameters "?"
is Placeholder const char * sql= with parameters insert into T_person (name,age) values (?,?); ";
Sqlite3_stmt *stmp;
Check the SQL statement syntax before executing the SQL statement-1 represents the length of the string int result= sqlite3_prepare_v2 (_sqldb, SQL,-1, &stmp, NULL);
if (RESULT==SQLITE_OK) {NSLog (@ "Insert SQL statement syntax no problem");
Binding parameters, the subscript of the inserted parameters is starting from 1 sqlite3_bind_text (stmp, 1, "GCB",-1, NULL);
Sqlite3_bind_int (STMP, 2, 12);
The SQL statement that executes the parameter argument cannot have exec int result=sqlite3_step (STMP);
Insert to judge, to use Sqlite_done to judge if (Result==sqlite_done) {NSLog (@ "Insert success");
} else{NSLog (@ "Insert failed");
} else{NSLog (@ "There is a problem inserting SQL statement");
}-(void) finddata{char *sql= "Select Id,name,age from T_person";
The query completes the sqlite3_stmt *stmt with the step;
Check the syntax problems of SQL statements int result= SQLITE3_PREPARE_V2 (_sqldb, SQL,-1, &stmt, NULL); The IF (RESULT==SQLITE_OK) {while sqlite3_step (stmt) ==sqlite_row) {//query's column is 0 starting at the beginning of the inserted column starting at 1/int xh=sqlite3_column_int (stmt,
0); int Xh=sqlite3_colUmn_int (stmt, 0);
char * name= (char *) Sqlite3_column_text (stmt, 1);
int Age=sqlite3_column_int (stmt, 2);
NSLog (@ "xh=%i-->name=%s-->age=%i", xh,name,age);
} else{NSLog (@ "Query SQL syntax is incorrect");
}
}
The above is the entire content of this article, I hope to help you learn.