Open two sqlite database and execute query in iphone(读写两数据库)
2013-12-30 13:52
489 查看
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); } ios iphone sqlite3
|
3 Answers
activeoldestvotesup vote1down voteaccepted | 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
| ||||||||||||||||
comment |
up vote1down vote | 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..
| ||||
comment |
up vote1down vote | 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.
| ||||||||
|
相关文章推荐
- PHP Warning: SQLite3::query(): unable to open database file in
- 使用SQLiteOpenHelper获取用于操作数据库的SQLiteDatabase实例
- SQLiteDatabase 多线程数据库读写分析与优化
- 用 iPhone 的 sqlite 数据库读写图片
- 用 iPhone 的 sqlite 数据库读写图片
- 用 iPhone 的 sqlite 数据库读写图片
- 评论数据库Win A Free Copy of Packt’s Managing Multimedia and Unstructured Data in the Oracle Database e-book
- 用 iPhone 的 sqlite 数据库读写图片
- Adaptive Query Optimization in Oracle Database 12c (12.1 and 12.2)
- Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
- .Net版SQLite无法访问网络位置的数据库文件--提示cannot open database file
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- 使用SQLiteOpenHelper获取用于操作数据库的SQLiteDatabase实例
- Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
- How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络
- 【安卓面试题】使用SQLiteOpenHelper的getReadableDatabase()获得的数据库能不能,做写的操作
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- 136.You have two database servers SEMP and SACCT. The database in the SEMP server maintains the empl
- Android数据库入门小案例SQLiteOpenHelper和SQLiteDatabase使用
- sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database