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

Discussion in 'Python' started by Khalid Al-Ghamdi, Nov 12, 2012.

  1. 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
     
    Khalid Al-Ghamdi, Nov 12, 2012
    #1
    1. Advertising

  2. Khalid Al-Ghamdi

    Peter Otten Guest

    Re: Is there a way to creat a func that returns a cursor that can beused?

    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
     
    Peter Otten, Nov 12, 2012
    #2
    1. Advertising

  3. Re: Is there a way to creat a func that returns a cursor that can beused?

    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
     
    Chris Angelico, Nov 12, 2012
    #3
  4. Khalid Al-Ghamdi

    Terry Reedy Guest

    Re: Is there a way to creat a func that returns a cursor that canbe used?

    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
     
    Terry Reedy, Nov 12, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    203,883
    Adrienne Boswell
    Jun 20, 2006
  2. Johnny
    Replies:
    3
    Views:
    485
    Robert Kern
    Aug 23, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    559
    Hari Sekhon
    Jun 20, 2006
  4. dmaziuk
    Replies:
    3
    Views:
    631
    Chris Gonnerman
    Jan 25, 2011
  5. Replies:
    37
    Views:
    496
Loading...

Share This Page