R.parry
R.parry

Reputation: 23

AttributeError: 'function' object has no attribute 'connect'

I'm trying to insert data into my already created sqlite3 database. The data is able to be inputted. However, when trying to connect to my database and save I receive an error saying " AttributeError: 'function' object has no attribute 'connect'" My code fragment is listed below:

def data_entry():
    option_choice=0
    call("clear")
    print"\t","\n"*4
    print"\t","-"*45
    print"\t","\t-- Data Entry --"

    ID = raw_input("what is your user ID?")
    Name = raw_input("What is your name?")
    Age = raw_input("how old are you?")
    incidents = raw_input("How many incidents have you been involved in?")
    physical_ability = raw_input("what is your physical driving state?")
    Address = raw_input("what is your address?")
    Tel = raw_input("what is your phone number?")
    datestamp = raw_input("What is today's date?")
    keyword = raw_input("What is the make of your vehicle?")
    monthly_salary = raw_input("How much do you earn each month?")

    conn = db.connect('Insurance_Shark.db') 
    cursor = conn.cursor()
    cursor.execute("INSERT INTO customerdatabase VALUES(?, ?, ?, ?, ?, ?)",
        (ID, Name, Age, incidents, physical_ability, Address, Tel, datestamp, keyword, monthly_salary))

Upvotes: 2

Views: 4439

Answers (1)

alecxe
alecxe

Reputation: 473833

You haven't shown how the db variable is defined, but it looks like it is a function, because:

>>> def db():
...     pass
... 
>>> db.connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'connect'

The connect() function is available directly from the sqlite3 module:

>>> import sqlite3 
>>> sqlite3.connect("test.db")
<sqlite3.Connection object at 0x10d922a28>

Upvotes: 1

Related Questions