首页 文章

将数据从一个SQLite数据库复制到另一个

提问于
浏览
114

我有2个SQLite数据库,它们有共同的数据但目的不同,我想避免重新插入数据,所以我想知道是否可以将整个表从一个数据库复制到另一个数据库?

6 回答

  • 46

    对于一次性操作,您可以使用.dump和.read .

    从old_db.sqlite转储表my_table

    c:\sqlite>sqlite3.exe old_db.sqlite
    sqlite> .output mytable_dump.sql
    sqlite> .dump my_table
    sqlite> .quit
    

    假设那里的表不存在,请将转储读入new_db.sqlite

    c:\sqlite>sqlite3.exe new_db.sqlite
    sqlite> .read mytable_dump.sql
    

    现在你克隆了你的 table . 要对整个数据库执行此操作,只需在.dump命令中省略表名 .

    奖励:数据库可以有不同的编码 .

  • 6

    单线上最简单,最正确的方式:

    sqlite3 old.db ".dump mytable" | sqlite3 new.db
    

    将保留主键和列类型 .

  • 0

    您必须使用ATTACH命令将Database X与数据库Y相连,然后对要传输的表运行相应的Insert Into命令 .

    INSERT INTO X.TABLE SELECT * FROM Y.TABLE;
    

    或者,如果列未按顺序匹配:

    INSERT INTO X.TABLE(fieldname1, fieldname2) SELECT fieldname1, fieldname2 FROM Y.TABLE;
    
  • 149

    考虑一个例子,我有两个数据库,即allmsa.db和atlanta.db . 假设数据库allmsa.db在美国有所有msas的表,而数据库atlanta.db为空 .

    我们的目标是将atlanta表从allmsa.db复制到atlanta.db .

    步骤

    • sqlite3 atlanta.db(进入亚特兰大数据库)

    • 附上allmsa.db . 这可以使用命令 ATTACH '/mnt/fastaccessDS/core/csv/allmsa.db' AS AM; 来完成,注意我们给出要附加的数据库的完整路径 .

    • 使用 sqlite> .databases 检查数据库列表,可以看到输出为

    seq  name             file                                                      
    ---  ---------------  ----------------------------------------------------------
    0    main             /mnt/fastaccessDS/core/csv/atlanta.db                  
    2    AM               /mnt/fastaccessDS/core/csv/allmsa.db
    
    • 现在你来到你的实际目标 . 使用命令 INSERT INTO atlanta SELECT * FROM AM.atlanta;

    这应该符合您的目的 .

  • 27

    从数据库到另一个数据库的复制表的Objective-C代码

    -(void) createCopyDatabase{
    
              NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
              NSString *documentsDir = [paths objectAtIndex:0];
    
              NSString *maindbPath = [documentsDir stringByAppendingPathComponent:@"User.sqlite"];;
    
              NSString *newdbPath = [documentsDir stringByAppendingPathComponent:@"User_copy.sqlite"];
              NSFileManager *fileManager = [NSFileManager defaultManager];
              char *error;
    
             if ([fileManager fileExistsAtPath:newdbPath]) {
                 [fileManager removeItemAtPath:newdbPath error:nil];
             }
             sqlite3 *database;
             //open database
            if (sqlite3_open([newdbPath UTF8String], &database)!=SQLITE_OK) {
                NSLog(@"Error to open database");
            }
    
            NSString *attachQuery = [NSString stringWithFormat:@"ATTACH DATABASE \"%@\" AS aDB",maindbPath];
    
           sqlite3_exec(database, [attachQuery UTF8String], NULL, NULL, &error);
           if (error) {
               NSLog(@"Error to Attach = %s",error);
           }
    
           //Query for copy Table
           NSString *sqlString = @"CREATE TABLE Info AS SELECT * FROM aDB.Info";
           sqlite3_exec(database, [sqlString UTF8String], NULL, NULL, &error);
            if (error) {
                NSLog(@"Error to copy database = %s",error);
            }
    
            //Query for copy Table with Where Clause
    
            sqlString = @"CREATE TABLE comments AS SELECT * FROM aDB.comments Where user_name = 'XYZ'";
            sqlite3_exec(database, [sqlString UTF8String], NULL, NULL, &error);
            if (error) {
                NSLog(@"Error to copy database = %s",error);
            }
     }
    
  • 7

    我需要将数据从sql server compact database移动到sqlite,因此使用sql server 2008可以右键单击表并选择“Script Table To”然后选择“Data to Inserts” . 复制insert语句删除'GO'语句,并在使用'DB Browser for Sqlite'应用程序应用于sqlite数据库时成功执行 .

相关问题