Python Forum
Newbie help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Newbie help (/thread-11327.html)

Pages: 1 2 3


Newbie help - dust - Jul-03-2018

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/creating-the-data-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


RE: Newbie help - gontajones - Jul-03-2018

Here the script is running ok.
What version of python are you using?


RE: Newbie help - buran - Jul-03-2018

I guess you use python2 and this code is for python3. As a newbie, you should start with python3.
in python2 input() evaluates the user input. you should use raw_input() instead - it will always return string.
in python3 it's ok, input() works as raw_input() in python2, while eval(input()) works like input() in python2
but again - switch to python3


RE: Newbie help - gontajones - Jul-03-2018

If you are using python2...change:

response = input("The table {0} already exists, do you wish to recreate it (y/n): ".format(table_name))
To:

response = raw_input("The table {0} already exists, do you wish to recreate it (y/n): ".format(table_name))



RE: Newbie help - buran - Jul-03-2018

(Jul-03-2018, 03:45 PM)gontajones Wrote: If you are using python2..
they should switch to python3.


RE: Newbie help - gontajones - Jul-03-2018

Quote:they should switch to python3.
Agree!


RE: Newbie help - dust - Jul-03-2018

Hi all, thanks for the replies, that must be it then, I am indeed running Python version 2 (because I did a hour-long intro course last week and we were told to install the older version, not sure why, I'll ask them)... :)


RE: Newbie help - buran - Jul-03-2018

(Jul-03-2018, 04:53 PM)dust Wrote: because I did a hour-long intro course last week and we were told to install the older version
python3 is around long enough. official support for python2 will end in less than 1.5 years and if they didn't bother to update their course material to python3 that speaks volumes about the quality of their course. better find another course
https://pythonclock.org/


RE: Newbie help - dust - Jul-03-2018

Understood, thanks!! :)


RE: Newbie help - dust - Jul-04-2018

Hi again, I just tried to install the latest version of Python but it wouldn't work, I got Access Denied errors. I'm guessing this is maybe because I'm on my PC at work (I've been doing this during my lunch hour) and it's a security thing?