SQLite with QT SDK 1.1
-
Hi.
I hope all of you will be fine.
I want to use sqlite with QT. I am in some confusion while using it. I have some questions.
1- How to use sqlite bundled with QT?
2- How to use sqlite if downloaded from sqlite website?
3- Which files to bundle with the application if sqlite is used?Thanks.
Regards,
Khalid Mushtaq -
I don't remember if sqlite plugin is build in the standard installation. Otherwise you can see this link "http://doc.trolltech.com/4.7/sql-driver.html#qsqlite":http://doc.trolltech.com/4.7/sql-driver.html#qsqlite for build it.
If you are in windows remember to deploy the sqlite lib, specially sqlite3.dll QtSql4.dll and the folder sqldriver that include qsqlite4.dll (i suppose deploy release version).
-
Thanks stuk.
Actually I want to ask that I downloaded the sqlite dll from http://www.sqlite.org/ now how to use it? How I can create a database in that file and how to use that database in QT and where to place that file?
Hope you will get the point what I am trying to ask.
Regards,
Khalid Mushtaq -
There is no needance in dll from official sqlite site. Qt has own SQLite support. You can read more about it in Assistant in QtSql section.
-
Ok.
Then please tell me what to use in setHostName, setDatabase, username, password, port etc while connecting the database?
How can I create a database in that SQLite?
Regards,
Khalid Mushtaq -
Just fill databaseName with path to your sqlite database file and you will get a connection :)
-
Hostname, username and password are not needed for sqlite.
Database name is the path to your database file. If you do not provide an absolute path, it is relative to your current working directory (the directory where your application executable lives, if you did not change it).
The following snippet creates a sql database in file "testdb.sqlite" in directory "/tmp", and creates a test table in it.
@
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
db.setDatabaseName("/tmp/testdb.sqlite");
db.open();
qDebug() << db << db.isOpen();
QSqlQuery q = db.exec("CREATE TABLE blurb ( id int, name varchar )");
qDebug() << q.lastError();
db.close();
@Be aware, that the exec fails on a second and subsequent run of the snippet, due to the fact that the table already exists then.
-
SQLite has the very nice CREATE TABLE IF NOT EXISTS statement.
-
[quote author="peppe" date="1296907715"]SQLite has the very nice CREATE TABLE IF NOT EXISTS statement.
http://www.sqlite.org/lang_createtable.html[/quote]
Nice! Good to know that. Thank's Peppe!
-
Thanks. Now I got the point:-)