Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there a way to creat a func that returns a cursor that can be used?

Reply
Thread Tools

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

 
 
Khalid Al-Ghamdi
Guest
Posts: n/a
 
      11-12-2012
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:

>>> 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
>>> connect()

<sqlite3.Cursor object at 0x0119EA20>
>>> cur.execute("select * from schedule")

Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
cur.execute("select * from schedule")
NameError: name 'cur' is not defined
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      11-12-2012
Khalid Al-Ghamdi wrote:

> 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:
>
>>>> 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
>>>> connect()


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

cur = connect()

> <sqlite3.Cursor object at 0x0119EA20>
>>>> cur.execute("select * from schedule")

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



 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      11-12-2012
On Mon, Nov 12, 2012 at 9:45 PM, Khalid Al-Ghamdi <> wrote:
> 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:
>
>>>> def connect():

> return cur
>>>> connect()

> <sqlite3.Cursor object at 0x0119EA20>
>>>> cur.execute("select * from schedule")

> 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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      11-12-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
how to tell if cursor is sqlite.Cursor or psycopg2.Cursor dmaziuk Python 3 01-25-2011 04:52 AM
Can I know if "new Func()" or "Func()" is called? jht5945@gmail.com Javascript 37 10-14-2006 09:18 PM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
What's the difference between built-in func getattr() and normal call of a func of a class Johnny Python 3 08-23-2005 08:28 AM



Advertisments