【发布时间】:2013-01-18 21:13:58
【问题描述】:
可能重复:
How should I specify the path for an sqlite db in an iPhone project?
我想知道为什么我只能在 iphone 上读取我的 Sqlite 数据库而不能写任何东西... 在模拟器上一切正常..
有没有设置权限之类的配置?
PS:当我更新或插入 sqlite(从设备)时没有错误。
这是我的代码(打开 sqlite):
- (id)init {
if ((self = [super init])) {
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"test"
ofType:@"sqlite"];
if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
}
}
return self;
}
@end
还有我的“更新”代码:
-(void)addCoin:(int)score
{
NSString *query = @"select coin_user from user where id_user = '1'";
//query = @"SELECT id_soal FROM soal where jawaban_soal = 'romin'";
sqlite3_stmt *statement;
int total;
if (sqlite3_prepare(_database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
total = sqlite3_column_int(statement, 0);
}
sqlite3_finalize(statement);
}
query = [NSString stringWithFormat:@"update user set coin_user = '%i' where id_user = '1'",total + score];
if (sqlite3_prepare(_database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
sqlite3_step(statement);
sqlite3_finalize(statement);
}
}
【问题讨论】:
-
您不能从主包中写入 sqlite 文件,因为它们是只读的。您必须将 sqlite 文件从主包复制到 Documents 目录,然后使用复制到文档目录中的 sqlite 文件。希望这个帮助..
-
这个链接可以帮助你复制gist.github.com/1071312
标签: iphone objective-c xcode sqlite