Skip to content

Commit

Permalink
finish_db_control
Browse files Browse the repository at this point in the history
  • Loading branch information
vanguardmaster01 committed Sep 7, 2023
1 parent ccfbaf0 commit 1908bd7
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 1 deletion.
Binary file added DbFuncs/__pycache__/db.cpython-311.pyc
Binary file not shown.
Binary file added DbFuncs/__pycache__/db_create.cpython-311.pyc
Binary file not shown.
92 changes: 92 additions & 0 deletions DbFuncs/db.py
Original file line number Diff line number Diff line change
@@ -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
96 changes: 96 additions & 0 deletions DbFuncs/db_create.py
Original file line number Diff line number Diff line change
@@ -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())
33 changes: 33 additions & 0 deletions DbFuncs/db_get_list_test.py
Original file line number Diff line number Diff line change
@@ -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()
Empty file added DbFuncs/sql.db
Empty file.
Empty file added api.py
Empty file.
Binary file added config/__pycache__/constants.cpython-311.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions config/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dbPath = './DbFuncs/sql.db'
153 changes: 153 additions & 0 deletions kv/list.kv
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
WindowManager:
ListScreen:
ItemScreen:

<ListScreen>:
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

<ItemScreen>:
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:


Loading

0 comments on commit 1908bd7

Please sign in to comment.