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

Terry Reedy tjreedy at udel.edu
Mon Nov 12 21:41:35 CET 2012


On 11/12/2012 7:25 AM, Joel Goldstick wrote:

> 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.

-- 
Terry Jan Reedy



More information about the Python-list mailing list