-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataBaseManager.py
More file actions
executable file
·191 lines (167 loc) · 6.83 KB
/
DataBaseManager.py
File metadata and controls
executable file
·191 lines (167 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import sqlite3
from sqlite3 import Error
conn = 0
def createDataBase():
""" create a database connection to a SQLite database """
global conn
try:
conn = sqlite3.connect("MyDataBase.db")
c= conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS Stocks '
'(Item_Id INTEGER PRIMARY KEY AUTOINCREMENT,'
'Name text,'
'Quantity INTEGER,'
'Barcode text,'
'picture text,'
'buying_price INTEGER,'
'sale_price INTEGER,'
'profit INTEGER)')
# c.execute('INSERT INTO Item VALUES (1,"Asif",5,"barcode","picture",10,20,10)')
c.execute('CREATE TABLE IF NOT EXISTS Bills '
'(Bill_Id INTEGER PRIMARY KEY AUTOINCREMENT,'
'today_date Date ,'
'Profit INTEGER,'
'Sale_earn INTEGER,'
'Discount INTEGER,'
'Lended_amount INTEGER,'
'customer_phone_number text,'
'customer_name text)')
conn.execute('CREATE TABLE IF NOT EXISTS Sales '
'(Sale_Id INTEGER PRIMARY KEY AUTOINCREMENT,'
'Billid INTEGER,'
'Itemid INTEGER,'
'ItemName text,'
'Item_quantitty INTEGER,'
'Item_total INTEGER,'
'Item_profit INTEGER,'
'Item_price INTEGER,'
'CONSTRAINT Sales_fk'
' FOREIGN KEY(Itemid) REFERENCES Stocks(Item_Id)'
# ' FOREIGN KEY(Billid) REFERENCES Bills(Bill_Id)'
')')
conn.execute('CREATE TABLE IF NOT EXISTS Admin (UserName INTEGER ,Password text, IsLogin Boolean) ')
conn.execute('CREATE TABLE IF NOT EXISTS Customers (C_Name text ,Due_Amount INTEGER,PhoneNumber text NOT NULL UNIQUE , Address text)')
conn.execute('CREATE TABLE IF NOT EXISTS Dealers (D_Name text ,Due_Amount INTEGER,Spend_Amount INTEGER,PhoneNumber text, Address text)')
conn.execute('CREATE TABLE IF NOT EXISTS Ledger (customerName text, receivingDate date, ReceivedAmount INTEGER, Due_Amount INTEGER,Note text)')
conn.execute("INSERT INTO Bills(Bill_Id,today_date,Profit,Sale_earn) VALUES (0,'date',0,0)")
# conn.execute('CREATE TABLE IF NOT EXISTS BillList (BillNumber INTEGER,'
# 'Name text, '
# 'Quantity INTEGER, '
# 'Price INTEGER, '
# 'Total INTEGER ) ')
conn.commit()
c.close()
# conn.close()
except Error as e:
print(e)
finally:
conn.close()
def GetConnection():
global conn
try:
conn = sqlite3.connect("MyDataBase.db")
c = conn.cursor()
return conn,c
except Error as e:
print(e)
def AddData():
global conn
try:
conn = sqlite3.connect("MyDataBase.db")
c = conn.cursor()
# conn.execute('CREATE TABLE IF NOT EXISTS Customers (C_Name text ,Due_Amount INTEGER,PhoneNumber text, Address text)')
# conn.execute('CREATE TABLE IF NOT EXISTS Dealers (D_Name text ,Due_Amount INTEGER,Spend_Amount INTEGER,PhoneNumber text, Address text)')
# row = c.fetchall()
# c.execute('DROP TABLE IF EXISTS Bills')
# c.execute('ALTER TABLE Bills ADD COLUMN Lend_date Date DEFAULT 0')
# 'borrower_Id INTEGER default 0')
# c.execute('DROP TABLE IF EXISTS Sales')
conn.commit()
c.close()
conn.close()
except Error as e:
print(e)
finally:
conn.close()
# createDataBase()
def print_stocks():
global conn
try:
conn = sqlite3.connect("MyDataBase.db")
c = conn.cursor()
c.execute("Select * from Stocks")
rows = c.fetchall()
for row in rows:
print(row)
c.close()
conn.close()
except Error as e:
print(e)
finally:
conn.close()
def print_bills():
global conn
try:
conn = sqlite3.connect("MyDataBase.db")
c = conn.cursor()
# conn.execute("INSERT INTO Bills(Bill_Id,today_date,Profit,Sale_earn) VALUES (0,'date',0,0)")
# conn.commit()
# c.execute("Select * from Customers") #
c.execute("Select * from Bills ") # WHERE borrower_Id != '0'
# c.execute("Update Bills SET Sale_earn = Sale_earn -310, Profit = Profit - 310 Where Bill_Id = 6") #Select * from Bills
# conn.commit()
rows = c.fetchall()
for row in rows:
print(row)
c.close()
conn.close()
except Error as e:
print(e)
finally:
conn.close()
def print_sales():
global conn
try:
conn = sqlite3.connect("MyDataBase.db")
c = conn.cursor()
# c.execute(f'SELECT Itemid, sum(Item_total),'
# f'sum(Item_profit)'
# f' from (SELECT * From Sales WHERE Billid IN (SELECT Bill_Id from Bills where today_date = date("2019-01-23"))) '
# # f'INNER JOIN Stocks ON Sales.Itemid = Stocks.Name'
# # f'WHERE Billid = (SELECT Bill_Id from Bills where today_date = date("2019-01-23"))'
# f'GROUP BY Itemid')
# c.execute(f'SELECT Billid, Itemid,Name,sum(Item_total),'
# f'sum(Item_profit)'
# f' from Stocks'
# # f'(SELECT * From Sales WHERE Billid IN (SELECT Bill_Id from Bills where today_date = date("2019-01-23"))) '
# f' INNER JOIN (SELECT * From Sales WHERE Billid IN (SELECT Bill_Id from Bills where today_date = date("2019-01-23"))) ON Itemid = Item_Id'
# f' GROUP BY Itemid')
# f'WHERE Billid = (SELECT Bill_Id from Bills where today_date = date("2019-01-23"))')
# f'GROUP BY Item_Id')
id = 3
billNumber = 6
# c.execute(f"Delete from Sales WHERE Itemid = {id} and Billid = {billNumber}")
# c.execute(f'SELECT Stocks.Name,Sales.Item_quantitty,Sales.Item_total,Sales.Item_profit'
# f' from Sales, Stocks'
# f' WHERE Billid = {3} and Item_Id = Itemid')
# c.execute(f"Select * from Sales Where Billid = {1}")
# c.execute('SELECT Bill_Id FROM Bills WHERE today_date = date("2019-01-23")')
# list = [3,4]
c.execute(f'SELECT * From Customers ') #WHERE Billid IN (SELECT Bill_Id from Bills where today_date = date("2019-01-23"))
rows = c.fetchall()
for row in rows:
print(row)
c.close()
conn.close()
except Error as e:
print(e)
finally:
conn.close()
def main():
# AddData()
createDataBase()
# print_stocks()
# print_bills()
# print_sales()
# conn.commit()
main()