Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- import sqlite3 #This imports the "sqlite3" lib
- chand = sqlite3.connect("computer_cards.db") #This sets a connectivity handler to the d/B
- except:
- print('******************************')
- print('ERROR! Database does not exit!')
- print('Will quit.Goodbye')
- print('******************************')
- quit()
- def create_new(name, cores, speed, ram, cost): #This creates a new function called "create_new" with 5 parameters
- insert_sql = "INSERT INTO computer(name, cores, cpu_speed, ram, cost) VALUES ('{}', {}, {},{},{})".format(name, cores, speed, ram, cost)
- #The format method then replaces the {} markers in the SQL statement with the values from the 5 variables.
- chand.execute(insert_sql)
- chand.commit() #The code then needs to execute the insert_sql statement and commit the changes to the database
- #This code prompts the user for the new database record
- print('')
- print('Please enter a new record, with all 5 attributes')
- print('')
- name = input('Name > ')
- cores = input('Cores > ')
- speed = input('Speed > ')
- ram = input('RAM > ')
- cost = input('Cost > ')
- create_new(name, cores, speed, ram, cost)
- #This part simply calls the "create_new" function and passes in "Fahim2" and 3
- #print('')
- #print('We want to add a new record...')
- #print('')
- #create_new("Fahim2", 3)
- #print('')
- #################################################################
- # This part of the program just lists the data from the database.
- #################################################################
- result = chand.execute("SELECT * FROM computer") #This puts the query response into "result" cursor
- computer_data = result.fetchall() #This puts the data from the above SQL in "computer_data
- print('')
- print('The data table contains:')
- print('')
- for i in computer_data:
- # print(i) #This prints each record to the screen
- name = i[0] #The code only prints the first field
- core = i[1] #The code prints the second field
- speed = i[2]
- ram = i[3]
- cost = i[4]
- print(name,core,speed,ram,cost)
- print('')
- print('Goodbye')
- chand.close() #This closes the database connector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement