gpt4 book ai didi

ios - 为什么表不是在 ios 中的 sqlite 中创建的

转载 作者:行者123 更新时间:2023-11-28 22:03:11 25 4
gpt4 key购买 nike

这是我在现有数据库中添加表的代码。我的代码到达了 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;
}

最佳答案

您的实现似乎没有错误。我认为您正在打开放置在应用程序包中的 db 文件,它会根据您执行的查询进行更新。

启动应用程序后,数据库文件将被复制到设备/模拟器的文档目录中。因此,如果您在 iOS 模拟器中运行您的应用程序,请检查文档目录文件夹中的数据库。

打开查看是否更新

带有 db 文件 iOS 模拟器的文档目录的文件夹结构示例

/Users/XYZ/Library/Application Support/iPhone Simulator/7.1/Applications/B5EC1893-B38F-4AB4-AE7C-48EF685EE35F/Documents/db.sqlite

关于ios - 为什么表不是在 ios 中的 sqlite 中创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24605800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com