【发布时间】:2014-08-27 15:39:48
【问题描述】:
这是我在现有数据库中添加表的代码。我的代码到达sqlite3_prepare_v2 的成功回调并给出日志table created,但是当我使用终端打开我的sqlite 数据库时,我看到没有创建表。我的数据库表名是 fz1.sqlite 由 pragma 声明的:#define kDBName @"fz1.sqlite"
-(sqlite3 *)mydb:(NSString *)query{
static sqlite3 *database;
static sqlite3_stmt *enableForeignKey;
if (database == NULL) {
sqlite3 *newDBconnection;
// first get the path for the document directory of the application
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dbpath = [paths objectAtIndex:0];
NSString *path = [dbpath stringByAppendingPathComponent:kDBName];
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
database = newDBconnection;
if (sqlite3_prepare_v2(database,[query UTF8String], -1, &enableForeignKey, NULL) != SQLITE_OK) {
NSLog(@"ERROR IN PRAGMA!");
}
else{
sqlite3_exec(database, [query UTF8String], NULL, NULL, NULL);
NSLog(@"faiz's table created");
}
sqlite3_finalize(enableForeignKey);
} else {
NSLog(@"Error in opening database :(");
database = NULL;
}
}
return database;
}
【问题讨论】:
-
sqlite3 如果你问它会告诉你出了什么问题。开始这样做,您将更接近解决您的问题。
-
打印出您打开数据库的位置。对于 SQLite 新手来说,您没有在您认为的位置打开文件的几率高于 50:50。
标签: ios objective-c database sqlite