给你个例子供你参考
<span style="font-size:14px">#import "StudentDataBase.h"#import "DB.h"
#import "StudentModel.h"
@implementation StudentDataBase
//查找所有数据
+(NSArray *)findAll{
sqlite3 * db = [DB DBOpen] //调用DB open方法 获取数据库指针
sqlite3_stmt * stmt//镜像 copy出来的镜像 不影响sqlite里的内容
int result = sqlite3_prepare_v2(db, "select * from student", -1, &stmt, Nil)
//-1是不限制字符串的长度 &stmt是往stmt里面赋值 Const char **pzTail是设置前面哪个是不用的
NSMutableArray * peopels = [NSMutableArray array] //制作容器 要装数据
//判断结果 0进判断 result==0表示正确
if (SQLITE_OK == result) {
//判断是否有下一行
while (sqlite3_step(stmt) == SQLITE_ROW) {
//int iCol 是第几个
int stuId = sqlite3_column_int(stmt, 0) //取一行中的一个 int是值的类型
const unsigned charchar * stuName = sqlite3_column_text(stmt, 1) //取一行中的一个 text是值的类型
const unsigned charchar * stuSex = sqlite3_column_text(stmt, 2) //取一行中的一个 text是值的类型
float stuScore = sqlite3_column_double(stmt, 3) //取一行中的一个 double是值的类型
//转换
NSString * currentName = [NSString stringWithUTF8String:(const charchar *)stuName]
NSString * currentSex = [NSString stringWithUTF8String:(const charchar *)stuSex]
StudentModel * student = [StudentModel studentModelWithId:stuId name:currentName sex:currentSex score:stuScore]
//将model中内容 添加到 之前的容器 数组
[peopels addObject:student]
}
sqlite3_finalize(stmt)//关闭镜像
return peopels//将这个装有数据的数组 返回
}else{
sqlite3_finalize(stmt)
return [NSArray array] //如果没进入if 返回一个空数组
}
}
//查找单个数据
+(StudentModel *)findPeopleWith:(NSInteger )stuId{
sqlite3 * db = [DB DBOpen]
sqlite3_stmt * stmt
int result = sqlite3_prepare_v2(db, "select stuName,stuId,stuSex,stuScore from student where stuId=?", -1, &stmt, Nil)
sqlite3_bind_int(stmt, 1, stuId)// 第一个int是第几个问好? 第二个int 是绑定的内容 绑定问好
if (SQLITE_OK == result) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned charchar * stuName = sqlite3_column_text(stmt, 0)
int stuId = sqlite3_column_int(stmt, 1)
const unsigned charchar * stuSex = sqlite3_column_text(stmt, 2)
float stuScore = sqlite3_column_double(stmt, 3)
NSString * currentName = [NSString stringWithUTF8String:(const charchar *)stuName]
NSString * currentSex = [NSString stringWithUTF8String:(const charchar *)stuSex]
StudentModel * student = [StudentModel studentModelWithId:stuId name:currentName sex:currentSex score:stuScore]
sqlite3_finalize(stmt)
NSLog(@"000")
return student
}else{
NSLog(@"11111")
return nil
}
}else{
NSLog(@"222222")
sqlite3_finalize(stmt)
return nil
}
}
//插入数据
+(void)insertIntoModel:(StudentModel *)model{
sqlite3 * db = [DB DBOpen]
sqlite3_stmt * stmt
int result = sqlite3_prepare_v2(db, "insert into student values(?,?,?,?)", -1, &stmt, Nil)
sqlite3_bind_int(stmt, 1, model.stuId)
sqlite3_bind_text(stmt, 2, [model.stuName UTF8String], -1, Nil)
sqlite3_bind_text(stmt, 3, [model.stuSex UTF8String], -1, nil)
sqlite3_bind_double(stmt, 4, model.stuScore)
if (SQLITE_OK == result) {
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"添加成功")
sqlite3_finalize(stmt)
return
}else{
NSLog(@"添加失败")
sqlite3_finalize(stmt)
return
}
}else{
NSLog(@"111")
sqlite3_finalize(stmt)
return
}
}
//更新数据
+(void)updateStuName:(NSString *)name stuSex:(NSString *)sex stuScore:(CGFloat)score stuId:(NSInteger)stuid{
sqlite3 * db = [DB DBOpen]
sqlite3_stmt * stmt
int result = sqlite3_prepare_v2(db, "update student set stuName=?,stuSex=?,stuScore=? where stuId = ?", -1, &stmt, Nil)
sqlite3_bind_int(stmt, 4, stuid)
sqlite3_bind_text(stmt, 1, [name UTF8String], -1, Nil)
sqlite3_bind_text(stmt, 2, [sex UTF8String], -1, nil)
sqlite3_bind_double(stmt, 3, score)
if (SQLITE_OK == result) {
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"更新成功")
sqlite3_finalize(stmt)
return
}else{
NSLog(@"更新失败")
sqlite3_finalize(stmt)
}
}else{
NSLog(@"shibai")
sqlite3_finalize(stmt)
}
}
//删除数据
+(void)deleteModelStuId:(NSInteger)stuid{
sqlite3 * db = [DB DBOpen]
sqlite3_stmt * stmt
int relust = sqlite3_prepare_v2(db, "delete from student where stuid= ?", -1, &stmt, nil)
sqlite3_bind_int(stmt, 1, stuid)
if (SQLITE_OK == relust) {
//step 截段
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"删除成功")
sqlite3_finalize(stmt)
return
}else{
NSLog(@"删除失败")
sqlite3_finalize(stmt)
return
}
}else{
NSLog(@"删除失败11")
sqlite3_finalize(stmt)
return
}
}
@end
</span>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)