Is there a way to creat a func that returns a cursor that can be used?

K

Khalid Al-Ghamdi

Is there a way to create a func that returns a cursor that can be used to execute sql statements?

I tried this (after importing sqlite3), but it gave me the error below:
conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db .... etc.
cur = conn.cursor()
cur.execute("create table schedule (teams integer, sn integer, badge integer ,name text, grp integer,\
major text, track text, stage text, tc text, subject text, course text, ws text, date text, \
time text, proctor text, code text, no integer,fl_time text, flag2 text, flag3 text, flag4 text, clash1 integer, clash2 integer)")
return curTraceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
cur.execute("select * from schedule")
NameError: name 'cur' is not defined
 
P

Peter Otten

Khalid said:
Is there a way to create a func that returns a cursor that can be used to
execute sql statements?

You should read an introductory text on Python, this is not specific to
sqlite3.
I tried this (after importing sqlite3), but it gave me the error below:

conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db .... etc.
cur = conn.cursor()
cur.execute("create table schedule (teams integer, sn integer, badge
integer ,name text, grp integer,\
major text, track text, stage text, tc text, subject text, course
text, ws text, date text, \ time text, proctor text, code text, no
integer,fl_time text, flag2 text, flag3 text, flag4 text, clash1
integer, clash2 integer)") return cur

connect() returns a cursor, but you discard the result. Try

cur = connect()
 
C

Chris Angelico

Is there a way to create a func that returns a cursor that can be used to execute sql statements?

Yes, and you're almost there!
I tried this (after importing sqlite3), but it gave me the error below:

Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
cur.execute("select * from schedule")
NameError: name 'cur' is not defined

All you need to do is make use of the return value. Try this instead:

cur = connect()

That takes the returned cursor object and binds it to the name 'cur'
in global scope. You can then use 'cur.execute...' and it'll be the
same object.

As a side point, thank you for posting so clearly. It's easy to help
when your code and traceback are all there!

ChrisA
 
T

Terry Reedy

Chris gave you the same help that you got yesterday. ....
go to ://python.org> and read the tutorials,
specifically about functions.

It is hard to see what is working and not with an empty database. But to
drive the point home, running

import sqlite3

def connect():
conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db .... etc.
cur = conn.cursor()
cur.execute("create table schedule (teams integer, sn integer,
badge integer ,name text, grp integer,\
major text, track text, stage text, tc text, subject text, course
text, ws text, date text, \
time text, proctor text, code text, no integer,fl_time text, flag2
text, flag3 text,\
flag4 text, clash1 integer, clash2 integer)")
return cur

cur = connect()
print(cur.fetchone())
cur.execute("select * from schedule")
print(cur.fetchall())

# prints
None
[]

So the change suggested by multiple people *does* work ;-).

On a different note: use triple quote for multi-line strings and
backslashes are not needed.

cur.execute('''create table schedule (teams integer, sn integer,
badge integer ,name text, grp integer, major text,
track text, stage text, tc text, subject text, course text,
ws text, date text, time text, proctor text, code text,
no integer,fl_time text, flag2 text, flag3 text, flag4 text,
clash1 integer, clash2 integer)''')

works fine. SQL treats newlines in statements as whitespace.
 

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top