0

I am trying to create a connection to a database and insert/delete/make queries to the database. I know SQL relatively well but I cannot seem to wrap my head around it in Qt. I used to program in Delphi.

This is my code so far:

QSqlDatabase db;
db.addDatabase("QSQLITE");
db.setHostName( "localhost" ); //I don't know if i should include this the database is in the same directory as my program
db.setDatabaseName( "Xmato.odb" );
db.setUserName( "" ); //There is no username
db.setPassword( "" ); //There is no password
db.open();
db.prepare("SELECT * FROM Members");
db.exec();

I have added this to my .pro file:

QT += sql;

An included QtSql to my main file.

When I run this code I get the error:

QSqlQuery::prepare: database not open

Any ideas will me much appreciated.

P.S.: I use c++ on Linux Ubuntu 12.04 and used LibreOffice Base to create my database.

Armand Maree
  • 488
  • 2
  • 6
  • 21
  • `prepare()` is usually used when you're invoking a stored procedure and you want to pass through parameters later. What do you get if you delete the line with the `prepare()` statement and change your last statement to: `db.exec("SELECT * FROM Members");` ? – RobbieE Jun 21 '13 at 13:19
  • 1
    Does `db.open()` returns true, and if not, which indicates there is an error, what is the error description (returned by `db.lastError().text()`) ? For Sqlite, you can omit `setHostName`, `setUserName` and `setPassword`, these functions don't do anything. – alexisdm Jun 21 '13 at 13:20
  • @RobbieE I do use parameters in the program. I just simplified it for the question – Armand Maree Jun 21 '13 at 13:30
  • @alexisdm It says "Driver not loaded". What is the drivers and which one should I use? – Armand Maree Jun 21 '13 at 13:38
  • This looks like it might be some sort of Microsoft Access or OpenOffice/LibreOffice database, not SQLite. Check here for more information: http://hsqldb.org/. As far as I know, there is only a Java interface to this database back end. – Cameron Tinker Jun 21 '13 at 15:26

3 Answers3

2

After a bit of google-ing - openoffice libre's internal database is using HSQLDB (natural choice for Java). Here's a small discussion about HSQLDB.

It appears that some versions of openlibre base is also able to connect to external databases. I would recommend setting up something that is more accessible to C++, specifically Qt.

Only a few drivers like ODBC & SQLite is included by default.

Which means that depending on the database being used, one may need to get additional source code (or packages) and compile a plugin/dll/so. The library is loaded dynamically (i.e. run-time) by the QtSql module. I've run into this for mysql drivers.

When you get all of that setup, your call to addDatabase should match the kind of database you're using.

QSqlDatabase::addDatabase( "QODBC" );  // For something like MSSQL
QSqlDatabase::addDatabase( "QSQLITE" );  // For SQLite
QSqlDatabase::addDatabase( "QMYSQL" );  // For MySQL

Personally, if you're just doing this for kicks, a quick and easy database is SQLITE. You can even download plugins/extensions for Mozilla Firefox that will offer you a GUI to the database.

Community
  • 1
  • 1
Son-Huy Pham
  • 1,899
  • 18
  • 19
  • Thanks a lot. I understand a lot better now. I'll do some research, now that I know what to look for. Thanks again. – Armand Maree Jun 21 '13 at 17:27
0

Driver not loaded

you need the QSQLITE driver.

db.drivers() returns a list of all the available database drivers.

In Ubuntu 12.04 the driver for sqlite is in a package named libqt4-sql-sqlite.

But: is odb a sqlite database??

asclepix
  • 7,971
  • 3
  • 31
  • 41
  • i would say, you can use any name extension for the db file. it will just change the default app the os will use to open it, but since the structure is a sqlite database, it will cause some troubles naming it something like 'db.run' and try to install it – Zaiborg Jun 21 '13 at 21:04
0
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName( "Xmato.odb" );
if (db.open())
{
    QSqlQuery query(db); // if multiple connections used, without the `db` in constructor    will cause the query to use the default database (first opened and available one)
    query.exec("select * from members");
}

should do the same. username and password are not needed and since it is a file, you just have to use setDatabaseName to set what file you want to open.

Zaiborg
  • 2,492
  • 19
  • 28