February 24, 2011

Matze5590 Matze5590
Lab Rat
44 posts

SQLITE Database in Project

Page  
1

Hello everybody,

i am trying to load data from an sqlite database.
with the simulator it is possible to open the db when i use the following, but then the db is stored on “C:” and not in my projects folder. so i think that it would not work on device without copying the db manually.

  1. db = QSqlDatabase::addDatabase("QSQLITE");
  2. db.setDatabaseName("/db.sqlite");

because of that i tried to add it to my ressources like a picture:

  1. db = QSqlDatabase::addDatabase("QSQLITE");
  2. db.setDatabaseName(":/database/db.sqlite");

(of course added in the .qrc file)

but with this code its not working on device as well as with the simulator, because “open” fails.
I think the App can’t find the db, but why?

Thanks you in advance!

18 replies

February 24, 2011

Volker Volker
Robot Herder
5428 posts

SQLite databases cannot be stored in the Qt resource system. You must put it into regular files and distribute them along your application.

The reason is simple: The database name is just the path to the file. Qt does not intercept the file name but hands it over directly to the SQLite functions (by calling toUtf8() on the “path”), which in turn try to open that file – and will eventually fail, of course.

February 24, 2011

Matze5590 Matze5590
Lab Rat
44 posts

Would it be possible with another kind of database? for example mysql or mssql? or is it generally not possible to store db’s in qt resources?

February 24, 2011

Volker Volker
Robot Herder
5428 posts

No, it would not be possible with other database too. Even if you’re using some embedded database (mysql comes into mind), these would suffer from the same problem, that Qt does not intercept the path.

February 24, 2011

Matze5590 Matze5590
Lab Rat
44 posts

so there’s no way to use databases with qt on mobile device?
And i have to use for example .csv ?

February 24, 2011

Volker Volker
Robot Herder
5428 posts

Provide the database file in your application bundle. I’m pretty sure you can have those in every mobile environment. Also, keep in mind that a file in a resource is read-only, you would never be able to write to that database.

February 24, 2011

Matze5590 Matze5590
Lab Rat
44 posts

ok, thanks for info!
but now i have a problem!
how can i use the information in the file/db in my app?
i’d like to read and write to that file/db and want to bring the file/db with the app, so that i don’t need to copy it on device manually.
or is there another way to add it to my project? without the .qrc?

February 24, 2011

Volker Volker
Robot Herder
5428 posts

As far as I know it’s possible to add this to some settings in the qmake project files, at least for symbian. But I have no experience on this. Please use the forum search and/or Google – I’m sure this was asked before.

February 24, 2011

Matze5590 Matze5590
Lab Rat
44 posts

Here’s the solution for everybody who’s interested in:

use:

  1. db = QSqlDatabase::addDatabase("QSQLITE");
  2. db.setDatabaseName("db.sqlite");

and add this to your .pro file:

  1. symbian {
  2. ...
  3.     addFiles.sources = db.sqlite
  4.     addFiles.path = .
  5.     DEPLOYMENT += addFiles
  6. ...
  7. }

Thanks to Volker!

May 17, 2011

vsh.vshnu vsh.vshnu
Lab Rat
6 posts

Hi All,
I have tried in the same way.But i didnt get it.
Basically i am developing the meego application in windows dev enviornment using Qt creator.

Following are my pro file contents,

  1. QT       += core gui sql
  2. TARGET = AppName
  3. TEMPLATE = app
  4. target.path=/usr/local/bin
  5. INSTALLS=target
  6. SOURCES += ---\
  7.                   ---
  8. HEADERS  += ---\
  9.                   ---\
  10. FORMS    += ---\
  11.                   ---
  12. RESOURCES += \
  13.         assests.qrc
  14. addFiles.sources =test.sqlite
  15. addFiles.path = .
  16. DEPLOYMENT += addFiles

So when i tried to build the file, automatically the other folde is added into my project directory and showing test.sqlite inside it;

But when i tried with the following code

  1. db = QSqlDatabase::addDatabase("QSQLITE");
  2. db.setDatabaseName(dbname);
  3. if(db.open())
  4. {
  5. //   ITs always true;in the sense it automatically creates a new database name test.sqlite in the /usr/local/bin folder
  6. }

Please give some suggestions over this issue.

Thanks,
Vsh.Vshnu

[EDIT: code formatting, please use @-tags, not stars (*), Volker]

May 17, 2011

leon.anavi leon.anavi
Mad Scientist
1046 posts

Hi Vsh.Vshnu,

Follow the SQLite examples given in Forum Nokia:
Creating an SQLite database in Qt [wiki.forum.nokia.com]
Searching for data in a database in Qt [wiki.forum.nokia.com]

Best regards,
Leon

 Signature 

http://anavi.org/

May 17, 2011

vsh.vshnu vsh.vshnu
Lab Rat
6 posts

leon.anavi wrote:
Hi Vsh.Vshnu,

Follow the SQLite examples given in Forum Nokia:
Creating an SQLite database in Qt [wiki.forum.nokia.com]
Searching for data in a database in Qt [wiki.forum.nokia.com]

Best regards,
Leon

Hi leon.anavi,
My problem is different.Its becasuse i am not able to open a existing sqlite database inside project main folder.
I want the sqlite files to be inside my application package file??

May 17, 2011

Volker Volker
Robot Herder
5428 posts

Then, what is the path stored in variable dbname?

May 17, 2011

vsh.vshnu vsh.vshnu
Lab Rat
6 posts

Hi Volker,
Its simply
QString dbName=“test.sqlite”;

Iam developing for meego in windows developement enviorment using Qt creator

May 17, 2011

Volker Volker
Robot Herder
5428 posts

the target path of your app is /usr/local/bin, the path of your db is test.sqlite – so the complete path is /usr/local/bin/test.sqlite. Sounds quite ok for me (in regard to the setup, a database file in /usr/local/bin is a no-go!).

Do you have a test.sqlite that actually is packed in your setup?

And be aware, the packing options are Symbian only!. It is ignored for any other operating systems, including Meego! It is for reason, that Matze5590 has put them into the symbian scope!

May 17, 2011

leon.anavi leon.anavi
Mad Scientist
1046 posts

vsh.vshnu wrote:
Hi Volker,
Its simply
QString dbName=“test.sqlite”;

Iam developing for meego in windows developement enviorment using Qt creator

Try to use the absolute path. You can get the file path of the application executable using QCoreApplication::applicationFilePath [doc.qt.nokia.com]

Cheers,
Leon

 Signature 

http://anavi.org/

Page  
1

  ‹‹ QNetworkConfigurationManager doesn’t work in Symbian^3 with Qt 4.7.1!!      error LNK2001 ››

You must log in to post a reply. Not a member yet? Register here!