简体   繁体   English

MYSQLdb / Python-并非在字符串格式化错误期间转换了所有参数?

[英]MYSQLdb/Python - not all arguments converted during string formatting error?

Trying to select some info from a database, I have a database manager class that looks like this: 尝试从数据库中选择一些信息,我有一个数据库管理器类,如下所示:

class DatabaseManager:
    def __init__(self):
        self.connection = MySQLdb.connect(host="localhost", 
                     user="neal", 
                      passwd="hacker123",
                      db="massive") 

        self.cursor = self.connection.cursor()

When trying to do a SELECT like this: 尝试执行这样的SELECT时:

    db = DatabaseManager()
    db.cursor.execute("SELECT password FROM USERS WHERE apikey = %s", str(request.apikey))

I get a 我得到一个

TypeError: not all arguments converted during string formatting

This is odd as I have similar queries elsewhere that worked fine, like this one: 这很奇怪,因为我在其他地方也能使用类似的查询,例如:

db.cursor.execute('''INSERT into USERS(email, username, password, apikey) values (%s, %s, %s, %s)''',
    (request.email, request.username, request.password, apikey))

Am I doing this wrong? 我做错了吗?

EDIT: Column confusion, table looks like this: 编辑:列混乱,表看起来像这样:

CREATE TABLE users (
id int NOT NULL AUTO_INCREMENT, 
email varchar(255) NOT NULL,
username varchar(25) NOT NULL,
password varchar(25) NOT NULL,
apikey varchar(45) NOT NULL,
PRIMARY KEY (id),
UNIQUE(email),
UNIQUE(username),
UNIQUE(apikey)
);

That is because the second argument of execute is an iterable . 那是因为execute第二个参数是可迭代的 So you would be better off with a list, set, or tuple. 因此,使用列表,集合或元组会更好。

Try this: 尝试这个:

db.cursor.execute("SELECT password FROM USERS WHERE apikey = %s", (str(request.apikey),))

您应该给元组,而不需要解析str;

db.cursor.execute("SELECT password FROM USERS WHERE apikey = %s", (request.apikey,))

you are much better off with something like this: 像这样您会变得更好:

db.cursor.execute("SELECT password FROM USERS WHERE apikey = {0}".format(str(request.apikey))) db.cursor.execute(“从用户那里选择密码apikey = {0}”。format(str(request.apikey)))

given that request.apikey can be implicitly converted into a string 鉴于request.apikey可以隐式转换为字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "Python MySQLdb TypeError:字符串格式化期间并非所有参数都转换" - Python MySQLdb TypeError: not all arguments converted during string formatting Python MySQLdb Insert TypeError:在字符串格式化期间并非所有参数都已转换 - Python MySQLdb Insert TypeError: not all arguments converted during string formatting Python MySQLDB-并非在格式化字符串时转换所有参数 - Python MySQLDB - not all arguments converted during string formatting Python MySQLdb TypeError(“并非在格式化字符串时转换所有参数”) - Python MySQLdb TypeError(“not all arguments converted during string formatting”) Python MySQLdb '并非所有 arguments 在字符串格式化期间转换' - Python MySQLdb 'not all arguments converted during string formatting' MySQLdb删除查询-TypeError:在字符串格式化期间并非所有参数都已转换 - MySQLdb delete query - TypeError: not all arguments converted during string formatting MySQLdb:Pandas数据帧到SQL数据库的错误:字符串格式化期间并非所有参数都已转换 - MySQLdb: Pandas data-frame to SQL database error: not all arguments converted during string formatting 带有Selenium错误TypeError的Python:在字符串格式化期间并非所有参数都已转换 - Python with Selenium error TypeError: not all arguments converted during string formatting python错误TypeError:并非所有参数都在字符串格式化期间转换 - python error TypeError: not all arguments converted during string formatting Python错误:在字符串格式化期间,并非所有参数都已转换 - Python Error: Not all arguments converted during string formatting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM