Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33172 > unrolled thread
| Started by | Khalid Al-Ghamdi <emailkgnow@gmail.com> |
|---|---|
| First post | 2012-11-12 02:45 -0800 |
| Last post | 2012-11-12 15:41 -0500 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
Is there a way to creat a func that returns a cursor that can be used? Khalid Al-Ghamdi <emailkgnow@gmail.com> - 2012-11-12 02:45 -0800
Re: Is there a way to creat a func that returns a cursor that can be used? Peter Otten <__peter__@web.de> - 2012-11-12 11:57 +0100
Re: Is there a way to creat a func that returns a cursor that can be used? Chris Angelico <rosuav@gmail.com> - 2012-11-12 22:01 +1100
Re: Is there a way to creat a func that returns a cursor that can be used? Terry Reedy <tjreedy@udel.edu> - 2012-11-12 15:41 -0500
| From | Khalid Al-Ghamdi <emailkgnow@gmail.com> |
|---|---|
| Date | 2012-11-12 02:45 -0800 |
| Subject | Is there a way to creat a func that returns a cursor that can be used? |
| Message-ID | <05f7724a-1e41-43ea-b837-88e476917f9a@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-11-12 11:57 +0100 |
| Message-ID | <mailman.3579.1352717997.27098.python-list@python.org> |
| In reply to | #33172 |
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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-11-12 22:01 +1100 |
| Message-ID | <mailman.3580.1352718075.27098.python-list@python.org> |
| In reply to | #33172 |
On Mon, Nov 12, 2012 at 9:45 PM, Khalid Al-Ghamdi <emailkgnow@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-11-12 15:41 -0500 |
| Message-ID | <mailman.3592.1352752925.27098.python-list@python.org> |
| In reply to | #33172 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web