Open two sqlite database and execute query in iphone(读写两数据库)
来源:互联网 发布:mysql 云服务 编辑:程序博客网 时间:2024/05/12 12:43
Here i want to execute query using two database and copy data from table of database1 to another database2. here i am able to open one database but getting problem in openning other database. Thanks in advance to all.
CurrentStatus *status = [CurrentStatus sharedCurrentStatus]; sqlite3 *database; sqlite3 *database1; sqlite3_stmt *statement; sqlite3_stmt *statement1; NSString *dbPath = [status.applicationDocumentDirectory stringByAppendingPathComponent:@"database.sqlite"]; NSString *dbPath1 = [status.applicationDocumentDirectory stringByAppendingPathComponent:@"database1.sqlite"]; if ((sqlite3_open_v2([dbPath UTF8String], &database, SQLITE_OPEN_READWRITE , NULL) == SQLITE_OK) && (sqlite3_open_v2([dbPath1 UTF8String], &database1, SQLITE_OPEN_READWRITE , NULL) == SQLITE_OK)) { NSString *sqlStr = [[NSString alloc] initWithFormat:@"select * from Login" ]; NSString *sql = [[NSString alloc] initWithString:sqlStr]; if ((sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK)) { NSLog(@"DB prepare_v2 Opening successfully"); if (sqlite3_step(statement) == SQLITE_ROW) { } else { } sqlite3_finalize(statement); NSLog(@"DB Opening successfully"); sqlite3_close(database); }else { NSLog(@"else DB Opening successfully"); } sqlite3_close(database); }
3 Answers
Try something similar to achieve your task ..
First, you are opening the "DB1" database, and then execute the following statement:
ATTACH DATABASE "2ndDB.db" AS 2ndDB;
After that, you can use the CREATE TABLE syntax:
CREATE TABLE newTableInDB1 AS SELECT * FROM 2ndDB.oldTableInMyOtherDB;
This will "copy" the data over to your new database
2ndDB
is not a valid identifier; you have to quote it as "2ndDB"
or use something like secondDB
. – CL.Oct 25 at 8:05 Copy which are the data you want it from database1 add each row in NSMutableDictionary and then add it to NSMutableArray. Close and reset your statement then open the database2 copy the data from NSMutableArray, that array act as database for you..
I hope it will work for you..
You need to You need to Attach databases and then copy tables from one database to another database. You need to do something like this:
NSString *attachSQL = [NSString stringWithFormat: @"ATTACH DATABASE \'%s\' AS %@", sourceDBPath,dbAlias];if (sqlite3_exec(db, [attachSQL UTF8String], NULL, NULL, &errorMessage) != SQLITE_OK) { NSString *errorStr = [NSString stringWithFormat:@"The database could not be attached: %@",[NSString stringWithCString:errorMessage encoding:NSUTF8StringEncoding]]; return errorStr; }
After that you need to copy table , like:
NSString *queryString=[NSString stringWithFormat:@"CREATE TABLE %@ AS SELECT * FROM %@.%@;",newTableName,dbAlias,origTableName];
And you are done, you have the new copy of database.
- Open two sqlite database and execute query in iphone(读写两数据库)
- PHP Warning: SQLite3::query(): unable to open database file in
- 用 iPhone 的 sqlite 数据库读写图片
- 用 iPhone 的 sqlite 数据库读写图片
- 用 iPhone 的 sqlite 数据库读写图片
- iPhone学习 Sqlite数据库读写图片实例
- Write database open in memory to disk (SQLite)
- Qt13 Creating connection open and close function with sqlite database
- Android SQLite数据库异常: unable to open database file
- ListView and Sqlite Database
- 数据库,DataBase,sqlite
- How to execute a query in varchar2 ?
- Using Functions SYSFUNC and IFC to Conditionally Execute Statements in Open Code
- QT读写Sqlite数据库
- android sqlite 数据库读写
- sqlite数据库,读写 sqlite3_exec
- 136.You have two database servers SEMP and SACCT. The database in the SEMP server maintains the empl
- android.database.sqlite.SQLiteException: unable to open database file
- Linux基本配置和管理 8 ---- 程序管理与SELinux初探
- Cookie操作
- 那些年关于3D打印技术你不知道的那些事儿
- oracle同音模糊查询
- 去掉调用system产生的黑框
- Open two sqlite database and execute query in iphone(读写两数据库)
- RAC 修改 DB 实例名 步骤
- 解密3D打印技术与产业的发展及前景
- 命令行下的树形打印
- Hibernate之Hql使用预编译的方法的两种方法:/?(二十五)
- java连接MYSQL
- c# dynamic 无法创建 泛型变量的问题
- 优化基于ExtJS 4.1的应用
- 在XC2440上实现使用U盘自动更新系统的uboot