Objective-C来自多个数据库的SQLite连接表 [英] Objective-C SQLite join tables from multiple database

查看:102
本文介绍了Objective-C来自多个数据库的SQLite连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an IPAD project. The project has 2 sqlite database. the first one say customer.sqlite and the second one called address.sqlite. Customer.sqlite comes along with the app and the address.sqlite is downloaded from the server every time the app is started. Everything works fine. The question i have here, can i do a join on 2 tables that are in 2 different database using objective-c.

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

I can open a connection to a single database using sqlite3_open(filename, sqliteconnection), how do i attach another database to the same connection? Is that possible??

谢谢

Suresh Kumar Narayanasamy

Suresh Kumar Narayanasamy

推荐答案

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

Ok found out the answer. Below is the 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));

     }
}    

谢谢

Suresh Kumar Narayanasamy

Suresh Kumar Narayanasamy

这篇关于Objective-C来自多个数据库的SQLite连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆