sqlite

#import "databasehandle.h"

#import@interface databasehandle ()

/**用来存放数据库的路径 */

@property(nonatomic,strong)NSString *filepath;

@end

static databasehandle *dataBse =nil;

@implementation databasehandle

/**数据库指针 */

static sqlite3 *DB =nil;

//懒加载lazy loading (只有被调用的时候才会被赋值)全局只要使用了一次self,那么在其他地方_方式也有只.但是一般来说都用self来调用懒加载.懒加载内部实现中最好不要出现self;

-(NSString *)filepath{

if (!_filepath) {

_filepath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"student.sqlite"];

}return _filepath;

}

//单利

+(instancetype)sharedDataBase{

if (!dataBse) {

dataBse =[[databasehandle alloc]init];

}return dataBse;

}

//创建表

-(void)createTable{

//1.准备sql语句

NSString *sqlString =@"create table if not exists person(name text,age integet)";

//2.执行sql语句

int result =sqlite3_exec(DB, sqlString.UTF8String, NULL, NULL, NULL);

if (result ==SQLITE_OK) {

NSLog(@"键表成功");

}else{

NSLog(@"键表失败%d",result);

}

}

-(void)OpenDB{

/**数据库存放的地址*/

NSLog(@"%@",self.filepath);

int result =sqlite3_open(self.filepath.UTF8String, &DB);

if (result ==SQLITE_OK) {

NSLog(@"打开成功");

}else{

NSLog(@"打开失败%d",result);

}

}

-(void)closeDB{

int result =sqlite3_close(DB);

if (result==SQLITE_OK) {

NSLog(@"关闭成功");

}else{

NSLog(@"失败");

NSLog(@"%d",result);

}

}

-(void)insertStudemtWithName:(NSString *)name age:(NSInteger)age{

//1.准备sql语句

NSString *sqlstring =@"insert into person(name,age)values(?,?)";

//2.创建伴随指针(用来绑定参数,和获取数据)

sqlite3_stmt *stmt =NULL;

//3.预执行

int result =sqlite3_prepare(DB, sqlstring.UTF8String, -1, &stmt, NULL);

if (result==SQLITE_OK) {

//4.参数绑定

//第二个参数代表的是第几个字段,从1开始计数

sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);

sqlite3_bind_int64(stmt, 2, age);

//5.执行

if (sqlite3_step(stmt)==SQLITE_DONE) {

NSLog(@"插入成功");

}else{

NSLog(@"插入失败%d",result);

}

}else{

NSLog(@"语句错误%d",result);

}

//关闭伴随指针

sqlite3_finalize(stmt);

}

//该(updata)

-(void)updataWithage:(NSInteger)age{

//1.准备 sql语句

NSString *sqlstring =@"updata Student set age =20 where age =?";

//2.创建伴随指针

sqlite3_stmt *stmt =NULL;

//3.预执行

int result =sqlite3_prepare(DB, sqlstring.UTF8String, -1, &stmt, NULL);

if (result ==SQLITE_OK) {

sqlite3_bind_int64(stmt, 1, age);

if (sqlite3_step(stmt)==SQLITE_DONE) {

NSLog(@"跟新成功");

}else{

NSLog(@"跟新失败");

}

}else{

NSLog(@"语法错误");

}

sqlite3_finalize(stmt);

}

-(void)selectWithName:(NSString *)name{

//1.准备sql语句

NSString *sqlstring =@"select name,age from Student where name =?";

//伴随指针

sqlite3_stmt *stmt =NULL;

int result =sqlite3_prepare(DB, sqlstring.UTF8String, -1, &stmt, NULL);

if (result ==SQLITE_OK) {

sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);

while (sqlite3_step(stmt)==SQLITE_ROW) {

NSString *readname =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];

NSInteger readage =sqlite3_column_int64(stmt, 1);

NSLog(@"姓名%@ 年龄%ld",readname ,readage);

}

}else{

NSLog(@"查询出错");

}

//6.关闭伴随指针

sqlite3_finalize(stmt);

}

-(void)deletepersonwithage:(NSInteger)age{

NSString *sqlstring =[NSString stringWithFormat:@"delete from person where age =%ld",age];

int result =sqlite3_exec(DB, sqlstring.UTF8String, NULL

, NULL, NULL);

if (result ==SQLITE_OK) {

NSLog(@"shanchu cehnggong");

}else{

NSLog(@"shibaishanchu");

}

}

你可能感兴趣的:(sqlite)