Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Titanium.Database undefined

I am trying to learn the platform and get down the basics of using sql data in a table view. My code is

var win = Titanium.UI.currentWindow;
//Database
var db = Titanium.Database.install('tips.sql','tips');


//This is the array we'll use to back the table view
var data = [];

//Get data for tableview
var rows = db.execute('SELECT * FROM tips');
while (rows.isValidRow()) {
  data.push({
    title: rows.fieldByName('title'),
    tip: rows.fieldByName('tip')
  });
    rows.next();
}
rows.close();
db.close();
// create table view
var tableview = Titanium.UI.createTableView({
    data:data,
    searchHidden:true
});

// create table view event listener
tableview.addEventListener('click', function(e) {
    Titanium.UI.createAlertDialog({
      title: e.rowData.title, 
      message:e.rowData.tip
    }).show();
});

win.add(tableview);

The error that I receive is

[ERROR] Script Error = Result of expression 'Titanium.Database' [undefined] is not an object. at guides.js (line 3).

tips.sql is the testdb.db that came with the kitchensink its just been exported by a program of mine. I have also tried to run it with testdb.db and have the same error. I have also tried to place the db file in the same folder, in the parent folder and elsewhere.

— asked October 31st 2010 by Grant Wolz
  • database
  • iphone
  • undefined
1 Comment
  • I can only assume that isValidRow method is returning false positive result, this way letting the while cycle execute. As a result there is no rows object and you get the error.

    — commented June 14th 2012 by Zurab Shubitidze

6 Answers

  • Try deleting the build folder and build again

    — answered February 25th 2011 by Per Jorgensen
    permalink
    2 Comments
    • This, and only this, solved the problem for me.

      — commented February 28th 2011 by Richard John
    • Worked for me as well, took me a while to figure that out

      — commented July 17th 2011 by Tom Schouteden
  • Try this instead of the Ti.Database.install() call:

    var conn = Titanium.Database.open('your_db');
    // Initialize the database
    conn.execute('CREATE TABLE IF NOT EXISTS table1 (id INTEGER PRIMARY KEY, field_name TEXT)');
    // etc.
    

    I'm not sure if you can load an external sql file with your table definitions. I've never tried. I just put the above into my js file.

    — answered October 31st 2010 by Tim Poulsen
    permalink
    0 Comments
  • I think the install method is poorly named because it works more like open than install. A functioning example looks like this.

    var db = Titanium.Database.install('../testdb.db','quotes');
    

    The first parameter is a path to the database. The second parameter is the database you are trying to access. See http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.Database.install-method.html

    Like Tim sad, you would probably want to test for the existence of the table and create as needed. You could do the test then use the file object to open a SQL file and pull the commands to execute or put them in the JS file.

    — answered October 31st 2010 by John McKnight
    permalink
    0 Comments
  • The problem is that I don't want to create a new table, the user will not be entering any new information. I need to display information that already exists in a database that comes with the app. This is why I am using the install command as recommended by titanium.

    Just no matter what I try I always get the same cryptic error.

    var db = Titanium.Database.open('../testdb.db');
    db.execute('CREATE TABLE IF NOT EXISTS tips (id INTEGER PRIMARY KEY, title TEXT, tip TEXT)');
    

    Errors

    var db = Titanium.Database.open('testdb.db');
    db.execute('CREATE TABLE IF NOT EXISTS tips (id INTEGER PRIMARY KEY, title TEXT, tip TEXT)');
    

    Errors

    var db = Titanium.Database.open('tips');
    db.execute('CREATE TABLE IF NOT EXISTS tips (id INTEGER PRIMARY KEY, title TEXT, tip TEXT)');
    

    Errors

    var db = Titanium.Database.open('tips');
    db.execute('CREATE TABLE IF NOT EXISTS tips (id INTEGER PRIMARY KEY, title TEXT, tip TEXT)');
    

    Errors

    var db = Titanium.Database.install('../testdb.db');
    db.execute('CREATE TABLE IF NOT EXISTS tips (id INTEGER PRIMARY KEY, title TEXT, tip TEXT)');
    

    Errors

    var db = Titanium.Database.install('../testdb.db', 'tips');
    db.execute('CREATE TABLE IF NOT EXISTS tips (id INTEGER PRIMARY KEY, title TEXT, tip TEXT)');
    

    Errors

    var db = Titanium.Database.install('../testdb.db', 'tips');
    

    Errors

    ../testdb.db Errors, testdb.db Errors

    Maybe someone can just post a full code sample to install a database and read from it. This should be one of the easiest things to on any platform and yet there isn't a single code sample that uses database.install.

    — answered October 31st 2010 by Grant Wolz
    permalink
    0 Comments
  • The code in this post http://developer.appcelerator.com/question/71731/database-install-making-me-sad-

    If you paste that code into your app.js and put the testdb.db from the KitchenSink in the Resources folder alongside app.js the code will work as long as you get rid of the ../ from the path in the install method.

    — answered October 31st 2010 by John McKnight
    permalink
    0 Comments
  • Bump. I've tried every method on this page so far, and the pages this one link to, and not one method works. I get

    [ERROR] Script Error = Result of expression 'Titanium.Database' 
    [undefined] is not an object. at history.js (line 33). with all.
    

    My code: var conn = Titanium.Database.install('testdb.db','testdb');

    My environ:
    iPhone simulator on OS X Snow Leapard
    Titanium 1.2.2
    Titanium SDK 1.5.1
    iPhone SDK 4.2

    Could this be a permissions thing? I've made sure the database is in the same folder, and is not read only. I can open the database with other apps just fine.

    Frustrated,
    RD

    — answered February 25th 2011 by Richard John
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.