Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie help
#1
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:

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
Reply


Messages In This Thread
Newbie help - by dust - Jul-03-2018, 03:33 PM
RE: Newbie help - by gontajones - Jul-03-2018, 03:43 PM
RE: Newbie help - by buran - Jul-03-2018, 03:44 PM
RE: Newbie help - by gontajones - Jul-03-2018, 03:45 PM
RE: Newbie help - by buran - Jul-03-2018, 03:54 PM
RE: Newbie help - by gontajones - Jul-03-2018, 04:04 PM
RE: Newbie help - by dust - Jul-03-2018, 04:53 PM
RE: Newbie help - by buran - Jul-03-2018, 05:41 PM
RE: Newbie help - by dust - Jul-03-2018, 05:44 PM
RE: Newbie help - by dust - Jul-04-2018, 08:25 AM
RE: Newbie help - by gontajones - Jul-04-2018, 09:35 AM
RE: Newbie help - by dust - Jul-04-2018, 09:41 AM
RE: Newbie help - by gontajones - Jul-04-2018, 10:01 AM
RE: Newbie help - by dust - Jul-04-2018, 10:03 AM
RE: Newbie help - by dust - Jul-25-2018, 09:07 AM
RE: Newbie help - by buran - Jul-25-2018, 09:14 AM
RE: Newbie help - by buran - Jul-25-2018, 09:16 AM
RE: Newbie help - by dust - Aug-01-2018, 11:11 AM
RE: Newbie help - by buran - Aug-01-2018, 11:24 AM
RE: Newbie help - by dust - Aug-01-2018, 11:35 AM
RE: Newbie help - by buran - Aug-01-2018, 11:52 AM
RE: Newbie help - by dust - Aug-01-2018, 12:34 PM
RE: Newbie help - by buran - Aug-01-2018, 12:54 PM
RE: Newbie help - by micseydel - Aug-01-2018, 06:29 PM
RE: Newbie help - by dust - Aug-01-2018, 09:19 PM
RE: Newbie help - by Larz60+ - Aug-29-2018, 09:25 AM
RE: Newbie help - by dust - Aug-29-2018, 10:04 AM
RE: Newbie help - by dust - Aug-29-2018, 01:14 PM
RE: Newbie help - by Larz60+ - Aug-29-2018, 02:53 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020