ios -- SQLITE

做iOS开发以来,一直运用的第三方数据库的增删改查,项目多次出现读取数据出现问题,自己也不知道如何查起,后来经过查资料,自己写了几个方法来实现数据的增删改查功能:

首先在TARGETS---》》Build phases里面添加数据库所关联的库文件libsqlite3.tbd

添加完以后,在控制器上添加

#import且增加属性

{

sqlite3*db;

}

在.m文件中实现增删改查的方法即可

#import"ViewController.h"

#define DBNAME    @"personinfo.sqlite"

#define NAME      @"name"

#define AGE       @"age"

#define ADDRESS   @"address"

#define TABLENAME @"PERSONINFO"

@interfaceViewController()

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[selfOpenDataBase];

[selfcreateTable];

[selfInsertData];

[selfSelectData];

[selfdeleteData];

[selfupdateData];

}

#pragma mark-----------打开数据库

-(void)OpenDataBase

{

NSArray*paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString*documents = [pathsobjectAtIndex:0];

NSString*database_path = [documentsstringByAppendingPathComponent:DBNAME];

if(sqlite3_open([database_pathUTF8String], &db) !=SQLITE_OK) {

sqlite3_close(db);

NSLog(@"数据库打开失败");

}

}

#pragma mark----------创建数据表PERSONINFO的语句

-(void)createTable

{

NSString*sqlCreateTable =@"CREATE TABLE IF NOT EXISTS PERSONINFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, address TEXT)";

[selfexecSql:sqlCreateTable];

}

#pragma mark------------创建一个独立的执行sql语句的方法

-(void)execSql:(NSString*)sql

{

char*err;

if(sqlite3_exec(db, [sqlUTF8String],NULL,NULL, &err) !=SQLITE_OK) {

sqlite3_close(db);

NSLog(@"数据库操作数据失败!");

}

}

#pragma mark------------增加数据

-(void)InsertData

{

NSString*sql1 = [NSStringstringWithFormat:

@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",

TABLENAME,NAME,AGE,ADDRESS,@"张三",@"18",@"北京"];

NSString*sql2 = [NSStringstringWithFormat:

@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",

TABLENAME,NAME,AGE,ADDRESS,@"老四",@"19",@"深圳"];

[selfexecSql:sql1];

[selfexecSql:sql2];

}

#pragma mark----------------删除数据

-(void)deleteData

{

sqlite3_stmt*statement;

NSString*sqlStr = [NSStringstringWithFormat:@"DELETE FROM PERSONINFO WHERE age = %d",19];

//        NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM PERSONINFO"];

intresult =sqlite3_prepare_v2(db, [sqlStrUTF8String], -1, &statement,NULL);

if(result ==SQLITE_OK) {

if(sqlite3_step(statement) ==SQLITE_ROW) {//觉的应加一个判断,若有这一行则删除

if(sqlite3_step(statement) ==SQLITE_DONE) {

sqlite3_finalize(statement);

sqlite3_close(db);

}

}

}

sqlite3_finalize(statement);

sqlite3_close(db);

NSLog(@"-------删除后---------");

[selfSelectData];

}

#pragma mark----------修改数据

-(void)updateData

{

sqlite3_stmt*statement;

NSString*sqlStr = [NSStringstringWithFormat:@"update PERSONINFO set name = '%@' where age = %d",@"王五",18];

//[db executeUpdate:@"UPDATE User SET Name = ? WHERE Name = ? ",@"李四",@"张三"];

intresult =sqlite3_prepare_v2(db, [sqlStrUTF8String], -1, &statement,NULL);

if(result ==SQLITE_OK) {

if(sqlite3_step(statement) ==SQLITE_ROW) {//觉的应加一个判断,若有这一行则修改

if(sqlite3_step(statement) ==SQLITE_DONE) {

sqlite3_finalize(statement);

sqlite3_close(db);

}

}

}

sqlite3_finalize(statement);

sqlite3_close(db);

NSLog(@"------修改后----------");

[selfSelectData];

}

#pragma mark-----------------查询数据

-(void)SelectData

{

NSString*sqlQuery =@"SELECT * FROM PERSONINFO";

sqlite3_stmt* statement;

if(sqlite3_prepare_v2(db, [sqlQueryUTF8String], -1, &statement,nil) ==SQLITE_OK) {

while(sqlite3_step(statement) ==SQLITE_ROW) {

char*name = (char*)sqlite3_column_text(statement,1);

NSString*nsNameStr = [[NSStringalloc]initWithUTF8String:name];

intage =sqlite3_column_int(statement,2);

char*address = (char*)sqlite3_column_text(statement,3);

NSString*nsAddressStr = [[NSStringalloc]initWithUTF8String:address];

NSLog(@"name:%@  age:%d  address:%@",nsNameStr,age, nsAddressStr);

}

}

sqlite3_close(db);

}

你可能感兴趣的:(ios -- SQLITE)