Search code examples
titaniumappceleratorappcelerator-titaniumappcelerator-alloy

Uncaught no such table: Ti.Database.install


I am using Ti.Database.install to install pre existing database which I have created via SQLite Manager. I have inserted some data as well in city table. On IOS simulator everything works fine. When I am trying run the same on Android device I am getting error "Uncaught no such table: city (code 1)". I have placed test.sqlite inside assets/db folder

var database =  Ti.Database.install('/db/test.sqlite', 'test');
Ti.API.debug('installed '+ database.getName()  );

var db = Ti.Database.open('test');
Ti.API.debug('reopened db'  );

var rs = db.execute('SELECT * from city LIMIT 5');
while (rs.isValidRow()) {

   // Ti.API.info(rs.fieldByName('name'));
   // rs.next();
}

db.close();

Solution

  • This method always work for Android/iOS using single code-base. Keep database file in this location:

    Project -> app -> lib -> mydatabase.sqlite

    Then I use below code in a file which is also in same lib directory for both Android/iOS:

    Project -> app -> lib -> my_database.js :

    try {
        var db = Ti.Database.install('mydatabase.sqlite', 'APP_DB_NAME');
        db.close();
    } catch (ex) {}
    

    Note: As per docs, install() method takes arguments path using same-directory approach, which means that wherever your database file is, you will have to run the install code in .js-file which must be in same directory.

    So, the above .sqlite file is in that lib folder, & that install script .js file is also in that lib folder, which further gives you a common place to run the install script only in 1 file & you can use that file anywhere using require('my_database');