db.js
// creates your database if necessary
var db = Titanium.Database.open('myinfodb');
db.execute('CREATE TABLE IF NOT EXISTS [info] (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)');
db.close();
var addIntoDb = function(name) {
var db = Titanium.Database.open('myinfodb');
db.execute('INSERT INTO info (name) VALUES(?)', name);
Ti.Ti.API.info(name+' Added to db');
db.close();
}
exports.addIntoDb = addIntoDb; // <=== This exposes the function to another file
// This function would remain accessible only to the file because we didn't export the function
function getFromDb() {
var holddatavar = db.execute('SELECT name FROM info');
return holddatavar;
}
那么你可以使用它在其他JavaScript文件来访问它,像这样:
var db = require('/mypath/db'); // NO .js extension on this
db.addIntoDb('Pravin');