2

Hi I'm trying to create an sqlite database in ios. I use the method sqlite3_open with the params needed but i always get the error 14 (SQLITE_CANTOPEN 14 /* Unable to open the database file */).

It does't work even with its simplest declaration

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");
}

Any ideas what's happening?

Thanks.

2
  • stackoverflow.com/questions/4486254/sqlite-database-error-14 Commented Feb 27, 2013 at 13:01
  • Probably not helpful in this case, but in general, when you get a bad return code do NSLog(@"SQLite errmsg = %s", sqlite3_errmsg());, to get the text error message. Commented Sep 6, 2013 at 18:10

3 Answers 3

4

You can't get write access to a sqlite database in your application bundle. Instead, copy the database in the bundle into the "file" system, in an appropriate place (given iCloud concerns), and then open it there. Have your app look for the database in the file system first and only copy it if it's not there.

Sign up to request clarification or add additional context in comments.

1 Comment

They should all be this easy!
1

use document directory to store the database file and then create sqlite.... it works

Comments

0

I was on this issue today with my own app in .net Maui but I believe it applies to this issue as well.

I, too, was trying to create a sqlite db using params in the documents folder.

However, it wasn't until I removed the params that the db actually created.

I recognize the asker's question is related to xcode, but I believe .net Maui's sqlite is the same.

        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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.