This commit is contained in:
2025-09-28 17:05:51 +08:00
parent 8eb80ab66d
commit d97fa3e0fe
398 changed files with 18737 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
# Copyright (c) Daniel Gakwaya.
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.16)
project(5-SQLite VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick QuickControls2)
qt_standard_project_setup(REQUIRES 6.5)
qt_add_executable(app5-SQLite
main.cpp
)
qt_add_qml_module(app5-SQLite
URI 5-SQLite
VERSION 1.0
QML_FILES Main.qml Database.js
)
set_target_properties(app5-SQLite PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(app5-SQLite
PRIVATE Qt6::Quick Qt6::QuickControls2
)
install(TARGETS app5-SQLite
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

View File

@@ -0,0 +1,79 @@
// Copyright (c) Daniel Gakwaya.
// SPDX-License-Identifier: MIT
function dbInit(){
console.log(" Initializing database...")
db = LocalStorage.openDatabaseSync("sqlitedemodb", "1.0", "SQLite Demo database", 100000);
db.transaction( function(tx) {
print('... create table')
tx.executeSql('CREATE TABLE IF NOT EXISTS sqlitedemotable(name TEXT, value TEXT)');
});
}
function storeData(){
console.log(" Storing data...")
//Check if the database was ever created
if (!db){
return ;
}
db.transaction(function(tx){
//Check if sqlitedemo entry is available in database table
var result = tx.executeSql('SELECT * from sqlitedemotable where name = "sqlitedemo"');
//Prepare json object data from qml code
var obj = { x: rootId.x, y: rootId.y,
width : rootId.width,height : rootId.height,
colorred : rectId.color.r,colorgreen : rectId.color.g ,
colorblue : rectId.color.b };
if ( result.rows.length ===1 ){
//Update
console.log("Updating database table...")
result = tx.executeSql('UPDATE sqlitedemotable set value=? where name="sqlitedemo"',
[JSON.stringify(obj)])
}else{
//Create entry
console.log("Creating new database table entry")
result = tx.executeSql('INSERT INTO sqlitedemotable VALUES (?,?)',
['sqlitedemo', JSON.stringify(obj)])
}
});
}
function readData(){
console.log(" Reading data...")
if (!db){
return ;
}
db.transaction( function(tx) {
print('... Reading data from database')
var result = tx.executeSql('select * from sqlitedemotable where name="sqlitedemo"');
if(result.rows.length === 1){
//We have data that we can work with
// get the value column
var value = result.rows[0].value;
// convert to JS object
var obj = JSON.parse(value)
// apply to object
rootId.x = obj.x;
rootId.y = obj.y;
rootId.width= obj.width;
rootId.height = obj.height
rectId.color= Qt.rgba(obj.colorred,obj.colorgreen,obj.colorblue,1)
}
});
}

View File

@@ -0,0 +1,51 @@
// Copyright (c) Daniel Gakwaya.
// SPDX-License-Identifier: MIT
import QtQuick
import QtQuick.LocalStorage
import QtQuick.Dialogs
import "Database.js" as JS
Window {
id: rootId
visible: true
width: 640
height: 480
title: qsTr("SQLite")
property var db ;
Rectangle{
id: rectId
anchors.fill: parent
color: "red"
MouseArea{
anchors.fill: parent
onClicked: {
colorDialogId.open()
}
ColorDialog {
id: colorDialogId
title: "Please choose a color"
onAccepted: {
console.log("The new color is: "+ selectedColor)
rectId.color = selectedColor
}
onRejected: {
console.log("Canceled")
}
}
}
}
Component.onCompleted: {
//Read data
JS.dbInit()
JS.readData()
}
Component.onDestruction: {
JS.storeData()
}
}

View File

@@ -0,0 +1,22 @@
// Copyright (c) Daniel Gakwaya.
// SPDX-License-Identifier: MIT
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickStyle>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickStyle::setStyle("Basic");
QQmlApplicationEngine engine;
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
&app, []() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.loadFromModule("5-SQLite", "Main");
return app.exec();
}