PDA

View Full Version : SQLite in QT



sophister
8th April 2009, 12:24
I want to create a table in an existed database, and I have usccessfully opened the database.
How can I use the QSqlQuery::exec( QString& ) to create a table with the following fields:
id, INTEGER, the primary key
name, VARCHAR(90)
author, VARCHAR(50)
copy, INTEGER
press, VARCHAR(120)

AND, the table's name is a varible named "tableName".

Thank you very much!!

wysota
8th April 2009, 13:00
http://www.sqlite.org/lang_createtable.html

sophister
8th April 2009, 13:11
I'm sorry, but what do you mean??
I think I am asking a Qt related question, because I want to use SQLite in Qt, but it doesn't work well.

spirit
8th April 2009, 13:14
you have to prepare correct SQL-query for a table creation and wysota posted a link how to do this in right way.
EDITED: take a look at connection.h which is located in QTDIR/examples/sql.

sophister
8th April 2009, 14:11
I use this to create a table whick is a variable "tableName", why the following codes doesn't woek:

QSqlQuery build;
build.exec( QString("create table %1 ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name VARCHAR(90)"
"author VARCHAR(50)"
"copy INTEGER"
"press VARCHAR(120))").arg( name) )

and before these codes, I have open the database.
Thanks in advance.

spirit
8th April 2009, 14:13
what kind of error do you get? maybe this table already exists?

sophister
8th April 2009, 14:26
I don't know, I jusr use the following codes:

QSqlQuery build;
bool ok = build.exec( QString("create table %1 ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name VARCHAR(90)"
"author VARCHAR(50)"
"copy INTEGER"
"press VARCHAR(120))").arg( name) )
if( !ok )
QMessageBox::information(this, "Fail", "Fail to create new table!!");

But I am sure the table doesn't exit.
puzzled.

spirit
8th April 2009, 14:28
modify you code like this


QSqlQuery build;
bool ok = build.exec( QString("create table %1 ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name VARCHAR(90)"
"author VARCHAR(50)"
"copy INTEGER"
"press VARCHAR(120))").arg( name) )
if( !ok )
QMessageBox::information(this, "Fail", build.lastError().text());

and show us a result.

sophister
8th April 2009, 14:29
Oh, I have solved it !!
I miss the comma among the fields!!
Thank you for your replying.

spirit
8th April 2009, 14:31
ok, but anyway, use code which I posted above for determination errors, it will help you in a future.

wysota
8th April 2009, 14:36
Oh, I have solved it !!
I miss the comma among the fields!!
Thank you for your replying.

So after all your question was not related to Qt but to a proper SQL statement :cool:

sophister
8th April 2009, 16:09
Uh, yeah, you are right maybe.
I learn a lot from this forum.