[SOLVED] Accessing to an existing SQLite DataBase from Qt
-
Hi everybody,
I'm trying to read an existing SQLite database from my QML applicattion,
right now, I have the database file imported to my project but I'm unable to acces to it, has somebody knows how to do it?Thank you very much
-
Hi everybody,
I'm trying to read an existing SQLite database from my QML applicattion,
right now, I have the database file imported to my project but I'm unable to acces to it, has somebody knows how to do it?Thank you very much
Hi @carles.sole.grau
Are you getting any specific errors ? Did you open the database from your code?
There's an example which explains it:
http://doc.qt.io/qt-5/qtquick-localstorage-qmlmodule.html -
Hi, @p3c0
Yes I looked this example and so far, I know how to create a SQLite database and insert and "read" data from it.But now, I'm interested in getting information from a external DataBase, so I would like to import it into my projecte to work with it.
Thank you
-
Hi, @p3c0
Yes I looked this example and so far, I know how to create a SQLite database and insert and "read" data from it.But now, I'm interested in getting information from a external DataBase, so I would like to import it into my projecte to work with it.
Thank you
But now, I'm interested in getting information from a external DataBase, so I would like to import it into my projecte to work with it.
Can you elaborate ?
-
@p3c0 ,
Sorry for not knowing the meaning, but what exactly are you meaning with "Elaborate"? -
@p3c0 ,
Sorry for not knowing the meaning, but what exactly are you meaning with "Elaborate"?@carles.sole.grau
Thats fine. Can you give more details of what you are trying to do ? -
@p3c0 ,
ups... jaja
Of course,
Right now i have a SQLite database that it has made using SQLite DatabaseBrowser, its extensions is .sqlite.For my project, I'm interested to have access to this database, to read the content so:
*I add this file to my QML project
*I'm trying to using it using the Javascript functionsBut I don't know how to make the program able to read (or to access) to this database.
Thank you
-
@p3c0 ,
ups... jaja
Of course,
Right now i have a SQLite database that it has made using SQLite DatabaseBrowser, its extensions is .sqlite.For my project, I'm interested to have access to this database, to read the content so:
*I add this file to my QML project
*I'm trying to using it using the Javascript functionsBut I don't know how to make the program able to read (or to access) to this database.
Thank you
@carles.sole.grau Sorry you can't do that using purely js or QML function.s You will need C++ for that. For eg. using
QSqlQueryModel
. -
And any example of that?
Thank you
-
And any example of that?
Thank you
@carles.sole.grau
Here's one:
https://wiki.qt.io/How_to_Use_a_QSqlQueryModel_in_QMLJust needs some minor modifications to make it work with Qt Quick 2.x
-
Thank you,
I will try it!! -
Finally, I could solve my problem, all the things I have had to do are:
In main.cpp I add this code:
QDir dir("./Databases"); if (!dir.exists()) { dir.mkpath("."); } QString new_name = QString(QCryptographicHash::hash(("nameofthecopiedDB"),QCryptographicHash::Md5).toHex()); QFile file(":/SQLite/nameofsourceDB.sqlite"); file.copy("./Databases/" + new_name + ".sqlite"); file.close();
Since I have the DB I would to use in my project in SQLite folder from my resources (:, indicates resources).
The QCryptographicHash::hash function is necessary because in the:openDatabaseSync(string name, string version, string description, int estimated_size, jsobject callback(db))
the name is passed with md5 codification: "the path to a database is QQmlEngine::offlineStoragePafh + md5(name) + ".sqlite". Where name is 1st parameter of openDatabaseSync"
And then, in QML file, the openDatabaseSync() function:
db = Sql.LocalStorage.openDatabaseSync('nameofthecopiedDB',"1.0","description",1000000,"QSQLITE")
Thank you!