TonyMo

36 database.py

Apr 21st, 2021
871
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #  36 Creating new cards
  2. """
  3. Can you modify the program so that it also
  4. updates the cpu_speed, ram, and cost fields.   """
  5. #  errors see below. Line 33 ? and line 13 incorrect bindings ?
  6. #  Shut down, Had a sleep and now Runs fine. ?? Err caused by $595. !!!
  7. #  db updated with this mornings data input just fine.
  8. #  But :
  9. #  Shadows name 'name' from outer scope 17
  10. #  Shadows name 'cores' from outer scope  17 ETC.
  11. #    Quickfix is to rename the element. ?
  12.  
  13. import sqlite3
  14. conn = sqlite3.connect("computer_cards.db")
  15.  
  16.  
  17. def create(name, cores, cpu_speed, ram, cost):
  18.     insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) " \
  19.                  "VALUES ('{}', {}, {}, {}, {})".format(name, cores, cpu_speed, ram, cost)
  20.     conn.execute(insert_sql)
  21.     conn.commit()
  22.  
  23.  
  24. #  create("My computer", 4)
  25. #  gives ('My computer', 4, None, None, None)
  26.  
  27. """
  28. Now add a simple user interface to the program
  29. to gather the name and number of cores so you can use it to add multiple computers. """
  30.  
  31. print("Enter the details:")
  32.  
  33. name = input("Name > ")
  34. cores = input("Cores > ")
  35. cpu_speed = input("cpu_speed GHz > ")
  36. ram = input("Ram MB > ")
  37. cost = input("Cost $ > ")
  38.  
  39. # Add a record to the db
  40. create(name, cores, cpu_speed, ram, cost)
  41.  
  42. #  Execute a SQL statement to get data from the database.
  43. result = conn.execute("SELECT * FROM computer")
  44.  
  45. #  Using the cursor you can now fetch the data you requested.
  46. computers = result.fetchall()
  47.  
  48. #   For all the fields.
  49. for computer in computers:
  50.     print(computer)
  51.  
  52. conn.close()
  53.  
  54. """Errors
  55. Traceback (most recent call last):
  56.  File "/home/tony/Documents/_Python 103/W3/W3 Scripts/36/36 database.py", line 33, in <module>
  57.    create(name, cores, cpu_speed, ram, cost)
  58.  File "/home/tony/Documents/_Python 103/W3/W3 Scripts/36/36 database.py", line 13, in create
  59.    conn.execute(insert_sql)
  60. sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.
  61.  
  62. NB This ^ caused by entering $595. Works OK if 595 is entered.
  63. """
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×