Tip: How to "attach" another Sqlite database
This one was tough to figure out, so I thought I'd pass on how it's done. Say you have 2 databases, and you need to use the standard attach Sqlite syntax to join them. For example, in Sqlite:
sqlite3 db1.sql
>attach 'db2.sql' as db2;
>select * from db1 join db2 on .... etc.
How do we pull this off in Titanium Mobile?
Here's the code:
var db = Titanium.Database.install("db1", "db1");
var dir = Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory);
db.execute("attach '" + dir.getParent() +
"/Library/Application Support/database/db2.sql' as db2;");
var rows = db.execute("select * from db1 join db2...");
5 Answers
-
I've had success doing it like this (Ti Mobile SDK 1.8.2):
var dbOne = Ti.Database.open('db1'); var dbTwo = Ti.Database.open('db2'); var db1path = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory, '../Private Documents/db1.sql').nativePath; var db2path = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory, '../Private Documents/db2.sql').nativePath; //In the example at the top of this thread, they are missing "DATABASE" from the ATTACH statement dbOne.execute("ATTACH DATABASE '"+db1path+"' AS dbOne"); dbTwo.execute("ATTACH DATABASE '"+db2path+"' AS dbTwo"); //Ready for queries var result = dbOne.execute("SELECT * FROM dbTwo.tableName");
Good luck!
-
Hi Mark
I m trying almost same code. But it does not work for me. When i run my application it crash. Here is my code
var backup_db = Titanium.Database.open('db1');
var dir = Ti.Filesystem.getFile(Titanium.Filesystem.applicationSupportDirectory);backup_db.execute("attach '" + dir.getParent() +
"/database/databaseA.sql' as databaseA;");I got following error in titanium console.
2011-01-04 15:07:13.886 testVideo[1047:7903] [ERROR] Error occurred calling next on a PLSqliteResultSet. SQLite error: 'unable to open database file' for 'attach '/Users/apple/Library/Application Support/iPhone Simulator/4.1/Applications/BC8B0EC7-1F16-4EE8-B677-A4276F8457A4/Library/database/databaseA.sql' as databaseA;'
[ERROR] The application has crashed with an unhandled exception. Stack trace: -
Hi Mark
I m trying almost same code. But it does not work for me. When i run my application it crash. Here is my code
var backup_db = Titanium.Database.open('cellarhanddb1');
var dir = Ti.Filesystem.getFile(Titanium.Filesystem.applicationSupportDirectory);backup_db.execute("attach '" + dir.getParent() +
"/database/databaseA.sql' as databaseA;");I got following error in titanium console.
2011-01-04 15:07:13.886 testVideo[1047:7903] [ERROR] Error occurred calling next on a PLSqliteResultSet. SQLite error: 'unable to open database file' for 'attach '/Users/apple/Library/Application Support/iPhone Simulator/4.1/Applications/BC8B0EC7-1F16-4EE8-B677-A4276F8457A4/Library/database/databaseA.sql' as databaseA;'
[ERROR] The application has crashed with an unhandled exception. Stack trace: -
I have tried Mark's approach by using:
var dir = Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory);
db.execute("attach '" + dir.getParent()…But I found that dir.getParent() return null value, therefore the db path is not valid.
I am using Android emulator to perform this test.
Do you have any idea on it?
Also, if my attached DB as around 15000 records. What is the estimated time to complete the inserting to another DB?
Thanks.
-
For everyone who's interested in the solution for android:
ATTACH DATABASE 'data/data/" + Ti.App.getId() + "/databases/DBNAME' AS dbTwo