How i do execute sqlite in iphone?

I have the following problem. An application that has a sqlite database. I run the application in xcode, with the simulator. I have the following code to know where I have to put the database.


sqlite3 *bd;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"bd.sqlite"];
NSLog(@"bd route is ---- %@",dbPath);

So I get the path, and copy the sqlite database, so the application works perfectly in xcode. The problem is when I connect my iphone and try to run the application. Because the previous code gives me a route on the iphone, and I try to put the sqlite database there but it does not run, the application is blocked because it cannot find the database, even if I put it there. I don't know if what I am doing is correct or there is another way to do it. I'm going crazy looking for possible solutions on the internet, but none works for me and I've been standing for 3 months trying to run the application on the iphone. Please, some help or advice, I would appreciate it. Many thanks.

Answered by AncientCoder in 385392022

1. In Xcode's Project Navigator (View > Navigators > Show Project Navigator) at the left of screen, click on Info.plist


2. Press the + button that is to the right of the first line "Information Property List" and then, in the box that appears, type "Application supports iTunes file sharing". The Type property should be Boolean and the Value "YES"


3. Compile the app onto your device.


4. With the device connected to your Mac, open iTunes and click the small device icon near the top left of screen. You should then see a list of options on the left of screen: click File Sharing.


5. You should then see your app listed, so click on it.


6. Scroll to the bottom of the panel at right to show the "Add" button and then click it.


7. Select your database file from your Mac's storage and press the blue Add button, then press the blue Done button.


8. Your database file will (should) now be in the correct location for use by your app.


NOTE that you can also Save your database file from your device, via iTunes, to your Mac, and then examine the database on your Mac if you have appropriate SQLite reader software. I use RazorSQL, but there are free Mac versions such as "DB Browser for SQLite". If you are doing a lot of database work, then getting a Mac-based browser is well worth it.


Good luck and best wishes, Michaela

Is the data static?


How big is the data/how many records~fields?


Is there a hard requirement to use an sqlite bd or can the data be in another form?

Most of my applications use Sqlite and I sometimes have the sort of problem that you're encountering. What I do is:


  1. Set "Application supports iTunes file sharing" YES in the App's Plist (and recompile).
  2. In iTunes (or Finder if using Catalina) copy the SQLite database into the iPhone location exposed by iTunes or Finder.
  3. Check that the App runs correctly. If not, NSLog or print the SQL return code to the console and proceed according to the error.
  4. When all is working, turn off file sharing (unless you're comfortable having user access to the database file).
  5. If this isn't a publically available app, then this way you don't need the copying code in your app, unless you need to "refresh" the database file from time to time and can't use the iTunes method.


I have RazorSQL on my Macs so that I can create/edit schemas and view/update records in the database, after transferring them via iTunes from my device(s) or Simulator.


Best wishes,


Michaela

thank you Michaela, but

but, as I said I am a newbie, and how do I do the first step ????

Set "Application supports iTunes file sharing" YES in the App's Plist (and recompile).


In Xcode compilator or in iTunes??


Forgive my basic question ... but, as I said, I am starting and the truth is that it is hard for me to solve this big problem.

Accepted Answer

1. In Xcode's Project Navigator (View > Navigators > Show Project Navigator) at the left of screen, click on Info.plist


2. Press the + button that is to the right of the first line "Information Property List" and then, in the box that appears, type "Application supports iTunes file sharing". The Type property should be Boolean and the Value "YES"


3. Compile the app onto your device.


4. With the device connected to your Mac, open iTunes and click the small device icon near the top left of screen. You should then see a list of options on the left of screen: click File Sharing.


5. You should then see your app listed, so click on it.


6. Scroll to the bottom of the panel at right to show the "Add" button and then click it.


7. Select your database file from your Mac's storage and press the blue Add button, then press the blue Done button.


8. Your database file will (should) now be in the correct location for use by your app.


NOTE that you can also Save your database file from your device, via iTunes, to your Mac, and then examine the database on your Mac if you have appropriate SQLite reader software. I use RazorSQL, but there are free Mac versions such as "DB Browser for SQLite". If you are doing a lot of database work, then getting a Mac-based browser is well worth it.


Good luck and best wishes, Michaela

If the database is static and read-only, I would just include the .sqlite file in the Xcode project. Make sure it's included in the app target so it gets copied to the app bundle. Google "copy file from app bundle" for some examples of how to access files in the bundle.


If it's not read-only, personally I'd still include the initial state of the DB in the app bundle the same way, then copy it to a writable location (probably Application Support) on first run.

This is probably what OP is looking for, unless you want the user to be able to update the sqlite file afterwards or something. It should be added in the copy resources step for the app target, then you can use Bundle (method's named something like url for resource with name: extension:?? Can't quite remember off top of my head) to get the path to the file.

This is a valid approach, except that jgm has set his app up to use the Documents directory ( an acceptable location for app data) and he's a newbie. His problem appears to be that the copy to Documents is unsuccessful on the physical device, or SQLite objects to opening the database there. Without seeing the copy code (where and when in the app startup) and the SQLite error it's hard to tell what's going wrong. Turning on iTunes file sharing will allow him to confirm that a) the database is in Documents (i.e if the copy has indeed worked) and b) that the contents are not corrupted (provided he re-exports and examines, or the file size is correct). Adding the database file to Documents via iTunes will ensure that it's there and valid.


In the long term, it would still be advisable for him to sort out the copying from bundle issue: necessary if the app is for general distribution. Personally, I don't like having production databases in the bundle, only as the initial state (usually just for the schema).

It's a long time since I've used Objective C with SQLite, but I have a vague recollection of a similar problem to yours. Here's my database opning code from an old app - plus the copy to Documents code. If you use it, you might want to change the sqlite3_open to READONLY not READWRITE. Note the conversion of the path to UTF-8. This part of the old app still compiles and works under iOS 13 and Xcode 11: I still use the app regularly, but have added Swift modules over the years and recently implemented some SwiftUI views.


[self installDatabaseIfNeeded];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"running.db"];

const char *dbpath = [path UTF8String]; // Convert NSString to UTF-8

//NSLog(@"Path = %s",dbpath);

retcode = sqlite3_open_v2(dbpath, &runningDB, SQLITE_OPEN_READWRITE, NULL);

...........


- (void) installDatabaseIfNeeded {


// Get the documents database path


NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSString *documentsDatabasePath = [documentsFolder stringByAppendingPathComponent:@"running.db"];

//NSLog(@"Docuement path = %@",documentsDatabasePath);

NSFileManager *fileManager = [NSFileManager defaultManager];


if (![fileManager fileExistsAtPath:documentsDatabasePath]) {

// if the database doesn't exist in documents, look for it in the bundle and copy it if found

NSString *bundleDatabasePath = [[NSBundle mainBundle] pathForResource:@"running" ofType:@"db"];

if (bundleDatabasePath) {

NSError *error;

if (![fileManager copyItemAtPath:bundleDatabasePath toPath:documentsDatabasePath error:&error]) {

NSLog(@"copyItemAtPath failed: %@", error);

}

}

}

}


Cheers, Michaela

My understanding is that the OP is running that code snippet, as posted, to get the simulator to print out the path to its working directory. Then he manually copies his .sqlite file to that location using the Finder, after which everything works in the simulator. Obviously that's not going to work on the device. There is no code doing any copying.


I think there is no choice but to learn how to include a file in the app bundle then read from or copy that file in code.

THANK YOU !!!!!!!!!!!!!!!!!!!!

THANK YOU !!!!!!!!!!!!!!!!!!

THANK YOU !!!!!!!!!!!!!!!!!!!

It's works !!!.. THANK YOU, THANK YOU, Thank you so much for everything

thanks for explaining it point by point

For novice or beginning people it has been great to solve my problem as you explained, point by point.

I have done what you indicated and the application works perfectly on the iphone. I had been trying for 3 months and I was almost about to erase everything and forget about the application


Now I have the problem in the xcode that I can't find the objects to insert, the texts, images, buttons ... etc, they don't appear to me ... what a madness !!
Hello AncientCoder.
I have the same problem , but now i don't find iTunes...help !!
How i do execute sqlite in iphone?
 
 
Q