Results 1 to 6 of 6

Thread: Embedding binary data into an application

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Embedding binary data into an application

    I need to embed an sqlite database into my application. It's very small, only 2kb. I tried using a Qt resource file, but rcc chokes on it. Can rcc only embed pictures into an application? I can't see any way of actually CREATING a database through sql commands - Qt seems to need the database to physically exist to connect to

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Embedding binary data into an application

    Quote Originally Posted by Valheru
    I need to embed an sqlite database into my application.
    I remember reading a similar post by someone but I am not able to find it now. I don't think you can do that.

    Quote Originally Posted by Valheru
    I can't see any way of actually CREATING a database through sql commands - Qt seems to need the database to physically exist to connect to
    Try this :

    setDatabaseName will create a database if the file does not exists. Not sure, but I remember doing something similar.

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(path);//Path where you want the database
    3. if(db.open()){
    4. if(db.tables().count() != 0){
    5. return true;
    6. }
    7. else{
    8. if(createTables()){
    9. return true;
    10. }
    11. else{
    12. //Error Message
    13. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Embedding binary data into an application

    Quote Originally Posted by munna
    I remember reading a similar post by someone but I am not able to find it now. I don't think you can do that.



    Try this :

    setDatabaseName will create a database if the file does not exists. Not sure, but I remember doing something similar.

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(path);//Path where you want the database
    3. if(db.open()){
    4. if(db.tables().count() != 0){
    5. return true;
    6. }
    7. else{
    8. if(createTables()){
    9. return true;
    10. }
    11. else{
    12. //Error Message
    13. }
    To copy to clipboard, switch view to plain text mode 
    Meh, setDatabaseName() isn't creating anything - as I thought it wouldn't, since you are not specifying any layout for the database so the program doesn't know how to create the database.

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Embedding binary data into an application

    Below is the code that worked for me. First time the applicaion is launched, the database is created and then in createTables(), the required tables were created. From the second time onwards database is only opened.

    Qt Code:
    1. bool AddressBookDB::openDatabase()
    2. {
    3. QString homePath = QDir::homePath();
    4. homePath.append("/adressbook");
    5. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    6. db.setDatabaseName(homePath);
    7. if(db.open()){
    8. if(db.tables().count() != 0){
    9. return true;
    10. }
    11. else{
    12. if(createTables()){
    13. return true;
    14. }
    15. }
    16. }
    17. else{
    18. return false;
    19. }
    20. return false;
    21. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Embedding binary data into an application

    Quote Originally Posted by munna
    Below is the code that worked for me. First time the applicaion is launched, the database is created and then in createTables(), the required tables were created. From the second time onwards database is only opened.

    Qt Code:
    1. bool AddressBookDB::openDatabase()
    2. {
    3. QString homePath = QDir::homePath();
    4. homePath.append("/adressbook");
    5. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    6. db.setDatabaseName(homePath);
    7. if(db.open()){
    8. if(db.tables().count() != 0){
    9. return true;
    10. }
    11. else{
    12. if(createTables()){
    13. return true;
    14. }
    15. }
    16. }
    17. else{
    18. return false;
    19. }
    20. return false;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Sorry, could I trouble you to post the contents of createTables()?

  6. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Embedding binary data into an application

    The createTables() function is too big. Here is a part of it

    Qt Code:
    1. QSqlQuery query;
    2.  
    3. if(query.exec("create table cgroup(id INTEGER PRIMARY KEY,name TEXT,category INTEGER,operator TEXT)")){
    4. if(query.exec("insert into cgroup (id,name,category) values (NULL,'All',1)")){
    5. if(query.exec("select * from cgroup")){
    6. if(query.first()){
    7. QSqlRecord r = query.record();
    8. gid = r.value("id").toInt();
    9. }
    10. }
    11. if(query.exec("create table group_contacts(id INTEGER PRIMARY KEY,"
    12. "nameid INTEGER, groupid INTEGER, emailid INTEGER, phoneid INTEGER, addressid INTEGER)")){
    13. if(query.exec("insert into group_contacts(id,nameid,groupid) "
    14. "values (NULL, " + QString::number(nid) + ", " + QString::number(gid) +
    15. ")")){
    16.  
    17. }
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Writing Data Aware Forms in QT4.
    By gsQT4 in forum Qt Programming
    Replies: 5
    Last Post: 21st March 2007, 11:35
  2. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 13:53
  3. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 17:17
  4. Replies: 16
    Last Post: 7th March 2006, 16:57
  5. xml with binary question
    By TheKedge in forum Qt Programming
    Replies: 7
    Last Post: 13th January 2006, 00:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.