[solved] Wie in SQLite auf Existenz von Table prüfen?

Alles rund um die Programmierung mit Qt
Antworten
MADsCIENTIST

[solved] Wie in SQLite auf Existenz von Table prüfen?

Beitrag von MADsCIENTIST »

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!

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;
		}
	}
}
Hoffe jemand hat einen Tipp!
Zuletzt geändert von MADsCIENTIST am 24. Juli 2013 22:11, insgesamt 2-mal geändert.
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: Wie in SQLite auf Existenz von Table prüfen?

Beitrag von Christian81 »

isValid(): Returns true if the query is currently positioned on a valid record; otherwise returns false.

Da Du aber nie next() aufgerufen hast, wird da wohl immer false zurückkommen... Doku lesen hilft: http://qt-project.org/doc/qt-5.0/qtsql/ ... ml#details
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
MADsCIENTIST

Re: Wie in SQLite auf Existenz von Table prüfen?

Beitrag von MADsCIENTIST »

Ok, habe das schon, nachdem ich diesen Beitrag verfasst habe, eingebaut... funktioniert natürlich^^
Antworten