[solved] Wie in SQLite auf Existenz von Table prüfen?
Verfasst: 24. Juli 2013 20:33
Hallo,
ich versuche gerade automatisch eine SQLite-Datenbank und deren Inhalt erstellen zu lassen, nur scheint folgender Code noch fehlerhaft zu sein, weil *.db angeblich ungültig ist!
Hoffe jemand hat einen Tipp!
ich versuche gerade automatisch eine SQLite-Datenbank und deren Inhalt erstellen zu lassen, nur scheint folgender Code noch fehlerhaft zu sein, weil *.db angeblich ungültig ist!
Code: Alles auswählen
QSqlQuery query = QSqlQuery::QSqlQuery(db);
// treat table - create table and its content if necessary
if(!query.exec("SELECT * FROM sqlite_master WHERE type='table' AND name='" + TABLE_NAME + "'")) {
msgBox.setText("1. " + query.lastError().text());
msgBox.exec();
return;
}
// if table doesn't exist, create it
if(query.isValid()) { // EVENTUEL LIEGT IN DIESER ZEILE HIER DER FEHLER; HIER MÖCHTE ICH PRÜFEN, OB DIE ENTSPRECHENDE TABELLE GEFUNDEN WURDE
QString extCommand = "";
for(int i=0; i<ATTRCOUNT; i++) {
extCommand += ATTRNAMELIST[i] + ' ' + ATTRSETTINGS[i];
if(i+1 < ATTRCOUNT)
extCommand += ", ";
}
if(!query.exec("CREATE TABLE " + TABLE_NAME + " (" + extCommand + ')')) {
msgBox.setText("2. " + query.lastError().text()); // query.lastError().text().toLocal8Bit().data()
msgBox.exec();
return;
}
}
// treat the table's content - check for missing attributes and add them
for(int i=0; i<ATTRCOUNT; i++) {
if(!query.exec("SELECT * FROM " + TABLE_NAME + " WHERE type='column' AND name='" + ATTRNAMELIST[i] + "'")) {
if(!query.exec("ALTER TABLE " + TABLE_NAME + " ADD " + ATTRNAMELIST[i] + ' ' + ATTRSETTINGS[i])) {
msgBox.setText("3. " + query.lastError().text());
msgBox.exec();
return;
}
}
}