str not tuple error when running pandas code

huangapple go评论54阅读模式
英文:

str not tuple error when running pandas code

问题

我正在创建一个记忆问答游戏,其中使用Python向用户询问他们的一天。

我试图使用matplotlib和pandas创建一个进度图,但是当我运行代码时,我遇到以下错误:

pandas.errors.DatabaseError: 在SQL查询上执行失败:('SELECT Date, Score FROM scoring WHERE UserID =?', ('Kgarda43',)):execute()的第1个参数必须是字符串,而不是元组

这是我遇到错误的代码部分:

connection = sqlite3.connect('Memory.db')
cursor = connection.cursor()
sql = ("SELECT Date, Score FROM scoring WHERE UserID =?", (Username_Login.value,))
data = pandas.read_sql(sql, connection)  # 收集数据

任何帮助都将非常感谢。谢谢!

英文:

I'm creating a memory quiz game where it asks the user about their day (using python).

I'm trying to create a progress graph using matplotlib and pandas, however, when I run the code, I get the following error:

> pandas.errors.DatabaseError: Execution failed on sql '('SELECT Date, Score FROM scoring WHERE UserID =?', ('Kgarda43',))': execute() argument 1 must be str, not tuple

This is my code for the area I am getting an error:

connection=sqlite3.connect('Memory.db')
cursor=connection.cursor()
sql=("SELECT Date, Score FROM scoring WHERE UserID =?", (Username_Login.value,))
data= pandas.read_sql(sql,connection)#collecting the data

Any help would be great. Thanks!

答案1

得分: 1

我怀疑你数据库中的UserID字段应该是一个字符串,因此在发现这个错误后:

execute() argument 1 must be str, not tuple

你可以通过正确使用逗号来修复它,将其放入你的查询中:

sql = '''
SELECT Date, Score 
FROM scoring 
WHERE UserID = ?
'''
英文:

I suspect the UserID field in your database is supposed to be a string, therefore upon finding this error:

execute() argument 1 must be str, not tuple

You can fix it by correctly using your commas inside your query:

sql= '''
SELECT Date, Score 
FROM scoring 
WHERE UserID ='?'
     '''

huangapple
  • 本文由 发表于 2023年2月13日 22:43:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定