2014-10-03 38 views
0

我有兩個表'table1'和'table_00'。我想從表1還可以將一些數據table_00無法在iOS中使用多條語句在sqlite中將數據從一個表插入另一個表

我有這樣的方法,這樣做:

- (void)moveData { 


NSString *docsDir; 
NSArray *dirPaths; 

// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 

// Build the path to the database file 

databasePath = [[NSString alloc] 
       initWithString: [docsDir stringByAppendingPathComponent: 
           @"MYDB_3.db"]]; 

sqlite3_stmt *statement; 
const char *dbpath = [databasePath UTF8String]; 

if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) { 

    NSString *[email protected]"INSERT INTO TABLE_00 SELECT * FROM TABLE_1 WHERE ID=00000" ; 
    const char *insert_stmt = [insertSQL UTF8String]; 

    //i am using only one 'insertSQL', i put two 'insertSQL' strings here for reference 
    //mulitple statements 
    NSString *[email protected]"INSERT INTO TABLE_00 SELECT * FROM TABLE_1 WHERE UTILITYID='00000';" 
         "INSERT INTO TABLE_01 SELECT * FROM TABLE_1 WHERE UTILITYID='00001';" 
         "INSERT INTO TABLE_02 SELECT * FROM TABLE_1 WHERE UTILITYID='00002';" 
    "INSERT INTO TABLE_03 SELECT * FROM TABLE_1 WHERE UTILITYID='00003';"; 

    //this multiple query statement is not working 

    sqlite3_prepare_v2(myDatabase, insert_stmt, -1, &statement, NULL); 
    if (sqlite3_step(statement) == SQLITE_DONE) { 
        NSLog(@"data moved"); 
    } else { 
     NSLog(@"data not moved"); 
    } 
    sqlite3_finalize(statement); 
    sqlite3_close(myDatabase); 
} 

我把這種方法後,我可以用一個查詢移動數據,但是,我不能通過使用多個語句來移動數據。請告訴我爲什麼會發生這種情況?

在此先感謝。

回答

0

我用for循環執行多個語句。這裏是我怎麼做的

- (void)moveDatatoUtilityTables { 

NSMutableArray *ID_Table=[[NSMutableArray alloc]init]; 
NSMutableArray *ID=[[NSMutableArray alloc]init]; 

[ID_Table addObject:@"TABLE_00"]; 
[ID_Table addObject:@"TABLE_01"]; 
[ID_Table addObject:@"TABLE_02"]; 

[ID addObject:@"00000"]; 
[ID addObject:@"00001"]; 
[ID addObject:@"00002"]; 

NSLog(@"database path is %@",databasePath); 

sqlite3_stmt *statement; 
const char *dbpath = [databasePath UTF8String]; 

if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) { 

    for (int i=0; i<2; i++) { 


     NSString *insertSQL=[NSString stringWithFormat:@" INSERT INTO %@ SELECT * FROM TABLE_1 WHERE ID='%@'",[ID_Table objectAtIndex:i],[ID objectAtIndex:i]]; 


    const char *insert_stmt = [insertSQL UTF8String]; 


    sqlite3_prepare_v2(myDatabase, insert_stmt, 
         -1, &statement, NULL); 

    // const char *sqlite3_column_name(sqlite3_stmt*, int N); 

    if (sqlite3_step(statement) == SQLITE_DONE) { 

        NSLog(@"data moved"); 


    } else { 

     NSLog(@"data not moved"); 
    } 



    sqlite3_finalize(statement); 

    } 
     sqlite3_close(myDatabase); 
} 

}

1

我認爲你有一個額外的,它應該是:

INSERT INTO TABLE_00 SELECT * FROM TABLE_1 WHERE ID= '00000' 

一個更好的辦法是使用佔位符:

sqlite3_stmt *compiled = nil; 
const char *sql = "INSERT INTO TABLE_00 SELECT * FROM TABLE_1 WHERE ID= ?"; 
sqlite3_prepare_v2(database, sql, -1, &compiled, NULL); 
sqlite3_bind_text(compiled, 1, [yourId UTF8String], -1, SQLITE_TRANSIENT); 
sqlite3_step(compiled); 
sqlite3_finalize(compiled); 
+0

嗨,我這樣做,和它說的數據移動,但是當我檢查到該表中,沒有數據 – 2014-10-03 20:21:08

+0

是文字實際上 – 2014-10-03 20:26:48

+0

我做到了,它工作。但我試圖用多個語句來做同樣的事情,在這種情況下,只有第一個語句執行。看看這個問題,我會再次修改它! – 2014-10-03 20:30:33

相關問題