SQLite database operations in iOS, iossqlite
Implement SQLite database operations in iOS: 1. Import framework (libsqlite3.0.tbd) 2. Import header file <sqlite3.h> 3. add, delete, modify, and query data
Demo process for simple SQLite database operations:
1. Create a. h.m file named SQLite_Manage and import the header file <sqlite3.h>
2. The database has only one app and uses the singleton mode: (the Code is as follows)
1 + (SQLite_Manager *)sharedManager{2 static SQLite_Manager *manager = nil;3 static dispatch_once_t onceToken;4 dispatch_once(&onceToken, ^{5 manager = [[SQLite_Manager alloc]init];6 });7 return manager;8 }
3. Open the database. The Code is as follows:
1-(void) open {2 // document Path 3 NSString * docment = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 4 // sqlite Path 5 NSString * sqlitePath = [docment stringByAppendingPathComponent: @ "database. sqlite "]; 6 // open the database 7 int result = sqlite3_open (sqlitePath. UTF8String, & db); 8 // determines whether the database is successfully opened. 9 if (result = SQLITE_ OK) {10 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "opened successfully" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 11 [alertView show]; 12} else {13 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "failed to open" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 14 [alertView show]; 15} 16}
4. Create a table using the following code:
1-(void) creatTable {2 // SQL statement 3 NSString * sqlString = @ "create table Person (id integer primary key, name text, age integer )"; 4 // Execute SQL statement 5 char * error = nil; 6 sqlite3_exec (db, sqlString. UTF8String, nil, nil, & error); 7 8 // check whether an error occurs. 9 if (error = nil) {10 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "table created successfully" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 11 [alertView show]; 12} else {13 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "Table creation failed" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 14 [alertView show]; 15} 16}
5. Insert data. The Code is as follows:
1-(void) insert {2 // SQL statement 3 NSString * sqlString = @ "insert into Person ('name', 'age') values ('ager ', 18) "; 4 // Execute SQL statement 5 char * error = nil; 6 sqlite3_exec (db, sqlString. UTF8String, nil, nil, & error); 7 // check whether an error occurs. 8 if (error = nil) {9 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "data inserted successfully" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 10 [alertView show]; 11} else {12 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "data insertion failed" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 13 [alertView show]; 14} 15 16}
6. Modify the data. The Code is as follows:
1-(void) update {2 // SQL statement 3 NSString * sqlString = @ "update Person set 'name' = 'arn' where id = 1 "; 4 // Execute SQL statement 5 char * error = nil; 6 sqlite3_exec (db, sqlString. UTF8String, nil, nil, & error); 7 8 // check whether an error occurs. 9 if (error = nil) {10 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "data updated successfully" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 11 [alertView show]; 12} else {13 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "data update failed" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 14 [alertView show]; 15} 16}
7. query data. The Code is as follows:
1-(void) select {2 // SQL statement 3 NSString * sqlString = @ "select * from Person"; 4 // prepare SQL 5 sqlite3_stmt * stmt = nil; 6 sqlite3_prepare (db, sqlString. UTF8String,-1, & stmt, nil); 7 // Statement 8 while (sqlite3_step (stmt) = SQLITE_ROW) {9 int ID = sqlite3_column_int (stmt, 0); 10 const unsigned char * name = sqlite3_column_text (stmt, 1); 11 int age = sqlite3_column_int (stmt, 2); 12 NSLog (@ "% d, % s, % d ", ID, name, age); 13} 14 sqlite3_finalize (stmt); 15}
8. The code for deleting data is as follows:
1-(void) deleteData {2 // SQL statement 3 NSString * sqlString = @ "delete from Person where id = 1 "; 4 // Execute SQL statement 5 char * error = nil; 6 sqlite3_exec (db, sqlString. UTF8String, nil, nil, & error); 7 // check whether an error occurs. 8 if (error = nil) {9 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "deleted successfully" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 10 [alertView show]; 11} else {12 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "deletion failed" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 13 [alertView show]; 14} 15}
9. Shut down the database. The Code is as follows:
1-(void) close {2 // close database 3 int result = sqlite3_close (db); 4 // determine whether the database is closed successfully 5 if (result = SQLITE_ OK) {6 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "disabled successfully" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK ", nil]; 7 [alertView show]; 8} else {9 UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "database execution result" message: @ "failed to close" delegate: nil cancelButtonTitle: nil otherButtonTitles: @ "OK", nil]; 10 [alertView show]; 11} 12}