无法在ios上打开sqlite db

问题描述 投票:0回答:3

嗨,我正在尝试在 ios 中创建一个 sqlite 数据库。 我使用带有所需参数的 sqlite3_open 方法,但总是收到错误 14 (SQLITE_CANTOPEN 14 /* 无法打开数据库文件 */)。

即使是最简单的声明也不起作用

NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"data.sqlite3"];

BOOL success = [fileMgr fileExistsAtPath:dbPath];

sqlite3 *db;

if(!success)
{
    NSLog(@"Cannot locate database file '%@'.", dbPath);
}

if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
    NSLog(@"An error has occured.");
}
else{
    NSLog(@"Ok");
}

知道发生了什么吗?

谢谢。

iphone ios xcode sqlite
3个回答
4
投票

您无法获得对应用程序包中的 sqlite 数据库的写访问权限。相反,将捆绑包中的数据库复制到“文件”系统中的适当位置(考虑到 iCloud 问题),然后在那里打开它。让您的应用首先在文件系统中查找数据库,如果不存在则仅复制它。


1
投票

使用文档目录存储数据库文件,然后创建sqlite......它可以工作


0
投票

我今天在 .net Maui 中使用自己的应用程序解决了这个问题,但我相信它也适用于这个问题。

我也尝试使用文档文件夹中的参数创建一个 sqlite 数据库。

但是,直到我删除了数据库实际创建的参数。

我知道提问者的问题与 xcode 有关,但我相信 .net Maui 的 sqlite 是相同的。

        string appFolder = FileSystem.Current.AppDataDirectory;
        string fileNameMod = Path.Combine(appFolder,fileName);

        if (!fileNameMod.Contains(".db"))
        {
            fileNameMod += ".db";
        }

        using (SQLiteConnection con = new SQLiteConnection(fileNameMod)) // note it's only the file name, no params
        {
            // con.open does not exist, connection is already open here
© www.soinside.com 2019 - 2024. All rights reserved.