Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:20:04+00:00 2026-06-01T12:20:04+00:00

This is supposed to work yet just says no stocks table – supposed lost

  • 0

This is supposed to work yet just says no stocks table – supposed lost the connection somewhere inside the contextmanager?

import sqlite3
from contextlib import contextmanager

@contextmanager
def doquery(conn, q, params=()):
    c = conn.cursor()
    c.execute(q, params)
    conn.commit()
    yield c    
    c.close()

with sqlite3.connect(':memory:') as db:    
    doquery(db,'''create table stocks
    (date text, trans text, symbol text,
    qty real, price real)''')

    doquery(db,"""insert into stocks
          values ('2006-01-05','BUY','RHAT',100,35.14)""")

    with doquery(db, 'select * from stocks') as r:
        for row in r:
            print row
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:20:05+00:00Added an answer on June 1, 2026 at 12:20 pm

    The problem is with the way you are using the context manager. Calling doquery simply creates a context manager object – you need to use it within a with statement, which calls its __enter__ and __exit__ methods as appropriate. For example, try the following:

    from contextlib import contextmanager
    
    @contextmanager
    def enter_exit(text):
        print('entering')
        yield text
        print('exiting')
    
    print(enter_exit('attempt 1'))
    
    with enter_exit('attempt 2') as t:
        print(t)
    

    The output I get is:

    <contextlib._GeneratorContextManager object at 0xcf3e90>
    entering
    attempt 2
    exiting
    

    You might want to re-read the documentation about the with statement and contextlib.

    Another problem with your code is that if c.execute or conn.commit raises an exception, c.close will not be called – I don’t know if that is actually necessary, but presumably it is the reason why you want to use a context manager rather than a function in the first place. The following changes should fix both problems:

    import sqlite3
    from contextlib import contextmanager
    
    @contextmanager
    def doquery(conn, q, params=()):
        c = conn.cursor()
        try:
            c.execute(q, params)
            conn.commit()
            yield c
        finally:
            c.close()
    
    with sqlite3.connect(':memory:') as db:
        with doquery(db,'''create table stocks
                     (date text, trans text, symbol text,
                     qty real, price real)'''):
            pass
    
        with doquery(db,"""insert into stocks
                     values ('2006-01-05','BUY','RHAT',100,35.14)"""):
            pass
    
        with doquery(db, 'select * from stocks') as r:
            for row in r:
                print(row)
    

    However, I don’t think this is the cleanest way of doing this. As far as I can see, there is no reason to create three separate cursor objects – you can use the same one for each query. I don’t think the call to conn.commit is actually necessary either – using the database connection as a context manager will automatically commit transactions, or roll them back if an exception is raised (see the sqlite3 module documentation).

    EDIT: Here is a much cleaner version, which still works. I really don’t know what closing the cursor actually does though – it probably isn’t necessary (Cursor.close doesn’t even seem to be documented).

    import sqlite3
    from contextlib import closing
    
    with sqlite3.connect(':memory:') as db:
        with closing(db.cursor()) as c:
            c.execute('''create table stocks
                     (date text, trans text, symbol text,
                     qty real, price real)''')
            c.execute("""insert into stocks
                     values ('2006-01-05','BUY','RHAT',100,35.14)""")
            c.execute('select * from stocks')
            for row in c:
                print(row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this fail, it's supposed to be simple and work ? fisier.seekg(0, ios::end);
How are you supposed to dispose of a BitmapSource ? // this wont work
I'm trying to return json content read from MySQL server. This is supposed to
This code is supposed to be able to sort the items in self.array based
This program is supposed to determine how many units are stored in the value
This code is supposed to read postfix problems from a file and and write
OK, this probably is supposed to be the easiest thing in the world, but
This piece of code is supposed to go through a list and preform some
I supposed to make this type of charts, is it posssible to make through
I'm stumped on how namespaced custom events are supposed to work in jQuery. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.