Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. import sqlite3
  2. import pprint
  3.  
  4.  
  5. def create_books_table():
  6. connection = sqlite3.connect("books.db")
  7. connection_cursor = connection.cursor()
  8.  
  9. connection_cursor.execute("""CREATE TABLE IF NOT EXISTS books(
  10. id integer PRIMARY KEY,
  11. book_title text,
  12. author text,
  13. publish_date date,
  14. publisher text,
  15. selling_price numeric
  16. )""")
  17. connection.commit()
  18. connection.close()
  19.  
  20.  
  21. create_books_table()
  22.  
  23.  
  24. def create_books():
  25. connection = sqlite3.connect("books.db")
  26. connection_cursor = connection.cursor()
  27. connection_cursor.execute("""books (book_title, author, publish_date, publisher, selling_price)
  28. VALUES("Pavadinimas", "Autorius", "Data", "Knygos kaina", 10)""")
  29. connection.commit()
  30. connection.close()
  31.  
  32.  
  33.  
  34. def get_all_books():
  35. connection = open_connection()
  36. connection_cursor = connection.cursor()
  37. connection_cursor.execute("""SELECT * FROM books""")
  38. books = connection_cursor.fetchall()
  39.  
  40. print("\nAll books: ")
  41. for row in books:
  42. print("book_title = " , row[0])
  43. print("author = ", row[1])
  44. print("publish_date = ", row[2])
  45. print("selling_price = ", row[3])
  46.  
  47. end_connection(connection)
  48.  
  49. def get_books():
  50. connection = sqlite3.connect('books.db')
  51. cursor = connection.cursor()
  52.  
  53. for row in cursor.execute("SELECT * FROM books"):
  54. print(row)
  55.  
  56.  
  57. get_books()
  58.  
  59.  
  60. def update_books_title(new_value, book_id):
  61. connection = sqlite3.connect("books.db")
  62. connection_cursor = connection.cursor()
  63. connection_cursor.execute("""UPDATE books SET book_title = ? WHERE id = ?""")
  64. update_data = [new_value, book_id]
  65. connection.commit()
  66. connection.close()
  67.  
  68.  
  69. def update_books_publisher(new_value, book_id):
  70. connection = sqlite3.connect("books.db")
  71. connection_cursor = connection.cursor()
  72. connection_cursor.execute("""UPDATE books SET publisher = ? WHERE id = ?""")
  73. update_data = [new_value, book_id]
  74. connection.commit()
  75. connection.close()
  76.  
  77.  
  78. def update_books_author(new_value, book_id):
  79. connection = sqlite3.connect("books.db")
  80. connection_cursor = connection.cursor()
  81. connection_cursor.execute("""UPDATE books SET author = ? WHERE id = ?""")
  82. update_data = [new_value, book_id]
  83. connection.commit()
  84. connection.close()
  85.  
  86.  
  87. def update_books_publish_date(new_value, book_id):
  88. connection = sqlite3.connect("books.db")
  89. connection_cursor = connection.cursor()
  90. connection_cursor.execute("""UPDATE books SET publish_date = ? WHERE id = ?""")
  91. update_data = [new_value, book_id]
  92. connection.commit()
  93. connection.close()
  94.  
  95.  
  96. def update_books_selling_price(new_value, book_id):
  97. connection = sqlite3.connect("books.db")
  98. connection_cursor = connection.cursor()
  99. connection_cursor.execute("""UPDATE books SET selling_price = ? WHERE id = ?""")
  100. update_data = [new_value, book_id]
  101. connection.commit()
  102. connection.close()
  103.  
  104.  
  105. def delete_books_by_id(books_id):
  106. connection = sqlite3.connect("books.db")
  107. connection_cursor = connection.cursor()
  108. connection_cursor.execute("""DELETE FROM books WHERE id = ?""")
  109. entry_id = [books_id]
  110. connection.commit()
  111. connection.close()
  112.  
  113.  
  114. def create_publishers_table():
  115. connection = sqlite3.connect("books.db")
  116. connection_cursor = connection.cursor()
  117.  
  118. connection_cursor.execute("""CREATE TABLE IF NOT EXISTS publishers(
  119. id integer PRIMARY KEY,
  120. publisher_name text,
  121. book_title text,
  122. author text,
  123. printed_quantity integer,
  124. printing_price numeric
  125. )""")
  126. connection.commit()
  127. connection.close()
  128.  
  129.  
  130. create_publishers_table()
  131.  
  132.  
  133. def create_publisher():
  134. connection = sqlite3.connect("books.db")
  135. connection_cursor = connection.cursor()
  136. connection_cursor.execute("""INSERT INTO publishers (publisher_name, book_title, author, printed_quantity, printing_price)
  137. VALUES("Pavadinimas", "Autorius", "Data", "Knygos kaina", 10)""")
  138. connection.commit()
  139. connection.close()
  140.  
  141.  
  142.  
  143.  
  144.  
  145. def get_publishers():
  146. connection = sqlite3.connect('books.db')
  147. cursor = connection.cursor()
  148.  
  149. for row in cursor.execute("""SELECT * FROM books WHERE book_title OR
  150. author OR
  151. publish_date OR
  152. publisher OR
  153. selling_price LIKE 10"""):
  154. print(row)
  155.  
  156.  
  157. def update_publisher_name(new_value, publisher_id):
  158. connection = sqlite3.connect("books.db")
  159. connection_cursor = connection.cursor()
  160. connection_cursor.execute("""UPDATE publishers SET publisher_name = "Pavadinimas" WHERE id = ?""")
  161. update_data = [new_value, publisher_id]
  162. connection.commit()
  163. connection.close()
  164.  
  165.  
  166. def update_publisher_book_title(new_value, publisher_id):
  167. connection = sqlite3.connect("books.db")
  168. connection_cursor = connection.cursor()
  169. connection_cursor.execute("""UPDATE books SET publisher = ? WHERE id = ?""")
  170. update_data = [new_value, publisher_id]
  171. connection.commit()
  172. connection.close()
  173.  
  174.  
  175. def update_publisher_author(new_value, publisher_id):
  176. connection = sqlite3.connect("books.db")
  177. connection_cursor = connection.cursor()
  178. connection_cursor.execute("""UPDATE publishers SET author = ? WHERE id = ?""")
  179. update_data = [new_value, publisher_id]
  180. connection.commit()
  181. connection.close()
  182.  
  183.  
  184. def update_publisher_printed_quantity(new_value, publisher_id):
  185. connection = sqlite3.connect("books.db")
  186. connection_cursor = connection.cursor()
  187. connection_cursor.execute( """UPDATE publishers SET printed_quantity = ? WHERE id = ?""")
  188. update_data = [new_value, publisher_id]
  189. connection.commit()
  190. connection.close()
  191.  
  192.  
  193. def update_publisher_printing_price(new_value, publisher_id):
  194. connection = sqlite3.connect("books.db")
  195. connection_cursor = connection.cursor()
  196. connection_cursor.execute("""UPDATE publishers SET printing_price = ? WHERE id = ?""")
  197. update_data = [new_value, publisher_id]
  198. connection.commit()
  199. connection.close()
  200.  
  201.  
  202. def delete_publisher_by_id(publisher_id):
  203. connection = sqlite3.connect("books.db")
  204. connection_cursor = connection.cursor()
  205. connection_cursor.execute("""DELETE FROM publishers WHERE id = ?""")
  206. entry_id = [publisher_id]
  207. connection.commit()
  208. connection.close()
  209.  
  210. create_books_table()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement