Jul-03-2018, 03:33 PM
Hi all
Brand new at this, am just dipping my toe into Python as I want to try and have a go at creating some simple databases and then see what I can do with them.
I started here --> https://pythonschool.net/databases/creat...ata-model/
Great site so far, I've managed to create a simple database file but I've now hit a stumbling block and I just cannot work out what I'm doing wrong. I've got as far as the second video on the page where it adds a bit more code to the example so that it checks for an existing database and then gives you the option of leaving it as it is, or over-writing with a brand new one. Thing is, I cannot get the code to work at all, it just spits out an error yet, I'm certain I've typed it all in correctly.
Here's the code I have:
Traceback (most recent call last):
File "C:\Users\dwhiting\Documents\Python\database.py", line 31, in <module>
create_table(db_name, "Product",sql)
File "C:\Users\dwhiting\Documents\Python\database.py", line 10, in create_table
response = input("The table {0} already exists, do you wish to recreate it (y/n): ".format(table_name))
File "<string>", line 1, in <module>
NameError: name 'n' is not defined
Can anyone help? I'm betting it's just a typo somewhere but, I've checked it over and over and I'm sure it's right.
Thanks
Brand new at this, am just dipping my toe into Python as I want to try and have a go at creating some simple databases and then see what I can do with them.
I started here --> https://pythonschool.net/databases/creat...ata-model/
Great site so far, I've managed to create a simple database file but I've now hit a stumbling block and I just cannot work out what I'm doing wrong. I've got as far as the second video on the page where it adds a bit more code to the example so that it checks for an existing database and then gives you the option of leaving it as it is, or over-writing with a brand new one. Thing is, I cannot get the code to work at all, it just spits out an error yet, I'm certain I've typed it all in correctly.
Here's the code I have:
import sqlite3 def create_table(db_name,table_name,sql): with sqlite3.connect(db_name) as db: cursor = db.cursor() cursor.execute("select name from sqlite_master where name=?",(table_name,)) result = cursor.fetchall() keep_table = True if len(result) == 1: response = input("The table {0} already exists, do you wish to recreate it (y/n): ".format(table_name)) if response == "y": keep_table = False print("The {0} table will be recreated - all existing data will be lost".format(table_name)) cursor.execute("drop table if exists {0}".format(table_name)) db.commit() else: print("The existing table was kept") else: keep_table = False if not keep_table: cursor.execute(sql) db.commit() if __name__ == "__main__": db_name = "coffee_shop.db" sql = """create table Product (ProductID integer, Name text, Price real, primary key(ProductID))""" create_table(db_name, "Product",sql)If I run it with no database present, it creates one fine. If I then re-run it, it gives me the option to enter "y" or "n" but, after I do it, it just errors with:
Traceback (most recent call last):
File "C:\Users\dwhiting\Documents\Python\database.py", line 31, in <module>
create_table(db_name, "Product",sql)
File "C:\Users\dwhiting\Documents\Python\database.py", line 10, in create_table
response = input("The table {0} already exists, do you wish to recreate it (y/n): ".format(table_name))
File "<string>", line 1, in <module>
NameError: name 'n' is not defined
Can anyone help? I'm betting it's just a typo somewhere but, I've checked it over and over and I'm sure it's right.
Thanks