gpt4 book ai didi

ios - 打开两个sqlite数据库并在iphone中执行查询

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:26 26 4
gpt4 key购买 nike

这里我想使用两个数据库执行查询并将数据从数据库 1 的表复制到另一个数据库 2。在这里我可以打开一个数据库,但在打开其他数据库时遇到问题。在此先感谢大家。

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

最佳答案

尝试类似的事情来完成你的任务..

首先,您打开“DB1”数据库,然后执行以下语句:

ATTACH DATABASE "2ndDB.db" AS 2ndDB;

之后,您可以使用 CREATE TABLE 语法:

CREATE TABLE newTableInDB1 AS SELECT * FROM 2ndDB.oldTableInMyOtherDB;

这会将数据“复制”到您的新数据库

关于ios - 打开两个sqlite数据库并在iphone中执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19581647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com