Reputation: 952
I have below 2 approach to create Db table in titanium.
Approach 1 -- Create a sqlite database, using queries in the titanium code. like Ti.Database.open("DBName"); and then create tables using sqlite queries
var db = Ti.Database.open('DBName');
db.execute('CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT)');
Its working fine.
But i need to know how to create table by using model
Approach 2 -- Create database Ti.Database.open("DBName");
Create model book.js and here is the code
exports.definition = {
config: {
"columns": {
"id":"TEXT PRIMARY KEY",
"title":"TEXT ",
"author":"TEXT",
},
adapter: {
type: "sql",
collection_name: "book"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// extended functions and properties go here
});
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
// extended functions and properties go here
});
return Collection;
}
};
Now I am in little bit confusion for creating table using model.
Please help and guide me for creating table.
Upvotes: 2
Views: 1373
Reputation: 1960
If the adapter type="sql" then your DB is automatically created.
If you're using models, you no longer need to worry about opening the DB etc.
From the Alloy_Collection_and_Model_Objects, create a model and save it to DB:
var book = Alloy.createModel('book', {title:'Green Eggs and Ham', author:'Dr. Seuss'});
book.save();
To load data back from the DB:
var books = Alloy.Collections.instance('books');
var filteredArray = books.where({book_id: args.bookId});
Study the Alloy_Collection_and_Model_Objects and the Backbone JS docs
Upvotes: 4