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 3626546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:46:33+00:00 2026-05-18T23:46:33+00:00

I was thinking I’d try to make my sqlite db connection a function instead

  • 0

I was thinking I’d try to make my sqlite db connection a function instead of copy/pasting the ~6 lines needed to connect and execute a query all over the place.
I’d like to make it versatile so I can use the same function for create/select/insert/etc…

Below is what I have tried. The ‘INSERT’ and ‘CREATE TABLE’ queries are working, but if I do a ‘SELECT’ query, how can I work with the values it fetches outside of the function?
Usually I’d like to print the values it fetches and also do other things with them.

When I do it like below I get an error

Traceback (most recent call last):
File "C:\Users\steini\Desktop\py\database\test3.py", line 15, in <module>
for row in connection('testdb45.db', "select * from users"):
ProgrammingError: Cannot operate on a closed database.

So I guess the connection needs to be open so I can get the values from the cursor, but I need to close it so the file isn’t always locked.

Here’s my testing code:

import sqlite3

def connection (db, arg, cubby):
    conn = sqlite3.connect(db)
    conn.execute('pragma foreign_keys = on')
    cur = conn.cursor()
    cur.execute(arg)
    for row in cur:
        cubby.append(row)
    conn.commit()
    conn.close()

cubby=[]
connection('testdb.db', "create table users ('user', 'email')", cubby)
connection('testdb.db', "insert into users ('user', 'email') values ('joey', 'foo@bar')", cubby)
for row in connection('testdb45.db', "select * from users", cubby):
    print row

How can I make this work?

EDIT: modified the code a little so the cur values so it’s appended to an outside list, but still pretty bad

  • 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-05-18T23:46:34+00:00Added an answer on May 18, 2026 at 11:46 pm

    I think the problem is a little more difficult than it looks at first.

    You’re seeing the error because you’ve closed your connection to the database in your “connection” function.

    Your probably better off creating a DatabaseManagement Class, to manage a single connection.

    Something like:

    import sqlite3
    
    class DatabaseManager(object):
        def __init__(self, db):
            self.conn = sqlite3.connect(db)
            self.conn.execute('pragma foreign_keys = on')
            self.conn.commit()
            self.cur = self.conn.cursor()
    
        def query(self, arg):
            self.cur.execute(arg)
            self.conn.commit()
            return self.cur
    
        def __del__(self):
            self.conn.close()
    

    Then you should be able to do something like:

    dbmgr = DatabaseManager("testdb.db")
    for row in dbmgr.query("select * from users"):
        print row
    

    This will keep the connection open for the duration of the object’s existence.

    You still may find this is a deeper problem, but play around and see what works for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was thinking about doing a take_timing function that would take the timing of
Was thinking of creating a CONS function to create a list, but then realized
im thinking if there is a way to run a jquery function like $.post
Thinking of implementing a LDAP-viewer in QML. The first step is obviously to make
I was thinking that when a function returns an object on the stack to
Thinking about getting into .net technology project management I've had plenty of experience with
Thinking about a Windows-hosted build process that will periodically drop files to disk to
Thinking in regards to Sliverlight, I would like to know where would be good
Thinking about my other problem , i decided I can't even create a regular
Thinking about avoiding code replication, I got a question that catches me every time

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.