Search code examples
javascriptandroidsqlsqlitetitanium

Titanium mobile - Uncaught Error: no such table


I am having a terrible time trying to figure out why I keep getting this error. I have searched this and other forums and no answer seems to be able to help so I will ask here.

I am trying to create a basic photo catalogue app for Android using Titanium. The catalogue info is strore in an sqlite database.

I can create the database OK - I can access some info from it in one window. When I then go to another window and try to get further info from the table it gives me the error:

Uncaught error: no such table: photos:, while compiling SELECT..........

See code below:

This is the first window that opens from app.js

The table shows up fine here - NO errors

// create var for the currentWindow
var currentWin = Ti.UI.currentWindow;

// set the data from the database to the array
//function setData() {  

// create table view
var tableview = Ti.UI.createTableView({
});

//var db = Ti.Database.open('catalogue');
//db.remove();
var db = Ti.Database.install('catalogue.sqlite','photos');

var rows = db.execute('SELECT * FROM photos GROUP BY title');

// create the array
var dataArray = [];


while(rows.isValidRow()) {

    var title = rows.fieldByName('title');
    var id = rows.fieldByName('id');

    dataArray.push({title:title, id:id, url:'catalogue_detail.js'});
    rows.next();
}

tableview.setData(dataArray);

// add the tableView to the current window
currentWin.add(tableview);



tableview.addEventListener('click', function(e){

    if (e.rowData.url)
    {
        var win = Ti.UI.createWindow({
            id:e.rowData.id,
            title:e.rowData.title,
            url:e.rowData.url

        });

        win.open();
    }
});


// call the setData function to attach the database results to the array
//setData();

I then open catalogue_detail.js from the above and I get the error

catalogue_detail.js

var win = Titanium.UI.currentWindow;

var id = win.id

var tableview = Titanium.UI.createTableView({
});

var db = Titanium.Database.open('catalogue.sqlite');//I have chanegd this to 'photos' and I get the same error

var sql = db.execute('SELECT title, location, photographer FROM photos where id="' + id + '"');

var title = sql.fieldByName('title');


var location = sql.fieldByName('location');
var photographer = sql.fieldByName('photographer');  


// create the array
var dataArray = [];


while(sql.isValidRow()) {

    var title = sql.fieldByName('title');
    var location = sql.fieldByName('location');
    var photographer = sql.fieldByName('photographer');

    dataArray.push({title:title, location:location, photographer:photographer});
    sql.next();
}

tableview.setData(dataArray);


win.add(tableview);

So the table in my database called 'photos' open in one window then I get the error trying to open the same database?

I have uninstalled the app, recreated the database, renamed tables etc but still the same error...

I am sure it is a simple thing I am missing but I am very confused!

Thanks for any assistance.

JC


Solution

  • Try absolute path: Ti.Database.open('catalogue.sqlite'); and Ti.Database.install('/catalogue.sqlite');.

    Also remember about closing connection to database when you retrieve all data from query:

    sql.close();
    db.close();