Objective-C来自多个数据库的SQLite连接表

Sur*_*amy 4 sqlite mobile xcode objective-c ipad

我正在研究一个IPAD项目.该项目有2个sqlite数据库.第一个说customer.sqlite,第二个叫address.sqlite.Customer.sqlite随应用程序一起提供,每次启动应用程序时都会从服务器下载address.sqlite.一切正常.我在这里的问题,我可以使用objective-c在2个不同数据库中的表上进行连接.

我可以使用sqlite3_open(filename,sqliteconnection)打开与单个数据库的连接,如何将另一个数据库连接到同一个连接?那可能吗??

谢谢

Suresh Kumar Narayanasamy

Sur*_*amy 8

好的找到了答案.下面是示例代码

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));

     }
}    
Run Code Online (Sandbox Code Playgroud)

谢谢

Suresh Kumar Narayanasamy