Objective-C SQLite joins tables from multiple databases

I am working on an iPad project. The project has 2 sqlite databases. the first says customer.sqlite, and the second says address.sqlite. Customer.sqlite comes with the application, and the .sqlite address is downloaded from the server every time the application starts. Everything is working fine. The question I have is, can I make a join on 2 tables that are in 2 different databases using objective-c.

I can open a connection to one database using sqlite3_open (file name, sqliteconnection), how to connect another database to the same connection? Is it possible?

thanks

Suresh Kumar Narayanasami

+4
source share
2 answers

Ok found out the answer. Below is a sample code

sqlite3 *_myLocalConnection; if (sqlite3_open([["Your First DB file path"] UTF8String], &_myLocalConnection) == SQLITE_OK) { NSString *strSQLAttach = [NSString stringWithFormat:@"ATTACH DATABASE \'%s\' AS SECOND", [["Your second db file path"] UTF8String] ]; char *errorMessage; if (sqlite3_exec(_myLocalConnection, [strSQLAttach UTF8String], NULL, NULL, &errorMessage) == SQLITE_OK) { sqlite3_stmt *myStatment; NSString *strSQL = @"select * from SECOND.ADDRESS myAddress inner join main.CUSTTOMER myCustomer on myAddress.CustomerID = myCustomer.customerID "; if (sqlite3_prepare_v2(_myLocalConnection, [strSQL UTF8String], -1, &myStatment, nil) == SQLITE_OK) //do your loop logic else NSLog(@"Error while attaching '%s'", sqlite3_errmsg(_myLocalConnection)); } } 

thanks

Suresh Kumar Narayanasami

+8
source

No, you can’t. But there should be no drawbacks doing JOINs with Objective-C code instead of using SQ (except that it might be more convenient for you to do JOINs in SQL rather than Objective-C).

Related: How to merge multiple database files in SQLite?

+1
source

All Articles