在设备上无法将数据插入SQLite(使用FMDB)吗? [英] Inserting a data into SQLite (using FMDB) doesn't work on device?

查看:54
本文介绍了在设备上无法将数据插入SQLite(使用FMDB)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这里的新手.在设备上运行时,我无法将任何数据插入SQLite数据库.我正在使用下面的代码来连接SQLite.它可以在我的模拟器中完美运行,但不能在我的iPad设备上运行.有人可以帮忙吗?谢谢你.

I am a newbie here. I am not able to insert any data into my SQLite database when running on my device. I am using the below piece of code to connect SQLite. It works perfect in my simulator but doesn't work on my iPad device. Can anyone help please? Thank you.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DADatabase1.sqlite"];
NSLog(@"%@", writableDBPath);

FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath];

/*if ([fileManager fileExistsAtPath:writableDBPath] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"DADatabase1.sqlite" ofType:@"sqlite"];
    [fileManager copyItemAtPath:resourcePath toPath:writableDBPath error:nil];
}*/
[db open];
[db traceExecution];
[db beginTransaction];
BOOL success = [db executeUpdate:[NSString stringWithFormat:@"insert into PatientDetails (patAge) values (?)",patAge.text]];
if(!success)
{
    NSLog(@"Query not success!");
}
else {
    NSLog(@"Query success!");
}
//NSString *query = [NSString stringWithFormat:];
[db commit];
[db close];

推荐答案

您是否将DatabaseTables放在了Projects资源文件夹中?您必须将数据库文件复制到Apps Document文件夹.

Did you put your Database with your Tables in your Projects resources folder? You have to copy your database file to your Apps Document folder.

+ (BOOL)initDB {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"sqlite.db"]; 

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
    NSString *bundle =  [[ NSBundle mainBundle] pathForResource:@"sqlite" ofType:@"db"];
    [fileManager copyItemAtPath:bundle toPath:path error:nil];
    return YES;
}
    return NO;
}

这篇关于在设备上无法将数据插入SQLite(使用FMDB)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆