PDA

View Full Version : Create DB & Write to DB



jaykappy
13th September 2019, 14:07
I am trying to create a database locally (right now on desktop and later to a phone)

Using this website: https://mimecar.gitbook.io/qt-course/en/chapter-05-advanced/chapter-05-s01

I understand the SQL statements create table, INSERT, FOR loop etc....
My issue is I cant see where the database is being created. I need a physical DB on my devise that I can write too....Eventually using those records to update a database on my server.

Right now I just need help:
1. creating a Database IF NOT THERE and creating it in a specific location
2. The ability to write records to that database
3. And view the database records


Can anyone please help....




// SNIP

import QtQuick.LocalStorage 2.0

// SNIP

var db = LocalStorage.openDatabaseSync(weatherRecorder_db, "1.0", "StorageDatabase", 1000000);
db.transaction(
function(tx) {
// Create the database if it doesn't already exist
tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)');

// Add (another) greeting row
tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]);

// Show all added greetings
var rs = tx.executeSql('SELECT * FROM Greeting');

var r = ""
for(var i = 0; i < rs.rows.length; i++) {
r += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + "\n"
}
text = r
console.log(text);
}
)

d_stranz
13th September 2019, 17:05
According to the docs for QtQuick LocalStorage (https://doc.qt.io/qt-5/qtquick-localstorage-qmlmodule.html):


These databases are user-specific and QML-specific, but accessible to all QML applications. They are stored in the Databases subdirectory of QQmlEngine:: offlineStoragePath(), currently as SQLite databases.

So examine the contents of that variable to see where the DB is physically located. If the DB is actually in SQLite format, then you can use any SQLite-based reader app to read / write them.

jaykappy
13th September 2019, 17:53
Can I specify where I want it to go?

d_stranz
14th September 2019, 00:39
Why don't you read the documentation for QQmlEngine and find the answer?