[Tutor] Connecting to a DB via a function call

Dave Angel d at davea.name
Sun Nov 11 14:10:10 CET 2012


On 11/11/2012 06:39 AM, Khalid Al-Ghamdi wrote:
> Hi,
>
> Why is it that the following even though I call the below function? And how
> do I write a function that makes connection that I can use to query my DB?
>
> Traceback (most recent call last):
>   File "C:/Python33/My Stuff/schedule_machine/schedule_machine01.py", line
> 19, in <module>
>     cur.execute('INSERT INTO schedule \
> NameError: name 'cur' is not defined
>
>
>    1. import csv
>    2. import sqlite3
>    3.
>    4. def connect():
>    5.     conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db  ....
>    etc.
>    6.     cur = conn.cursor()
>    7.     cur.execute("create table schedule (teams integer, sn integer,
>    badge integer ,name text, grp integer,\
>    8.    major text, track text, stage text,  tc text)")
>    9.
>    10. connect()
>
>

Thank you for posting the full traceback.

The code you posted doesn't match the error you show.  For one thing,
there's no line 19.  For another, the literal string  of the line shown
in the error doesn't agree with the one in the displayed code.  Next,
the variable is clearly defined in line 6, with no conditional to skip
around it.  Next, the error happens in top-level code, not inside any
function.  Finally, the def would never execute, since it has an
indentation error in line 8.

My guess is that you do not have the call to connect() in line 10, and
that you have another attempt to call the execute method at line 19.


I'm not the one to advise you on the database stuff, but if anyone is to
help you, you need to post code that matches the error you post. 
Incidentally, although it doesn't matter much for 10 lines, you should
ditch the line numbers when posting.  Paste the source file contents as
it really exists.  Otherwise, you're making it harder for everyone who
tries to run your code.

-- 

DaveA



More information about the Tutor mailing list