一般要永久保存数据肯定会选数据库,而移动设备上常用的数据库就是SQLite,创建数据库可以下载一个火狐浏览器,有一个组件叫“SQLite Manager” 可以用它来创建数据库和创建表,用法很简单,对于对数据库一点也不了解的朋友建议先看一下数据库,因为下面写代码的时候肯定会用到sql语句。
写代码的时候有几点常出现的错误,数据库启动失败,这个时候查看路径有没有错,或者是docment下已经有了数据库,因为将我们创建好的数据库拖进Xcode里时,是放进了包里,我们运行时会把包里的数据库复制到docment目录里,有些朋友第一次运行时没问题,但是发现数据库表有问题,修改后再运行就出现问题了,这是因为你的docment下已经有了一个数据库,解决办法就是进到docment目录里将把已有的库删掉,并重新运行一下就可以了。
还有在用代码插入数据,读取数据和修改数据里常出现的问题是sql语句写错了,这点细心一些就能解决掉。
这下面的代码分别是3个.m里的代码,详情请看附件
DB.m
//定义一个数据库静态变量
static sqlite3 *db = nil;
//打开数据库
+ (sqlite3 *)openDB
{
//数据库有值时直接返回此数据库
if (db != nil) {
return db;
}
//数据库.sqlite文件路径
NSString *sourceFilePath = [[NSBundle mainBundle] pathForResource:@"SQLiteDemo" ofType:@"sqlite"];
//docment文件路径
NSString *docmentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//docment文件下的数据库路径
NSString *targetFilePath = [docmentPath stringByAppendingPathComponent:@"SQLiteDemo.sqlite"];
//定义文件管理器
NSFileManager *fm = [[NSFileManager alloc] init];
//判断docment文件下有没有此数据库,没有执行if里的语句, 有就会跳过
if ([fm fileExistsAtPath:targetFilePath] == NO) {
//声明一个变量为布尔值
BOOL success = NO;
//将包里的数据库复制到docment路径下,成功复制返回值为YES,复制失败返回值为NO
success = [fm copyItemAtPath:sourceFilePath toPath:targetFilePath error:nil];
//成功复制返回open,失败返回fail, 作为提示信息
if (success == YES) {
NSLog(@"open");
} else {
NSLog(@"fail");
}
}
//将文件管理器释放掉
[fm release];
//使用open语句,将docment下的数据库打开
sqlite3_open([targetFilePath UTF8String], &db);
return db;
}
//关闭数据库
+ (void)closeDB
{
if (db != nil) {
//使用colse语句将数据库关闭
sqlite3_close(db);
}
}
PublicData.m
//声明一个静态的PublicData类的对象并赋予nil值
static PublicData *publicData = nil;
//实现单例方法
+ (PublicData *)sharedPublicData
{
if (publicData == nil) {
publicData = [[PublicData alloc] init];
}
return publicData;
}
//插入数据方法实现
+ (void)insertstuPicture:(NSData *)picture stuName:(NSString *)name stuHobby:(NSString *)hobby stuSal:(double)sal
{
//打开数据库
sqlite3 *db = [DB openDB];
//处理sql语句
sqlite3_stmt *stmt = nil;
//db数据库名,“”里为插入语句, -1为自动获取语句长度, &stmt更改, nil为空
int result = sqlite3_prepare_v2(db, "insert into student(stuPicture, stuName, stuHobby, stuSal) values (?, ?, ?, ?)", -1, &stmt, nil);
//返回值为0的话执行if里的语句
if (result == SQLITE_OK) {
//将sql语句里的第一个 ?绑定
sqlite3_bind_blob(stmt, 1, [picture bytes], [picture length], nil);
sqlite3_bind_text(stmt, 2, [name UTF8String], -1, nil);
sqlite3_bind_text(stmt, 3, [hobby UTF8String], -1, nil);
sqlite3_bind_double(stmt, 4, sal);
//执行sql语句
sqlite3_step(stmt);
}
//结束sql语句
sqlite3_finalize(stmt);
}
//修改数据方法实现
+ (void)updatestuPicture:(NSData *)picture stuName:(NSString *)name stuHobby:(NSString *)hobby stuSal:(double)sal
{
//打开数据库
sqlite3 *db = [DB openDB];
//处理sql语句
sqlite3_stmt *stmt = nil;
//db数据库名,“”里为修改语句, -1为自动获取语句长度, &stmt更改, nil为空
int result = sqlite3_prepare_v2(db, "update student set stuPicture = ?, stuName = ?, stuHobby = ?, stuSal = ?)", -1, &stmt, nil);
//返回值为0的话执行if里的语句
if (result == SQLITE_OK) {
//将sql语句里的第一个 ?绑定
sqlite3_bind_blob(stmt, 1, [picture bytes], [picture length], nil);
sqlite3_bind_text(stmt, 2, [name UTF8String], -1, nil);
sqlite3_bind_text(stmt, 3, [hobby UTF8String], -1, nil);
sqlite3_bind_double(stmt, 4, sal);
//执行sql语句
sqlite3_step(stmt);
}
//结束sql语句
sqlite3_finalize(stmt);
}
//查询全部数据方法实现
+ (NSMutableArray *)findStuAll
{
//打开数据库
sqlite3 *db = [DB openDB];
//处理sql语句
sqlite3_stmt *stmt = nil;
//db数据库名,“”里为查询语句, -1为自动获取语句长度, &stmt更改, nil为空
int success = sqlite3_prepare_v2(db, "select * from student", -1, &stmt, nil);
//返回值为0的话执行if里的语句
if (success == SQLITE_OK) {
//数组的声明
publicData.stuArray = [NSMutableArray array];
//执行sql语句 并且循环
while (sqlite3_step(stmt) == SQLITE_ROW) {
//定义几个变量来接受数据库里的数据,stmt后的0为数据库默认的第一个字段
int id = sqlite3_column_int(stmt, 0);
const void *picture = sqlite3_column_blob(stmt, 1);
int lon = sqlite3_column_bytes(stmt, 1);
const unsigned char *name = sqlite3_column_text(stmt, 2);
const unsigned char *hobby = sqlite3_column_text(stmt, 3);
double sal = sqlite3_column_double(stmt, 4);
//将接受的数据在转化成NSString类型并放在contactsData对象里
PublicData *contactsData = [[PublicData alloc] initWithStuID:id stuPicture:[UIImage p_w_picpathWithData:[NSData dataWithBytes:picture length:lon]] stuName:[NSString stringWithFormat:[NSString stringWithCString:(const char*)name encoding:NSUTF8StringEncoding]] stuHobby:[NSString stringWithFormat:[NSString stringWithCString:(const char*)hobby encoding:NSUTF8StringEncoding]] stuSal:sal];
//将contactsData对象放进数据里
[publicData.stuArray addObject:contactsData];
[contactsData release];
}
//结束sql语句
sqlite3_finalize(stmt);
//返回数组
return publicData.stuArray;
}
//不执行if里的语句的时候 执行结束语句
sqlite3_finalize(stmt);
//返回空
return nil;
}
//删除全部数据方法实现
+ (void)deleteAll
{
//打开数据库
sqlite3 *db = [DB openDB];
//处理sql语句
sqlite3_stmt *stmt = nil;
//db数据库名,“”里为删除语句, -1为自动获取语句长度, &stmt更改, nil为空
int result = sqlite3_prepare_v2(db, "delete from student", -1, &stmt, nil);
//返回值为0的话执行if里的语句
if (result == SQLITE_OK) {
//执行sql语句
sqlite3_step(stmt);
}
//结束sql语句
sqlite3_finalize(stmt);
}
//数据初始化方法实现
- (id)initWithStuID:(int)id stuPicture:(UIImage *)picture stuName:(NSString *)name stuHobby:(NSString *)hobby stuSal:(double)sal
{
self = [super init];
if(self) {
self.stuID = id;
self.stuPicture = picture;
self.stuName = name;
self.stuHobby = hobby;
self.stuSal = sal;
}
return self;
}
AppDelegate.m
//声明单例
PublicData *publicData = [PublicData sharedPublicData];
//查找数据库里的数据
publicData.stuArray = [PublicData findStuAll];
//打印查找出来的数据的第一个元素的姓名
NSLog(@"name = %@", [[publicData.stuArray objectAtIndex:0] stuName]);
附件是SQLite的整个Demo文件,有问题的朋友可以下来看一看
转载于:https://blog.51cto.com/taekgil/961417