加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 18:05:35 所属栏目:百科 来源:网络整理
导读:我正在研究一个IPAD项目.该项目有2个sqlite数据库.第一个说customer.sqlite,第二个叫address.sqlite. Customer.sqlite随应用程序一起提供,每次启动应用程序时都会从服务器下载address.sqlite.一切正常. 我在这里的问题,我可以使用 objective-c在2个不同数据
我正在研究一个IPAD项目.该项目有2个sqlite数据库.第一个说customer.sqlite,第二个叫address.sqlite.
Customer.sqlite随应用程序一起提供,每次启动应用程序时都会从服务器下载address.sqlite.一切正常.
我在这里的问题,我可以使用 objective-c在2个不同数据库中的表上进行连接.

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

谢谢

Suresh Kumar Narayanasamy

解决方法

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

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,&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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读