Frage

#include <QtGui>
#include <QtSql>
#include <QDebug>
int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("test");
    db.setDatabaseName("firma");
    db.setUserName("user");
    db.setPassword("pass");

    if (!db.open()) {
        qDebug() << db.lastError();
        return 1;
    }

    QSqlQuery query;
    bool ret = query.exec("CREATE TABLE employees(id int primary key auto_increment, lastname varchar(255), firstname varchar(255), department int) ");
    qDebug() << ret << endl;
}

Every time I get false. I can't get the bug.

War es hilfreich?

Lösung

SQLite prefers to see autoincrement and only wants to apply it to integer columns, auto_increment is a syntax error with SQLite. Your SQL should look like this:

CREATE TABLE employees(id integer primary key autoincrement, ...

Andere Tipps

QSqlQuery can help you find out why exec() returns false. Call QSqlError QSqlQuery::lastError () const, then QString QSqlError::text () const. What you get is the text of the error as reported by the database and the driver.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top