gpt4 book ai didi

python - mysqldb : error with Select/execute/escape_string

转载 作者:行者123 更新时间:2023-11-28 23:36:37 26 4
gpt4 key购买 nike

在 linux2 上的 Python 2.7.10 [GCC 5.2.1 20151010] 上执行此代码

import flask
from MySQLdb import escape_string as thwart
username="abc"

conn = MySQLdb.connect(host="localhost",user="root", passwd="xxxxxxx", db="pythonprogramming")
c = conn.cursor()
x = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))

我收到以下错误:

Traceback (most recent call last):
File "", line 1, in TypeError: must be impossible, not str

这是我电脑上的 MySQL 版本

+-------------------------+------------------------------+
| Variable_name | Value

+-------------------------+------------------------------+
| innodb_version | 5.7.11

| protocol_version | 10

| slave_type_conversions |

| tls_version | TLSv1,TLSv1.1

| version | 5.7.11

| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64

| version_compile_os | Linux

+-------------------------+------------------------------+

最佳答案

下面的代码对我有用,查询变量 x 返回“1L”

`param1="john"
x = c.execute("""SELECT * FROM users WHERE username = %s """, (param1,))
`

虽然我不太明白为什么下面的代码不起作用

param1="john"
x = c.execute("""SELECT * FROM users WHERE username = %s """ % (param1,))

param1="john"
x = c.execute("""SELECT * FROM users WHERE username = %s """ , (param1))

关于python - mysqldb : error with Select/execute/escape_string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35586450/

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