0

I'm having issues using the method I made to connect to my sqlite db. I get an error of 'NoneType' object has no attribute 'cursor', when i call connect_db() below:

When i try to use connect_db()

def connect_db(dbname):
    try:
        dbconn = sqlite3.connect(dbname)
    except:
        print ("error connecting to db")

It throws the error here:

        #save to database
        dbconn = connect_db('./syncdb.db')
        cursor = dbconn.cursor()
        cursor.execute('''
            CREATE TABLE paths (
                id INTEGER PRIMARY KEY, 
                source TEXT, 
                destination TEXT
            ) 
        ''')

Anyone know what I'm doing wrong? I've tried a lot of different ways already to use this method connect_db(), but it doesnt seem to be able to work correctly.

Thank you.

3
  • What is the issue? Try not catching all exceptions, see which exception is raised. Your indentation is a bit messy too, maybe it's just a paste error though. Commented Jul 4, 2015 at 17:05
  • The issue is, i cant access the connection/creation/ of syncdb.db created in connect_db() when i try to get it lower down in my script. How do i access it? Commented Jul 4, 2015 at 17:07
  • Please see my updated code/question. Commented Jul 4, 2015 at 17:24

1 Answer 1

2

you need to return the connection; otherwise the function returns None

def connect_db(dbname):
    try:
        dbconn = sqlite3.connect(dbname)
    except:
        print ("error connecting to db")
    return dbconn
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.