Example of how to install existing sqlite database into app

You must Login before you can answer or comment on any questions.

I am so confused.

Around the internet there are 100 different document with a 100 different methods of getting a database to install and run in your app.

All I really want is the one sure fire way of getting a pre-exsiting sqlite database installed into the app,so the database is persistent (not killed every time I build).

I have looked at the kitchen sink, and the persistence example, both which use sql statements to build the table before working with the database.

Can someone please help me out? I have wasted half a day looking/reading/watching all I can on the internet to only be left with not being sure of what I really need to do to get a datbase working in my app.

My database is located in Resources/database/efdata.db and efdata.sqlite . I don't care which file is used, I was watching youTube and someone said appcelerator doesn't read sqlite files, so I renamed it.

Based on (http://developer.appcelerator.com/question/9161/how-to-test-persistent-storage) I think that I need to install the database and then open it from the app directory.

var efSonar = Ti.Database.install('/database/efdata.db', 'efdatav01');      
efSonar.close();
 
var separator = Titanium.Filesystem.getSeparator();
var dbFile = Titanium.Filesystem.getFile(Titanium.Filesystem.getResourcesDirectory() + separator + "efdatav01");
var efSonar = Titanium.Database.open(dbFile);
I get an error
[ERROR][SQLiteLog(  898)] (14) cannot open file at line 30174 of [00bb9c9ce4]
[ERROR][SQLiteLog(  898)] (14) os_unix.c:30174: (2) open(/file:/android_asset/Resources/efdatav01) - 
[ERROR][SQLiteDatabase(  898)] Failed to open database '/file:/android_asset/Resources/efdatav01'.
[ERROR][SQLiteDatabase(  898)] android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
 
[ERROR][TiJSError(  898)] (main) [414,1759] ----- Titanium Javascript Runtime Error -----
[ERROR][TiJSError(  898)] (main) [1,1760] - In app.js:40,35
[ERROR][TiJSError(  898)] (main) [1,1761] - Message: Uncaught Error: Java Exception occurred
[ERROR][TiJSError(  898)] (main) [1,1762] - Source:         var efSonar = Titanium.Database.open(dbFile);
[ERROR][V8Exception(  898)] Exception occurred at app.js:40: Uncaught Error: Java Exception occurred

Thanks for the help. I really would like to get this app off the ground.

1 Answer

Try var efSonar = Titanium.Database.open('efdatav01'); instead. No need to go get a file path or whatnot.

— answered 8 months ago by Shannon Hicks
answer permalink
3 Comments
  • Something so simple... Thank you. Will that keep the database persistent through builds?

    — commented 8 months ago by Kris Mitchell

  • It tells me with a select statement, that the table I put in there is not. Is it really installing the db? How am I able to check?

    — commented 8 months ago by Kris Mitchell

  • Ti.Database.install will copy your database from your app Resources and install it onto the phone... if it does not already exist. So "persistent through builds" would be correct, as long as you don't delete the app on the test device.

    The install location is different on iOS & Android, as is the actual end-result filename. If you're running this in the iOS Simulator, you can find the "installed" version at ~/Library/Application Support/iPhone Simulator/[the iOS version you're running]/Applications/[some sort of GUID]/Library/Private Documents/

    — commented 8 months ago by Shannon Hicks

Your Answer

Think you can help? Login to answer this question!