Search code examples
databasetitaniumappcelerator

Cannot access Titanium Mobile Database


My code below is a simple Titanium mobile application for Android. I implemented database to create a table in it. But the code i wrote doesn't work for me. I created db file in resource folder also. But there is no change in the database. Is my code to connect database correct? Someone please verify my code and answer me the query.

var win = Ti.UI.createWindow({
    title:'Window',
    backgroundColor:'black',
    borderColor:'grey'
});

var label1 = Ti.UI.createLabel({
    text:'Welcome to new Window',
    color:'white',
    font:{fontSize:20},
    top:10
});

var text1 = Ti.UI.createTextField({
   top:50,width:200
});

var text2 = Ti.UI.createTextField({
   top:100,width:200
});

var db = Titanium.Database.open('testdb');

db.execute('INSERT INTO tips (title, tip ) VALUES(?,?)','santhosh','sathya');

db.close();

win.add(label1);

win.add(text1);

win.add(text2);

win.open({fullscreen:false});

Solution

  • If you have an existing SQLite database file that you want your application to use, then you must install it first.

    Titanium.Database.install( 'path/to/file.s3db', 'testdb' );
    

    However, be aware that once you install it, the file you specify above is not the database that will be in use by the app. Per this guide:

    On the other hand, install() will copy a pre-existing database file from Titanium's Resources directory, or one of its descendants, to applicationDataDirectory/../databases/ and return a reference to the opened database.