Qt-interest Archive, September 2007
QSqlQuery, SQLITE table creation problem
Message 1 in thread
Hello!
I'm experiencing problems creating a database table using an QSqlQuery (Qt
4.3.1 self compiled; Kubuntu Linux 7.04; Qt uses its own SQLITE library).
According to the documentation, QSqlQuery::exec() returns true, if the query
has been executed successfully.
Using the attached example, calling QSqlQuery::exec() always returns false.
However, analysing a database file (created with the attachment) shows the
table has been created.
Am I missing something?
Kind regards,
Jan
#include "QSqlDatabase"
#include "QSqlDatabase"
#include "QSqlQuery"
#include "QDebug"
#include "QStringList"
#include "QApplication"
///
/// Usage:
/// ./database <filespecification>
/// Example:
/// ./database ./data.db
/// ./database ":memory:"
///
int main(int argc, char** argv)
{
const QString filename(argv[1]);
if(filename.length()==0)
{
qDebug() << "Must provide database name in argv[1]";
}
else
{
QApplication app(argc, argv);
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(filename);
qDebug() << "Opening database: " << filename;
if(database.open()==false)
{
qDebug() << "Can not open database";
}
else
{
const QString CREATE_TABLE("CREATE TABLE EXAMPLE (CONTENT TEXT)");
QSqlQuery query(CREATE_TABLE, database);
if(query.exec())
{
qDebug() << "Table created";
}
else
{
qDebug() << "Failed to create table";
}
}
database.close();
}
}
Message 2 in thread
Jan Kellmer wrote:
> Hello!
>
> I'm experiencing problems creating a database table using an QSqlQuery (Qt
> 4.3.1 self compiled; Kubuntu Linux 7.04; Qt uses its own SQLITE library).
>
> According to the documentation, QSqlQuery::exec() returns true, if the query
> has been executed successfully.
> Using the attached example, calling QSqlQuery::exec() always returns false.
> However, analysing a database file (created with the attachment) shows the
> table has been created.
>
> Am I missing something?
>
> Kind regards,
> Jan
>
Try the following maybe, and see what it's saying.
qDebug() << "Failed to create table:" << query.lastError();
--
[ signature omitted ]
Message 3 in thread
Hello!
On Monday 03 September 2007 08:18:46 Bill KING wrote:
[...]
>
> Try the following maybe, and see what it's saying.
>
> qDebug() << "Failed to create table:" << query.lastError();
Good idea! I added code to output every element of the QSqlError object:
-- snip -- snap -- snip -- snap -- snip -- snap -- snip --
jake@example:~/dbtest$ ./dbtest ":memory:"
Opening database: ":memory:"
Failed to create table
isValid ...... : true
databaseText . : "table EXAMPLE already exists"
driverText ... : "Unable to fetch row"
number ....... : 17
text ......... : "table EXAMPLE already exists Unable to fetch row"
type ......... : ConnectionError
-- snap -- snip -- snap -- snip -- snap -- snip -- snap --
Please note: this is from an in-memory database, created on process startup.
The above error does not make any sense to me: by definition the database is empty and the 'EXAMPLE' table does not exist. I tried different table names and databases: result and error codes stay the same.
According to the SQLITE3 documentation (wild guessing as I'm not familiar with the SQLITE3 C/C++ itf.) error code 17 is 'SQLITE_SCHEMA - The database schema changed'.
Kind regards,
Jan Kellmer
Message 4 in thread
> The above error does not make any sense to me: by definition the database is empty and the 'EXAMPLE' table does not exist. I tried different table names and databases: result and error codes stay the same.
> According to the SQLITE3 documentation (wild guessing as I'm not familiar with the SQLITE3 C/C++ itf.) error code 17 is 'SQLITE_SCHEMA - The database schema changed'.
The above error makes perfect sense.
const QString CREATE_TABLE("CREATE TABLE EXAMPLE (CONTENT TEXT)");
QSqlQuery query(CREATE_TABLE, database); <----- first execution
if(query.exec()) <----- second exection
Regards,
Guido
--
[ signature omitted ]
Message 5 in thread
Hi!
On Monday 03 September 2007 23:37:50 Guido Seifert wrote:
[...]
>
> const QString CREATE_TABLE("CREATE TABLE EXAMPLE (CONTENT TEXT)");
> QSqlQuery query(CREATE_TABLE, database); <----- first execution
> if(query.exec()) <----- second exection
This behaviour is even documented *head meets keyboard*
Thank you ... and sorry for pestering the list with this PEBKAC.
Kind regards,
Jan
--
[ signature omitted ]
Message 6 in thread
> This behaviour is even documented *head meets keyboard*
>
> Thank you ... and sorry for pestering the list with this PEBKAC.
Oh please, Qt is huge. The docs are great, but still it is easy to overlook a single
line. Happens sooner or later to everybody. :-)
Guido
--
[ signature omitted ]
Message 7 in thread
Guido Seifert wrote:
>> This behaviour is even documented *head meets keyboard*
>>
>> Thank you ... and sorry for pestering the list with this PEBKAC.
>>
>
> Oh please, Qt is huge. The docs are great, but still it is easy to overlook a single
> line. Happens sooner or later to everybody. :-)
>
> Guido
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>
I agree, I've made this mistake before myself, and I still managed not
to spot it. Well spotted guido :)
--
[ signature omitted ]