2
#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.

Dewsworld
  • 13,367
  • 23
  • 68
  • 104

2 Answers2

2

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, ...
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • I managed it with a cut of **CHARSET** and **auto_incremnt**. Btw, **autoincrement** also not working. – Dewsworld Mar 17 '12 at 06:06
  • Your `CREATE TABLE employees(id int primary key auto_increment, lastname varchar(255), firstname varchar(255), department int)` gives me a syntax error in the `sqlite3` CLI tool, changing it as above makes it work fine. I also changed `int` to `integer`. – mu is too short Mar 17 '12 at 06:10
1

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.

Bill
  • 11,595
  • 6
  • 44
  • 52