Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 36 Creating new cards
- """
- Can you modify the program so that it also
- updates the cpu_speed, ram, and cost fields. """
- # errors see below. Line 33 ? and line 13 incorrect bindings ?
- # Shut down, Had a sleep and now Runs fine. ?? Err caused by $595. !!!
- # db updated with this mornings data input just fine.
- # But :
- # Shadows name 'name' from outer scope 17
- # Shadows name 'cores' from outer scope 17 ETC.
- # Quickfix is to rename the element. ?
- import sqlite3
- conn = sqlite3.connect("computer_cards.db")
- def create(name, cores, cpu_speed, ram, cost):
- insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) " \
- "VALUES ('{}', {}, {}, {}, {})".format(name, cores, cpu_speed, ram, cost)
- conn.execute(insert_sql)
- conn.commit()
- # create("My computer", 4)
- # gives ('My computer', 4, None, None, None)
- """
- Now add a simple user interface to the program
- to gather the name and number of cores so you can use it to add multiple computers. """
- print("Enter the details:")
- name = input("Name > ")
- cores = input("Cores > ")
- cpu_speed = input("cpu_speed GHz > ")
- ram = input("Ram MB > ")
- cost = input("Cost $ > ")
- # Add a record to the db
- create(name, cores, cpu_speed, ram, cost)
- # Execute a SQL statement to get data from the database.
- result = conn.execute("SELECT * FROM computer")
- # Using the cursor you can now fetch the data you requested.
- computers = result.fetchall()
- # For all the fields.
- for computer in computers:
- print(computer)
- conn.close()
- """Errors
- Traceback (most recent call last):
- File "/home/tony/Documents/_Python 103/W3/W3 Scripts/36/36 database.py", line 33, in <module>
- create(name, cores, cpu_speed, ram, cost)
- File "/home/tony/Documents/_Python 103/W3/W3 Scripts/36/36 database.py", line 13, in create
- conn.execute(insert_sql)
- sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.
- NB This ^ caused by entering $595. Works OK if 595 is entered.
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement