ValueError: 操作参数必须是 str 或 unicode [英] ValueError: operation parameter must be str or unicode
本文介绍了ValueError: 操作参数必须是 str 或 unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 python 将一些数据库字段插入到 SQLite 数据库中.我不断收到以下错误:
I'm trying to insert some database fields using python to an SQLite DB. I keep getting the following error:
ValueError: operation parameter must be str
下面是我的代码.
import sqlite3
conn = sqlite3.connect("pass.db")
c = conn.cursor()
#Create table password
table_create_statement = "create table if not exists password(id integer primary key, site text, pass text);"
c.execute(table_create_statement)
#Promt user to input entry
sites = str(raw_input("Enter name of the sites: "))
password = str(raw_input("Enter password: "))
#SQL statement to insert the new entry
sql_statement = "insert into password values(?, ?, ?);",(None, sites, password)
c.execute((sql_statement))
connection.commit()
print("Password entered successfully")
输出:
Enter name of the sites: www.google.com
Enter password: abcd
Traceback (most recent call last):
File "pw.py", line 39, in <module>
c.execute((sql_statement))
ValueError: operation parameter must be str or unicode
推荐答案
您需要将sql语句和参数作为单独的参数传递给execute()
.您当前正在传递一个元组.
You need to pass in the sql statement and parameters as separate arguments to execute()
. You are currently passing in a tuple.
改为这样做:
sql_statement = "insert into password values(?, ?, ?);"
c.execute(sql_statement, (None, sites, password))
现在 sql_statement
是单个字符串,参数作为第二个单独的参数传入.
Now sql_statement
is a single string, and the parameters are passed in as a second, separate argument.
请注意,raw_input()
已经返回了一个 str
对象,无需再次将其转换为 str
.
Note that raw_input()
already returns a str
object, no need to convert that to str
again.
这篇关于ValueError: 操作参数必须是 str 或 unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文