IOS learning notes 0C-SQLite data storage, iossqlite storage pictures
1. Import the libsqlite3.0.dylib framework to the Project
2. Import # Impourt <sqlite3.h> from the data sqlite header file (. h ).
3. We recommend that you use the sqlite Manager management software on your local computer.
The code below
//// ViewController. m // SQLiteDemo /// Created by wangtouwang on 15/4/9. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import "ViewController. h "# import" WPUser. h "# define DBNAME @" mysqlite. sqlite "@ interface ViewController () {sqlite3 * db;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self createSqlDBOrOpenDB]; [self createTable]; // for (int index = 0; inde X <30; index ++) {// [self insertIntoData]; //} [self updateData: [self searchAllData];} # pragma mark update data-(void) updateData :( NSMutableArray *) array {NSString * SQL = @ "update user set name = ?, Age =? Where id =? "; Sqlite3_stmt * stmt; for (int index = 0; index <array. count; index ++) {WPUser * user = (WPUser *) array [index]; user. name = @ "Zhang Jianhua"; user. age = 31; if (sqlite3_prepare_v2 (db, [SQL UTF8String],-1, & stmt, NULL) = SQLITE_ OK) {sqlite3_bind_text (stmt, 1, [user. name UTF8String],-1, NULL); sqlite3_bind_int (stmt, 2, user. age); sqlite3_bind_int (stmt, 3, user. ID); if (sqlite3_step (stmt) = SQLITE_DONE) {NSLog (@ "Update Succeeded ") ;}}# pragma mark reads data-(NSMutableArray *) searchAllData {NSMutableArray * array = [NSMutableArray array]; NSString * SQL = @" select id, name, age from user "; sqlite3_stmt * stmt; if (sqlite3_prepare_v2 (db, [SQL UTF8String],-1, & stmt, NULL) = SQLITE_ OK) {while (sqlite3_step (stmt) = SQLITE_ROW) {NSInteger ID = sqlite3_column_int (stmt, 0); char * name = (char *) sqlite3_column_text (stmt, 1); NSString * Uname = [[NSString alloc] initwithuf8string: name]; NSInteger age = sqlite3_column_int (stmt, 2 ); NSLog (@ "ID = % lu NAME = % s AGE = % lu", ID, name, age); WPUser * user = [WPUser initUserName: uname Age: age]; user. ID = ID; [array addObject: user] ;}} sqlite3_close (db); return array ;}# pragma mark adds data-(void) insertIntoData {NSString * SQL = @ "insert into user (name, age) values (?,?) "; WPUser * user = [WPUser initUserName: @" JACK "Age: 11]; sqlite3_stmt * stmt; // check whether the syntax is correct before inserting data if (sqlite3_prepare_v2 (db, [SQL UTF8String],-1, & stmt, NULL) = SQLITE_ OK) {// bind parameter 2: sequence number (starting from 1), parameter 3: is the string value, and parameter 4 is the string length. The fifth parameter of sqlite3_bind_text is a function pointer sqlite3_bind_text (stmt, 1, [user. name UTF8String],-1, NULL); sqlite3_bind_int (stmt, 2, user. age); // INSERT sqlite3_step if (sqlite3_step (stmt) = SQLITE_DONE) {NSLog (@ "insert into success ");} else {NSLog (@ "insert into error") ;}}# pragma mark creates a table-(void) createTable {NSString * SQL = @ "create table if not exists user (id integer primary key autoincrement, name text, age I Nteger); "; char * errormsg; // execute the create statement sqlite3_exex is function parameter 1: Database handle (db), parameter 2: SQL statement, parameter 3 callback parameter, is a pointer to a function. If you change * in front of callback to ^, It is a block code segment. // parameter 4 can be NULL, the fifth parameter is the error message. // you must first check whether the table already exists int result = sqlite3_exec (db, [SQL UTF8String], NULL, NULL, & errormsg ); if (result = SQLITE_ OK) {NSLog (@ "created successfully");} else {NSLog (@ "failed to create: % s", errormsg );}} # pragma mark: Create a database instance or open a data instance-(void) createSqlDBOrOpenDB {// get project Doucm Ent file directory address NSString * doc = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSLog (@ "% @", doc ); // create an SQLITE file or obtain the file path NSString * DB_PATH = [doc stringByAppendingPathComponent: DBNAME]; NSLog (@ "% @", DB_PATH ); // open or create a DB instance. The syntax and pointer int result = sqlite3_open ([DB_PATH UTF8String], & db) in C are used here ); // sqlite3_open method name parameter const filename DB file path, the second parameter is the database instantiation object return parameter int type If (result! = SQLITE_ OK) {sqlite3_close (db); // close the instantiation NSLog (@ "failed to open the data cry instantiation object");} else {NSLog (@ "CREATE SUCCESS ");}} @ end
//// WPUser. m // SQLiteDemo /// Created by wangtouwang on 15/4/9. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import "WPUser. h "@ implementation WPUser-(instancetype) initUserName :( NSString *) name Age :( NSInteger) age {if (self = [super init]) {self. name = name; self. age = age;} return self;} + (instancetype) initUserName :( NSString *) name Age :( NSInteger) age {return [[WPUser alloc] initUserName: name Age: age];} @ end