diff --git a/DbFuncs/__pycache__/db.cpython-311.pyc b/DbFuncs/__pycache__/db.cpython-311.pyc new file mode 100644 index 0000000..d0038d9 Binary files /dev/null and b/DbFuncs/__pycache__/db.cpython-311.pyc differ diff --git a/DbFuncs/__pycache__/db_create.cpython-311.pyc b/DbFuncs/__pycache__/db_create.cpython-311.pyc new file mode 100644 index 0000000..9b15c6d Binary files /dev/null and b/DbFuncs/__pycache__/db_create.cpython-311.pyc differ diff --git a/DbFuncs/db.py b/DbFuncs/db.py new file mode 100644 index 0000000..98bf2ea --- /dev/null +++ b/DbFuncs/db.py @@ -0,0 +1,92 @@ +import sqlite3 +import datetime + +import os +import sys + +script_dir = os.path.dirname( __file__ ) +constants_dir = os.path.join( script_dir, '..', 'config' ) +sys.path.append(constants_dir) +import constants + +from model.Product import Product + + +# convert image to blob data +def convertToBlobData(filename): + with open(filename, 'rb') as file: + blobData = file.read() + return blobData + + +# insert into product table +def insertProduct(product): + try: + conn = sqlite3.connect(constants.dbPath) + cursor = conn.cursor() + + insert_query = """insert into products (name, image, count, price, modifiedAt) + values (?, ?, ?, ?, ?)""" + + imageBlob = convertToBlobData(product.image) + data = (product.name, imageBlob, product.count, product.price, datetime.datetime.now()) + + cursor.execute(insert_query, data) + + print("inserted successfully") + + conn.commit() + cursor.close() + + return True + except sqlite3.Error as error: + print('fail', error) + return False + finally: + if conn: + conn.close() + print('connection is closed') + return False + +# update product item +def updateProduct(id, count): + try: + conn = sqlite3.connect(constants.dbPath) + cursor = conn.cursor() + + update_query = '''update products set count = ? where id = ?''' + params = (count, id) + cursor.execute(update_query, params) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('fail', error) + finally: + if conn: + conn.close() + print('connection is closed') + +# get all products +def getProducts(): + try: + conn = sqlite3.connect(constants.dbPath) + cursor = conn.cursor() + + select_query = '''select id, name, count from products''' + cursor.execute(select_query) + records = cursor.fetchall() + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('fail', error) + records = [] + finally: + if conn: + conn.close() + print('connection is closed') + + return records diff --git a/DbFuncs/db_create.py b/DbFuncs/db_create.py new file mode 100644 index 0000000..c7712a5 --- /dev/null +++ b/DbFuncs/db_create.py @@ -0,0 +1,96 @@ +import sqlite3 +import datetime + +import os +import sys + +script_dir = os.path.dirname( __file__ ) +constants_dir = os.path.join( script_dir, '..', 'config' ) +sys.path.append(constants_dir) +import constants + + +# create product table +def createDb(): + try: + conn = sqlite3.connect(constants.dbPath) + cursor = conn.cursor() + + create_product_table_query = '''CREATE TABLE IF NOT EXISTS products ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + image BLOB NOT NULL, + count INTEGER NOT NULL, + price REAL NOT NULL, + modifiedAt timestamp + )''' + + # Create a users table + cursor.execute(create_product_table_query) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print("fail", error) + finally: + if conn: + conn.close() + print('connection is closed') + +def insertData(name, avatar, modifiedAt): + try: + # Connect to the database (create a new file if it doesn't exist) + conn = sqlite3.connect(constants.dbPath) + + # Create a cursor object to execute SQL commands + cursor = conn.cursor() + + # #drop table + # cursor.execute('drop table if exists clients') + # print('dropped successfully') + + create_user_table_query = '''CREATE TABLE IF NOT EXISTS clients ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + avatar BLOB NOT NULL, + modifiedAt timestamp + )''' + + # Create a users table + cursor.execute(create_user_table_query) + + print('Created successfully') + + # cursor.execute("PRAGMA table_info('clients')") + # columns = cursor.fetchall() + # print(columns) + + # Insert User into table + insert_user_query = """insert into clients (name, avatar, modifiedAt) + values (?, ?, ?)""" + avatarBlog = convertToBlobData(avatar) + insert_data = (name, avatarBlog, modifiedAt) + cursor.execute(insert_user_query, insert_data) + print('inserted successfully') + + # Get All Users + get_all_users_query = '''select id, name from clients''' + cursor.execute(get_all_users_query) + records = cursor.fetchall() + + print(records) + + + # Commit the changes and close the connection + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print("fail", error) + finally: + if conn: + conn.close() + print('connection is closed') + +# insertData("Smith", "D:\Workspace\Python\Kivy\Hello-kivy\img/1-1.png", datetime.datetime.now()) \ No newline at end of file diff --git a/DbFuncs/db_get_list_test.py b/DbFuncs/db_get_list_test.py new file mode 100644 index 0000000..f2243ce --- /dev/null +++ b/DbFuncs/db_get_list_test.py @@ -0,0 +1,33 @@ +import sqlite3 +import datetime + +import os +import sys + +script_dir = os.path.dirname( __file__ ) +constants_dir = os.path.join( script_dir, '..', 'config' ) +sys.path.append(constants_dir) +import constants + +conn = sqlite3.connect(constants.dbPath) +cursor = conn.cursor() + +# Get All Users +get_all_users_query = '''select id, name, price from products''' +cursor.execute(get_all_users_query) +records = cursor.fetchall() + +print(records) + +cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") + +# Fetch all the table names +tables = cursor.fetchall() + +for table in tables: + print(table[0]) + + +conn.commit() +cursor.close() +conn.close() diff --git a/DbFuncs/sql.db b/DbFuncs/sql.db new file mode 100644 index 0000000..e69de29 diff --git a/api.py b/api.py new file mode 100644 index 0000000..e69de29 diff --git a/config/__pycache__/constants.cpython-311.pyc b/config/__pycache__/constants.cpython-311.pyc new file mode 100644 index 0000000..ce4cdc3 Binary files /dev/null and b/config/__pycache__/constants.cpython-311.pyc differ diff --git a/config/constants.py b/config/constants.py new file mode 100644 index 0000000..76203e2 --- /dev/null +++ b/config/constants.py @@ -0,0 +1 @@ +dbPath = './DbFuncs/sql.db' \ No newline at end of file diff --git a/kv/list.kv b/kv/list.kv new file mode 100644 index 0000000..4fdf067 --- /dev/null +++ b/kv/list.kv @@ -0,0 +1,153 @@ +WindowManager: + ListScreen: + ItemScreen: + +: + name: 'List' + canvas.before: + Color: + rgba: 1, 1, 1, 1 # Set the color to red (RGBA format) + Rectangle: + pos: self.pos + size: self.size + + BorderImage: + size: self.size + pos: self.pos + source: '' + border: 10, 10, 10, 10 + Color: + rgba: 1, 0, 0, 1 # Set the color to red (RGBA format) + + GridLayout: + rows: 3 + BoxLayout: + padding: 10, 10, 10, 10 + size_hint: (1, 0.3) + Image: + source: './img/top.png' + size: self.texture_size + + GridLayout: + cols: 2 + rows: 2 + spacing: 30 + padding: 10 + Image: + source: './img/1-1.png' + size: self.texture_size + on_touch_down: root.select_item() + Image: + source: './img/1-2.png' + size: self.texture_size + Image: + source: './img/2-1.png' + size: self.texture_size + Image: + source: './img/2-2.png' + size: self.texture_size + + BoxLayout: + padding: 10, 10, 10, 10 + size_hint: (1, 0.3) + RelativeLayout: + Image: + source: './img/bottom.png' + size: self.texture_size + +: + name: 'Item' + + canvas.before: + Color: + rgba: 1, 1, 1, 1 # Set the color to red (RGBA format) + Rectangle: + pos: self.pos + size: self.size + + GridLayout: + rows: 3 + GridLayout: + cols: 2 + spacing: 10 + BoxLayout: + padding: 10 + RelativeLayout: + Image: + source: './img/1-1.png' + size: self.texture_size + GridLayout: + rows: 2 + GridLayout: + padding: 10, 50, 10, 10 + cols: 2 + spacing: 5 + GridLayout: + rows: 5 + spacing: 5 + Label: + text: 'Art.Nr:' + color: (0,0,0,1) + Label: + text: 'Züge:' + color: (0,0,0,1) + Label: + text: 'Nikotin:' + color: (0,0,0,1) + Label: + text: 'Akku:' + color: (0,0,0,1) + Label: + text: 'Tankvol.:' + color: (0,0,0,1) + GridLayout: + rows: 5 + spacing: 5 + Label: + text: 'ABC1234' + color: (0,0,0,1) + Label: + text: '600' + color: (0,0,0,1) + Label: + text: '20mg' + color: (0,0,0,1) + Label: + text: '400mAh' + color: (0,0,0,1) + Label: + text: '2ml' + color: (0,0,0,1) + GridLayout: + cols: 3 + spacing: 5 + Image: + source: './img/left.png' + size: self.texture_size + Label: + text: '10.00 EUR' + color: (0,0,0,1) + Image: + source: './img/right.png' + size: self.texture_size + + GridLayout: + cols: 2 + spacing: 20 + padding: 10, 20, 10, 20 + size_hint: (1, 0.3) + Button: + text: '20.00 Eur' + color: (0,0,0,1) + background_normal: './img/bg-btn-1.png' + background_color: (1,1,1,0.5) + Button: + text: 'Buy' + background_color: (0,0,1,1) + border_radius: [20] + + BoxLayout: + size_hint:(1, 1) + RelativeLayout: + + diff --git a/main.py b/main.py index c906e97..3589e7f 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,12 @@ from kivy.uix.screenmanager import ScreenManager, Screen from kivy.properties import StringProperty +from DbFuncs import db +from DbFuncs import db_create +from model.Product import Product + +dbFlag = False + class ListScreen(Screen): def select_item(self): app = App.get_running_app() @@ -16,16 +22,45 @@ class ItemScreen(Screen): class WindowManager(ScreenManager): pass -kv = Builder.load_file('list.kv') +kv = Builder.load_file('./kv/list.kv') class MainApp(App): # Main Application def build(self): Window.size = (600, 850) + sm = ScreenManager() sm.add_widget(ListScreen(name='List')) sm.add_widget(ItemScreen(name='Item')) + + + # create db and insert data + if dbFlag: + self.insertProduct() + + # get all products. + products = self.getProducts() + + # display products on the selling-screen + + return sm + + # create db and insert data + def insertProduct(self): + + db_create.createDb() + + names = ['1-1.png', '1-2.png', '2-1.png', '2-2.png', '1-1.png', '1-2.png', '2-1.png'] + path = 'D:\Workspace\Python\Kivy\Hello-kivy\img' + for name in names: + image = path + "/" + name + data = Product('111', image, 10, 100) + db.insertProduct(data) + + def getProducts(self): + products = db.getProducts() + return products if __name__ == '__main__': MainApp().run() \ No newline at end of file diff --git a/model/Product.py b/model/Product.py new file mode 100644 index 0000000..5d23d4b --- /dev/null +++ b/model/Product.py @@ -0,0 +1,6 @@ +class Product: + def __init__(self, name, image, count, price): + self.name = name + self.image = image + self.count = count + self.price = price \ No newline at end of file diff --git a/model/__pycache__/Product.cpython-311.pyc b/model/__pycache__/Product.cpython-311.pyc new file mode 100644 index 0000000..be2df19 Binary files /dev/null and b/model/__pycache__/Product.cpython-311.pyc differ