Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. import sqlite3
  2.  
  3. # creating db file and connecting with python
  4. conn = sqlite3.connect('ShoppingMall.db')
  5. cursor = conn.cursor()
  6.  
  7. cursor.execute('drop table if exists CUSTOMERS')
  8. cursor.execute('drop table if exists ORDERS')
  9. conn.commit()
  10.  
  11. # Practice to create tables
  12.  
  13. #varchar(n) : allow n characters
  14. sql_command = '''
  15. CREATE TABLE CUSTOMERS (
  16. ID varchar(20) not null,
  17. Name varchar(20) not null,
  18. Rating VARCHAR(10) not null,
  19. Reserves int default 0,
  20. Phone varchar(11) not null,
  21. Address varchar(50),
  22. Primary key(ID) ) ; '''
  23.  
  24. cursor.execute(sql_command)
  25.  
  26. conn.commit()
  27.  
  28. sql_command = '''
  29. CREATE TABLE ORDERS (
  30. OrderNumber int(10) not null,
  31. ID varchar(20) not null,
  32. ProdName varchar(20) not null,
  33. Quantity int not null,
  34. UnitPrice int not null,
  35. OrderDate Date,
  36. primary key(OrderNumber) ) ; '''
  37. cursor.execute( sql_command )
  38.  
  39. conn.commit()
  40.  
  41. # Inserting data
  42.  
  43. sql_command = '''
  44. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  45. VALUES('ri902', 'Rick', 'Gold', '30000', '2178001000', 'Chicago')
  46. '''
  47. cursor.execute(sql_command)
  48.  
  49. conn.commit()
  50.  
  51. sql_command = '''
  52. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  53. VALUES('da290', 'David', 'Platinum', '23000', '2132001000', 'NewYork')
  54. '''
  55. cursor.execute(sql_command)
  56.  
  57. sql_command = '''
  58. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  59. VALUES('rona324', 'Emily', 'Silver', '70000', '8019004000', 'Washington')
  60. '''
  61. cursor.execute(sql_command)
  62.  
  63. sql_command = '''
  64. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  65. VALUES('imkim', 'Kim', 'Gold', '20000', '2178002400', 'Dallas')
  66. '''
  67. cursor.execute(sql_command)
  68.  
  69. sql_command = '''
  70. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  71. VALUES('er123', 'Eric', 'Silver', '40000', '3138001000', 'Chicago')
  72. '''
  73. cursor.execute(sql_command)
  74.  
  75. sql_command = '''
  76. INSERT INTO ORDERS
  77. VALUES('2019031801', 'ri902', 'Kimchi', '10', '35', '20190318')
  78. '''
  79. cursor.execute(sql_command)
  80.  
  81.  
  82. sql_command = '''
  83. INSERT INTO ORDERS
  84. VALUES('2019030113', 'da290', 'Ramen', '20', '1', '20190301')
  85. '''
  86. cursor.execute(sql_command)
  87.  
  88.  
  89. sql_command = '''
  90. INSERT INTO ORDERS
  91. VALUES('2019021901', 'rona324', 'Ramen', '2', '1', '20190219')
  92. '''
  93. cursor.execute(sql_command)
  94.  
  95.  
  96. sql_command = '''
  97. INSERT INTO ORDERS
  98. VALUES('2019031724', 'imkim', 'Icecream', '8', '16', '20190317')
  99. '''
  100. cursor.execute(sql_command)
  101.  
  102.  
  103. sql_command = '''
  104. INSERT INTO ORDERS
  105. VALUES('2019031907', 'er123', 'Fried chicken', '1', '17', '20190319')
  106. '''
  107. cursor.execute(sql_command)
  108.  
  109. conn.commit()
  110.  
  111.  
  112. sql_command = '''
  113. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  114. VALUES('da290', 'David', 'Platinum', '23000', '2132001000', 'NewYork')
  115. '''
  116. cursor.execute(sql_command)
  117.  
  118. sql_command = '''
  119. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  120. VALUES('rona324', 'Emily', 'Silver', '70000', '8019004000', 'Washington')
  121. '''
  122. cursor.execute(sql_command)
  123.  
  124. sql_command = '''
  125. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  126. VALUES('imkim', 'Kim', 'Gold', '20000', '2178002400', 'Dallas')
  127. '''
  128. cursor.execute(sql_command)
  129.  
  130. sql_command = '''
  131. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  132. VALUES('er123', 'Eric', 'Silver', '40000', '3138001000', 'Chicago')
  133. '''
  134. cursor.execute(sql_command)
  135.  
  136. sql_command = '''
  137. INSERT INTO ORDERS
  138. VALUES('2019031801', 'ri902', 'Kimchi', '10', '35', '20190318')
  139. '''
  140. cursor.execute(sql_command)
  141.  
  142.  
  143. sql_command = '''
  144. INSERT INTO ORDERS
  145. VALUES('2019030113', 'da290', 'Ramen', '20', '1', '20190301')
  146. '''
  147. cursor.execute(sql_command)
  148.  
  149.  
  150. sql_command = '''
  151. INSERT INTO ORDERS
  152. VALUES('2019021901', 'rona324', 'Ramen', '2', '1', '20190219')
  153. '''
  154. cursor.execute(sql_command)
  155.  
  156.  
  157. sql_command = '''
  158. INSERT INTO ORDERS
  159. VALUES('2019031724', 'imkim', 'Icecream', '8', '16', '20190317')
  160. '''
  161. cursor.execute(sql_command)
  162.  
  163.  
  164. sql_command = '''
  165. INSERT INTO ORDERS
  166. VALUES('2019031907', 'er123', 'Fried chicken', '1', '17', '20190319')
  167. '''
  168. cursor.execute(sql_command)
  169.  
  170. conn.commit()
  171.  
  172. sql_command = '''
  173. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  174. VALUES('da290', 'David', 'Platinum', '23000', '2132001000', 'NewYork')
  175. '''
  176. cursor.execute(sql_command)
  177.  
  178. sql_command = '''
  179. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  180. VALUES('rona324', 'Emily', 'Silver', '70000', '8019004000', 'Washington')
  181. '''
  182. cursor.execute(sql_command)
  183.  
  184. sql_command = '''
  185. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  186. VALUES('imkim', 'Kim', 'Gold', '20000', '2178002400', 'Dallas')
  187. '''
  188. cursor.execute(sql_command)
  189.  
  190. sql_command = '''
  191. INSERT INTO CUSTOMERS(ID, Name, Rating, Reserves, Phone, Address)
  192. VALUES('er123', 'Eric', 'Silver', '40000', '3138001000', 'Chicago')
  193. '''
  194. cursor.execute(sql_command)
  195.  
  196. sql_command = '''
  197. INSERT INTO ORDERS
  198. VALUES('2019031801', 'ri902', 'Kimchi', '10', '35', '20190318')
  199. '''
  200. cursor.execute(sql_command)
  201.  
  202.  
  203. sql_command = '''
  204. INSERT INTO ORDERS
  205. VALUES('2019030113', 'da290', 'Ramen', '20', '1', '20190301')
  206. '''
  207. cursor.execute(sql_command)
  208.  
  209.  
  210. sql_command = '''
  211. INSERT INTO ORDERS
  212. VALUES('2019021901', 'rona324', 'Ramen', '2', '1', '20190219')
  213. '''
  214. cursor.execute(sql_command)
  215.  
  216.  
  217. sql_command = '''
  218. INSERT INTO ORDERS
  219. VALUES('2019031724', 'imkim', 'Icecream', '8', '16', '20190317')
  220. '''
  221. cursor.execute(sql_command)
  222.  
  223.  
  224. sql_command = '''
  225. INSERT INTO ORDERS
  226. VALUES('2019031907', 'er123', 'Fried chicken', '1', '17', '20190319')
  227. '''
  228. cursor.execute(sql_command)
  229.  
  230. conn.commit()
  231.  
  232.  
  233.  
  234. # Practicing SELECT statement
  235.  
  236. result = cursor.execute('SELECT * FROM CUSTOMERS') # all data in customers in tuples
  237.  
  238. for i in result:
  239. print(i)
  240.  
  241. result = cursor.execute('SELECT ID, Name FROM CUSTOMERS')
  242.  
  243. for i in result:
  244. print(i)
  245.  
  246. # Practicing WHERE
  247. result = cursor.execute('''
  248. SELECT Name FROM CUSTOMERS WHERE Address = 'Chicago'
  249. ''')
  250.  
  251. for i in result:
  252. print(i)
  253.  
  254. result = cursor.execute('SELECT * FROM ORDERS WHERE QUANTITY*UNITPRICE > 100 AND ORDERDATE >= 20190302')
  255.  
  256.  
  257. for i in result:
  258. print(i)
  259.  
  260. # Practicing COUNT
  261. result = cursor.execute('''
  262. SELECT COUNT(*) FROM ORDERS WHERE QUANTITY*UNITPRICE > 100
  263. ''')
  264. print(result.fetchone()) #fetchone returns one record as a tuple
  265.  
  266. # PRACTICING GROUP BY
  267. result = cursor.execute('''
  268. SELECT ProdName, sum(QUANTITY*UNITPRICE) FROM ORDERS GROUP BY PRODNAME
  269. ''')
  270.  
  271. for i in result:
  272. print(i)
  273.  
  274. # PRACTICING JOIN
  275.  
  276. result = cursor.execute('''
  277. SELECT CUSTOMERS.NAME, CUSTOMERS.PHONE, ORDERS.*
  278. FROM CUSTOMERS, ORDERS
  279. WHERE ORDERS.ID=CUSTOMERS.ID
  280. ''')
  281.  
  282. for i in result:
  283. print(i)
  284.  
  285. #PRACTICING UPDATE
  286.  
  287. cursor.execute('''
  288. UPDATE ORDERS SET PRODNAME = 'Cake' WHERE PRODNAME = 'Ramen'
  289. ''')
  290.  
  291. result = cursor.execute('''
  292. SELECT * FROM ORDERS
  293. ''')
  294.  
  295. for r in result:
  296. print(r)
  297.  
  298. conn.commit()
  299.  
  300.  
  301.  
  302. # Apply and create pragram
  303.  
  304. def Menu():
  305. while True:
  306. print('1 = customers inquiry')
  307. print('2 = orders inquiry')
  308. print('3 = search customer')
  309. print('4 = search order')
  310. print('0 = exit program')
  311. cmd = input('>>')
  312. if cmd.isdigit():
  313. cmd = int(cmd)
  314. if cmd in range(0,5):
  315. return cmd
  316.  
  317. def printResult(schema, result):
  318. schema_list = schema.split()
  319. print('-'*(len(schema)+10))
  320. print(schema)
  321. print('-'*(len(schema)+10))
  322.  
  323. for rec in result: # result = tuples in tuple
  324. for j in range(len(rec)): # rec =tuple
  325. print(rec[j], end=' ') # print element in tuple
  326. if len(str(rec[j])) < len(schema_list[j]): #if length of rec < length of schema name
  327. print(' '*(len(schema_list[j]) - len(str(rec[j]))), end=' ') # fill out that difference with empty spaces
  328.  
  329. print() # horizonal space (enter-key)
  330. print('-'*(len(schema)+10))
  331.  
  332. # shoppingmall management program
  333. CustomerSchema = 'CustomerID CustomerName Rating Reserves PhoneNumber Address'
  334. OrderSchema = 'Order# CustomerName ProductName Quantity UnitPrice OrderDate'
  335.  
  336. #conn = sqlite3.conn(ShoppingMall.db)
  337. #cursor = conn.cursor()
  338.  
  339. running = True
  340. while running:
  341. cmd = Menu()
  342. if cmd == 0:
  343. running = False
  344. elif cmd == 1:
  345. result = cursor.execute('SELECT * FROM CUSTOMERS')
  346. printResult(CustomerSchema, result)
  347. elif cmd == 2:
  348. result = cursor.execute('SELECT * FROM ORDERS')
  349. printResult(OrderSchema, result)
  350. elif cmd == 3:
  351. print('Put last four digits of phone #:', end=' ')
  352. phonenum = input()
  353. result = cursor.execute('SELECT * FROM CUSTOMERS')
  354. for r in result:
  355. cphone = r[4]
  356. cphone = cphone[-4:-1] + cphone[-1] # last four digits
  357. if phonenum == cphone:
  358. print(r)
  359. break
  360. else:
  361. print(phonenum + 'No customer')
  362. elif cmd == 4:
  363. print('Put a product name:', end=' ')
  364. pname = input()
  365. result = cursor.execute('SELECT * FROM ORDERS')
  366. for r in result:
  367. name = r[2]
  368. if pname == name:
  369. print(r)
  370.  
  371. cursor.close()
  372. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement