Seite 1 von 1

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

Verfasst: 24. Juli 2013 20:33
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!

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

Verfasst: 24. Juli 2013 21:58
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

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

Verfasst: 24. Juli 2013 22:10
von MADsCIENTIST
Ok, habe das schon, nachdem ich diesen Beitrag verfasst habe, eingebaut... funktioniert natürlich^^