#import<sqlite3.h>
第二步加入libsqlite3.0.dylib库文件
第三步创建数据库对象
staticsqlite3*db =nil;
第四步打开数据库
- (void)openDB
{
if(db!=nil) {
NSLog(@"数据库已经打开");
return;
}
NSArray*array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*path = [arraylastObject];
NSString*sqlitePath = [pathstringByAppendingPathComponent:@"student.sqlite"];
@"sqlitePath = %@",sqlitePath);
//打开数据库
intresult =sqlite3_open(sqlitePath.UTF8String,&db);
if(result ==sqlITE_OK) {
数据库打开成功");
建数据库表
NSString*createsql =@"CREATE TABLE IF NOT EXISTS lanou23(number INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,gender TEXT,age INTEGER)";
sqlite3_exec(db,createsql.NULL,117)">nil);
创建表成功");
}else{
创建表失败");
}
}数据库打开失败");
}
}
第五步插入数据void)insertStudent:(Person*)person
{
创建插入语句
NSString*insertsql = [NSStringstringWithFormat:@"INSERT INTO lanou23(name,gender,age) values('%@','%@','%ld')",person.name,164)">gender,164)">age];
sql.插入成功插入失败");
}
}
第六步修改数据void)updateStudent:(Person*)person number:(NSInteger)number
{
NSString*updatasql = [@"UPDATE lanou23 SET name = '%@',gender = '%@',age = '%ld' WHERE number = '%ld'",164)">age,number];
sql.修改成功修改失败第七步删除void)deleteStudent:(NSString*deletesql = [@"DELETE FROM lanou23 WHERE number = '%ld'",deletesql.删除成功else
{
删除失败第八步查询所有数据
- (NSMutableArray*)selectAllStudent
{
NSMutableArray*array = [NSMutableArrayarray];
NSString*selectsql =@"SELECT * FROM lanou23";
查询一个或几个数据
NSString*sql = [@"SELECT * FROM lanou23 WHERE name = %@",大水杯"];
声明数据库跟随指针,作用是跟踪表里的每一行
sqlite3_stmt*stmt =nil;
查询之前准备工作将数据库对象db,sql语句关联起来
//1.参数1.数据库对象
//2.2.sql语句
//3.3.sql语句长度为不限长度
//4.4.数据库跟随指针
sqlite3_prepare_v2(sql.1,&stmt,19)">查询成功");
循环遍历每条数据
while(sqlite3_step(stmt) ==sqlITE_ROW) {
取值
intnumber =sqlite3_column_int(stmt,0);
constunsignedchar* name =sqlite3_column_text(stmt,255)">1);
char* gender =2);
intage =3);
NSString*nameStr = [stringWithUTF8String:(char*)name];
NSString*genderStr = [char*)gender];
NSIntegernumberInteger = number;
NSIntegerageInteger = age;
Person*person = [[Personalloc]init];
person.name= nameStr;
person.gender= genderStr;
person.age= ageInteger;
person.number= numberInteger;
[arrayaddObject:person];
}
}查询失败");
}
returnarray;
第三步创建数据库对象
staticsqlite3*db =nil;
第四步打开数据库
- (void)openDB
{
if(db!=nil) {
NSLog(@"数据库已经打开");
return;
}
NSArray*array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*path = [arraylastObject];
NSString*sqlitePath = [pathstringByAppendingPathComponent:@"student.sqlite"];
@"sqlitePath = %@",sqlitePath);
//打开数据库
intresult =sqlite3_open(sqlitePath.UTF8String,&db);
if(result ==sqlITE_OK) {
数据库打开成功");
建数据库表
NSString*createsql =@"CREATE TABLE IF NOT EXISTS lanou23(number INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,gender TEXT,age INTEGER)";
sqlite3_exec(db,createsql.NULL,117)">nil);
创建表成功");
}else{
创建表失败");
}
}数据库打开失败");
}
}
第五步插入数据void)insertStudent:(Person*)person
{
创建插入语句
NSString*insertsql = [NSStringstringWithFormat:@"INSERT INTO lanou23(name,gender,age) values('%@','%@','%ld')",person.name,164)">gender,164)">age];
sql.插入成功插入失败");
}
}
第六步修改数据void)updateStudent:(Person*)person number:(NSInteger)number
{
NSString*updatasql = [@"UPDATE lanou23 SET name = '%@',gender = '%@',age = '%ld' WHERE number = '%ld'",164)">age,number];
sql.修改成功修改失败第七步删除void)deleteStudent:(NSString*deletesql = [@"DELETE FROM lanou23 WHERE number = '%ld'",deletesql.删除成功else
{
删除失败第八步查询所有数据
- (NSMutableArray*)selectAllStudent
{
NSMutableArray*array = [NSMutableArrayarray];
NSString*selectsql =@"SELECT * FROM lanou23";
查询一个或几个数据
NSString*sql = [@"SELECT * FROM lanou23 WHERE name = %@",大水杯"];
声明数据库跟随指针,作用是跟踪表里的每一行
sqlite3_stmt*stmt =nil;
查询之前准备工作将数据库对象db,sql语句关联起来
//1.参数1.数据库对象
//2.2.sql语句
//3.3.sql语句长度为不限长度
//4.4.数据库跟随指针
sqlite3_prepare_v2(sql.1,&stmt,19)">查询成功");
循环遍历每条数据
while(sqlite3_step(stmt) ==sqlITE_ROW) {
取值
intnumber =sqlite3_column_int(stmt,0);
constunsignedchar* name =sqlite3_column_text(stmt,255)">1);
char* gender =2);
intage =3);
NSString*nameStr = [stringWithUTF8String:(char*)name];
NSString*genderStr = [char*)gender];
NSIntegernumberInteger = number;
NSIntegerageInteger = age;
Person*person = [[Personalloc]init];
person.name= nameStr;
person.gender= genderStr;
person.age= ageInteger;
person.number= numberInteger;
[arrayaddObject:person];
}
}查询失败");
}
returnarray;
}
原文链接:https://www.f2er.com/sqlite/199817.html