Reputation: 14033
#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.
Upvotes: 2
Views: 555
Reputation: 11822
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.
Upvotes: 1
Reputation: 434595
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, ...
Upvotes: 2