本文介绍了将一个数据库中的表复制到另一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想将表从aDB复制到另一个bDB。 所以我做了一个方法。 我认为打开2数据库和使用插入查询将工作,但我不知道详细方式。 - void)copyDatabaseTableSoruceFileName:(NSString *)source CopyFileName:(NSString *)copy { sqlite3 * sourceDatabase = NULL; sqlite3 * copyDatabase = NULL; NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * documentDir = [paths objectAtIndex:0]; // source [self copyFileIfNeed:source path:documentDir]; NSString * SourceDBPath = [documentDir stringByAppendingPathComponent:source]; if(sqlite3_open([SourceDBPath UTF8String],& sourceDatabase)!= SQLITE_OK) { NSLog(@DB文件打开错误:%@,SourceDBPath); sourceDatabase = NULL; } // copy [self copyFileIfNeed:copy path:documentDir]; NSString * CopyDBPath = [documentDir stringByAppendingPathComponent:copy]; if(sqlite3_open([CopyDBPath UTF8String],& copyDatabase)!= SQLITE_OK) { NSLog(@DB文件打开错误:%@,CopyDBPath); copyDatabase = NULL; } //来源复制 //在这方面如何? } 是不是?和如何使更多? // source to copy area。解决方案在sqlite3中,可以结合ATTACH [1]和CREATE TABLE。 2]命令: 首先,打开bDB数据库,然后执行以下语句: ATTACH DATABASEmyother.dbAS aDB;之后,您可以使用CREATE TABLE语法: CREATE TABLE newTableInDB1 AS SELECT * FROM aDB.oldTableInMyOtherDB; 这会将数据复制到您的新数据库。 如果你想合并数据,还有一个INSERT [3]语句,但是你需要像这样引用你的字段: INSERT INTO newtable(field1,field2) SELECT otherfield1,otherfield2 FROM aDB.oldTableInMyOtherDB; 参考文献: [1] a href =http://www.sqlite.org/lang_attach.html> http://www.sqlite.org/lang_attach.html [2] http://www.sqlite.org/lang_createtable.html [3] http://www.sqlite.org/lang_insert.html I want to copy table from aDB to another bDB.So I made a method.I think open 2 database and Using insert query will work but I don't know detail way.-(void)copyDatabaseTableSoruceFileName:(NSString *)source CopyFileName:(NSString *)copy{sqlite3 *sourceDatabase=NULL;sqlite3 *copyDatabase=NULL;NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);NSString* documentDir = [paths objectAtIndex:0];//source [self copyFileIfNeed:source path:documentDir];NSString *SourceDBPath = [documentDir stringByAppendingPathComponent:source];if( sqlite3_open([SourceDBPath UTF8String],&sourceDatabase)!= SQLITE_OK ){ NSLog(@"DB File Open Error :%@", SourceDBPath); sourceDatabase = NULL;}//copy[self copyFileIfNeed:copy path:documentDir];NSString *CopyDBPath = [documentDir stringByAppendingPathComponent:copy];if( sqlite3_open([CopyDBPath UTF8String],©Database)!= SQLITE_OK ){ NSLog(@"DB File Open Error :%@", CopyDBPath); copyDatabase = NULL;}//source to copy // How in this area?}Is it right? and how to make more ? //source to copy area. 解决方案 in sqlite3, you can combine the ATTACH[1] and CREATE TABLE .. AS[2] commands:first, you are opening the "bDB" database, and then execute the following statement:ATTACH DATABASE "myother.db" AS aDB;After that, you can use the CREATE TABLE syntax:CREATE TABLE newTableInDB1 AS SELECT * FROM aDB.oldTableInMyOtherDB;This will "copy" the data over to your new database.If you want to merge the data, there is also an INSERT[3] statement, but with that you will need to reference your fields like this:INSERT INTO newtable (field1,field2) SELECT otherfield1,otherfield2 FROM aDB.oldTableInMyOtherDB;References:[1] http://www.sqlite.org/lang_attach.html[2] http://www.sqlite.org/lang_createtable.html[3] http://www.sqlite.org/lang_insert.html 这篇关于将一个数据库中的表复制到另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 16:40