gpt4 book ai didi

python - AttributeError: 'module' 对象没有属性 'connection'

转载 作者:行者123 更新时间:2023-12-03 18:15:18 24 4
gpt4 key购买 nike

我的代码的开头是:

import sqlite3

def construct_query(card_name, card_type,card_type_whole, power, tough, card_text, exp,rare):

query = "INSERT INTO CardComponet (all column names) VALUES ('{}','{}',{},{},'{}','{}','{}')".format(card_name, card_type_whole, power, tough, card_text, exp,rare)
return query

path_to_flat_file = 'C:\Users\Mrs Rose\Desktop\inputf.txt'

flat_file_object = open(path_to_flat_file, 'r')

connection = sqlite3.connection('practice1.db')

cursor = connection.cursor()

但我的错误是:
Traceback (most recent call last):
File "C:\Users\Mrs rose\Desktop\FinalProject.py", line 11, in <module>
connection = sqlite3.connection('practice1.db')
AttributeError: 'module' object has no attribute 'connection'

我尝试更改我的 .py 名称和我的数据库名称,但没有任何效果。如果可以的话请帮忙。

最佳答案

sqlite3.connection不存在。您要查找的函数称为 sqlite3.connect :

>>> import sqlite3
>>> sqlite3.connect
<built-in function connect>
>>>

另外,你不应该使用 str.format或类似的工具将值插入查询。来自 docs :

Usually your SQL operations will need to use values from Python variables. You shouldn’t assemble your query using Python’s string operations because doing so is insecure; it makes your program vulnerable to an SQL injection attack (see http://xkcd.com/327/ for humorous example of what can go wrong).

Instead, use the DB-API's parameter substitution. Put ? as a placeholder wherever you want to use a value, and then provide a tuple of values as the second argument to the cursor's execute() method. (Other database modules may use a different placeholder, such as %s or :1.)

关于python - AttributeError: 'module' 对象没有属性 'connection',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27435477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com