AttributeError:'功能'对象没有属性' connect'

时间:2016-01-10 18:08:18

标签: python database sqlite connection database-connection

我试图将数据插入到我已创建的sqlite3数据库中。可以输入数据。但是,当我尝试连接到我的数据库并保存时,我收到一条错误说" AttributeError:'功能'对象没有属性' connect'" 我的代码片段列在下面:

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))

1 个答案:

答案 0 :(得分:1)

您尚未展示如何定义db变量,但它看起来像它是一个函数,因为:

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

connect() function直接来自sqlite3模块:

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