0

I created a database(empty) with in terminal from the /usr/bin directory using:

touch /Users/myusername/Desktop/testopeningbook4.db

It showed on the desktop. Then imported it into my Xcode OSX project(Mac app i made to do databasing) with add files and selected the option copy files into the project and testopeningbook4.db shows in my project files in xcode(in project navigator) and is in my project folder for the app(in finder). It's no longer on the desktop I think I deleted that copy.

I opened the database in an objective-c code file/class and I've stored a few hundred thousand entries into it now, I believe(confirmed there is a lot of stuff in it now when I query with select but I havent counted and I am only running my app to test this from xcode as the results of select lack a gui now and go to the console with nslog).

See what I wanted to do was see how big the file got. But the one in the xcode project folder says its 0 bytes. I open it like this:

NSString *dbPath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"testopeningbook4.db"];

sqlite3 *database;
if(!(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK))
{
    NSLog(@"An error has occurred did not open database.");
    return false;
}
else
{
    NSLog(@"opened database");
}

Ideally what I want to do is copy the testopeningbook4.db file and import it into another xcode project for iOS this time for an iPhone/iPad app. But since the file in the xcode project folder is 0 bytes that must not be the right thing to copy. I found two links and tried both these ideas(i've closed the pages since and don't have the links):

one said drag the database from the Xcode project navigator and you could drag it out a copy onto the desktop. I did that. It's like 440 kilobytes which seems to small since testopeningbook3(now deleted) was about that size and I used a much smaller source file for it's data, but bigger than 0 kilobytes in the xcode project. I went to /usr/bin and typed

sqlite3 /databasepathtodesktop/testopeningbook4.db

Next in the sqlite3 command prompt typed

.tables 

and it didn't list anthing leading me to think its empty and not the copy that is working in my program.

The second idea I found from a link was to archive the project then get your database out of the archive. So I made a .xcarchive file and right click and do show package contents. In the contents I get to my app and again right click and do show package contents, then I get to the resources folder and my database is in there but again it's 0 byteslike the one in my project directory.

How would I get back the testopeningbook4.db with the data and megabytes all apart of it to import into another project or move it between computers.

I'm using Xcode 5.1.1 Thanks Mike

Edit: Modified the title to be more descriptive of the actual problem

8
  • I suspect that your Mac app modified the file somewhere else (check the path of your app and its package content, or check the path in the code). DB file in your Xcode project is not modified. Or simply search your file system for "testopeningbook4.db" Commented Apr 28, 2014 at 16:39
  • when i search the mac for testopeningbook4.db and do show in finder there is the 0 byte copy in my project folder in the file system and there is the 484 kilobyte copy that i got by dragging it from the Xcode project navigator onto the desktop that has no tables(i explained in question how i checked that) Commented Apr 28, 2014 at 16:47
  • i notice in terminal when i type sqlite3 /nonsensepath/testopeningbook4.db It opens the same way and i type .tables i get the same result. could the one i copied from navigator of Xcode onto desktop by dragging be it but i'm not opening it right in terminal? Commented Apr 28, 2014 at 16:57
  • You must, in your "first startup" code, copy the SQL file from the bundle to read/write storage. You can't modify the bundle copy. Commented Apr 28, 2014 at 16:58
  • not sure what that means. i opened it with the code i gave in question and stored data in it because i've been doing selects to view the data in the app i run from xcode Commented Apr 28, 2014 at 17:01

1 Answer 1

2

I had several problems working with sqlite databases on the bundle. Try to copy it to the documents directory and give it a try.

NSString *nameDB = @"myDatabase.sqlite"
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:nameDB];
[[NSFileManager defaultManager] createFileAtPath:path contents:dataDB attributes:nil];

Hope it works

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

5 Comments

for this @"myDatabase.sqlite" i use @"testopeningbook4.sqlite" or i include the .db in myDatabase name then append .sqlite? Am going to try this now just wanted to clarify
also i get use of undeclared identifier dataDB
for nameDB use your own database name and dataDB is your database as a file turned into a NSData. Your can do this with [NSData dataWithContentsOfFile:(NSString *)] and giving the location of your sqlite in the bundle
I'll look into this. it seems to want an NSData type there for dataDB, thanks
This worked. its 374 megabytes, bigger than I expected. I was able to repeat the steps of opening the file I copied with this code to documents folder in terminal and typing .tables and this time there were a lot of tables not none. thanks

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.