Alors voici mon objectif: J'ai besoin d'attacher une base de données de synchronisation à ma base de données principale et de mettre à jour ou remplacer tous les champs dans ma base de données principale. Donc, j'ai d'abord joindre ma base de données. Je tente ensuite de parcourir toutes les tables. Voici la partie excentrique: à l'intérieur de ma chaîne de requête maître, quand je dis: SELECT nom FROM sqlite_master l'instruction if ne s'exécute pas et dit "Erreur: pas une erreur." Maintenant, lorsque je demande à la requête maître de sélectionner le nom FROM sync_db.sqlite_master, l'instruction if s'exécute. Cependant, j'obtiens une erreur indiquant qu'aucune table de ce type n'existe: sync_db.sqlite_master. Quelqu'un pourrait peut-être me guider à travers le protocole approprié? Merci d'avance.Erreur pas une erreur base de données sqlite en attachant la base de données
//Atataching the sync db to the master db
NSString *attachSQL = [NSString stringWithFormat:@"ATTACH DATABASE \'%@\' AS sync_db", dbPathSync];
NSLog(@"Here's the arratch string: %@", attachSQL);
//
if ((errorNum = sqlite3_exec(mainOpenHandle, [attachSQL UTF8String], NULL, NULL, &errorMessage)) == SQLITE_OK) {
NSString *masterQuery = [NSString stringWithFormat:@"SELECT name FROM sync_db.sqlite_master WHERE type='table';"];
const char *masterStmt = [masterQuery UTF8String];
sqlite3_stmt *statement;
//If statement does not execute and prints error saying "not an error" when
//place SELECT from "sqlite_master" inside master query.
if (sqlite3_prepare_v2(syncOpenHandle, masterStmt, -1, &statement, NULL)) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString * currentTable = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
NSLog(@"Here's the current table: %@",currentTable);
//This is where the magic happens. If there are any keys matching the database, it will update them. If there are no current keys in the database, the query will insert them.
if ([currentTable isEqualToString:@"USER_DATA"] == NO && [currentTable isEqualToString:@"USER_ACTIVITY"]== NO && [currentTable isEqualToString:@"USER_ITINERARY"] == NO) {
NSString *tblUpdate = [NSString stringWithFormat:@"INSERT or REPLACE INTO main.%@ SELECT * FROM sync_db.%@;",currentTable, currentTable];
const char *updateStmt = [tblUpdate UTF8String];
if ((errorNum = sqlite3_exec(mainOpenHandle, updateStmt, NULL, NULL, &errorMessage))!= SQLITE_OK) {
if (errorNum == 1) {
//A database reset is needded
self->isResetDataBase = YES;
}
dbErr = YES;
}
}
}
NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
}
NSLog(@"Erorr syncing the database: Code: %d, message: '%s'", error,sqlite3_errmsg(mainOpenHandle));
NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
sqlite3_finalize(statement);
//Detaching the database from the mainDB
NSString *detachSQL = [NSString stringWithFormat:@"DETACH DATABASE sync_db"]; // reference sync db
if ((errorNum = sqlite3_exec(mainOpenHandle, [detachSQL UTF8String], NULL, NULL, &errorMessage))!= SQLITE_OK) {
NSLog(@"Detatched syncDb Failed. ErrorMessage = %s ", errorMessage);
}
}
}
NSLog(@"Error sync ... '%s'", sqlite3_errmsg(syncOpenHandle));
//Closing the database when finished.
if (mainOpenHandle != nil) {
sqlite3_close(self.mainOpenHandle);
}
if (syncOpenHandle != nil) {
sqlite3_close(self.syncOpenHandle);
NSError *err;
int success = [fileManager fileExistsAtPath:dbPathSync];
if (success) {
[[NSFileManager defaultManager]removeItemAtPath:dbPathSync error: &error];
}
}
if (userOpenHandle != nil) {
sqlite3_close(self.userOpenHandle);
}
Je tente ensuite de parcourir toutes les lignes. Mais voici la partie excentrique. Intérieur de