Results 1 to 2 of 2

Thread: Accessing sql local storage from C++?

  1. #1

    Default Accessing sql local storage from C++?

    Hi,

    I'd like to be able to manipulate SQL databases created with openDatabaseSync in a C++ component.
    The goal is to use a database filled in a C++ component as a model for QML views

    Would passing the db connection created in javascript to a QObject as a property work?

  2. #2
    Join Date
    Feb 2014
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Accessing sql local storage from C++?

    There wasn't a great answer and this came up on a lot of searches so I'm responding 3 years after the fact in-case someone else comes looking.

    (I'm using Qt 5.2 with LocalStorage 2.0)


    I just used an external variable to store the root:
    Qt Code:
    1. QtQuick2ApplicationViewer viewer;
    2. viewer.setMainQmlFile(QStringLiteral("qml/LightAssistant/main.qml"));
    3. viewer.showExpanded();
    4. LA::OFFLINE_STORAGE_PATH = viewer.engine()->offlineStoragePath();
    5. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    You can then iterate over the databases in the folder:
    OFFLINE_PATH/Databases/$hash$.ini

    (something like this) --I'm using QString dbPath;

    Qt Code:
    1. for(int i = 0; i < list.size() && !endLoop; i++)
    2. {
    3. QFileInfo file = list.at(i);
    4. if(file.fileName().endsWith(".ini"))
    5. {
    6. QFile ini(file.absoluteFilePath());
    7. if (!ini.open(QIODevice::ReadOnly | QIODevice::Text))
    8. continue;
    9.  
    10. while(!ini.atEnd())
    11. {
    12. QString line = ini.readLine();
    13. if(line.contains("myDbName")) //probably could be better
    14. {
    15. dbPath= file.absoluteFilePath().replace(".ini", ".sqlite");
    16. endLoop = true;
    17. }
    18. }
    19.  
    20. ini.close();
    21. }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    Look for the "name=databaseName" in each ini and then you can pass it to the local SQLITE connection which is as simple as:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(dbPath);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to danielbchapman for this useful post:

    anda_skoa (28th February 2014)

Similar Threads

  1. Webkit and Client Side Storage
    By bythos in forum Qt Programming
    Replies: 2
    Last Post: 23rd June 2010, 15:37
  2. MySQL currency storage problem
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2008, 10:04
  3. Storage and View -> imageViewer
    By C167 in forum Qt Programming
    Replies: 15
    Last Post: 22nd January 2008, 15:43
  4. blocks, storage and objects
    By mickey in forum General Programming
    Replies: 6
    Last Post: 6th December 2007, 18:55
  5. What is the format of QByteArray's storage?
    By LiCodeX in forum Newbie
    Replies: 4
    Last Post: 31st October 2007, 10: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.