python - torndb(MySQLdb) 查询参数不起作用

标签 python mysql mysql-python

像这样:

In [43]: conn.query('select %s from topic order by %s limit 2', 'id', 'id desc')
Out[43]: [{'id': u'id'}, {'id': u'id'}]
In [44]: conn.query('select id from topic order by id desc limit 2')
Out[44]: [{'id': 10L}, {'id': 9L}]

为什么结果不一样?

最佳答案

这是因为 MySQLdb 驱动程序将查询参数插入到查询中的方式。

第一个查询实际上转化为(看引号):

select 'id' from topic order by 'id desc' limit 2

'id'这里是字符串,不是列名。

关于python - torndb(MySQLdb) 查询参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361832/

相关文章:

Python - 具有多个(目标列表 "=")组的赋值语句有优点/缺点吗?

python - nbdev 和 'coroutine' 对象不可下标

mysql - 无法确定我需要表中的多条记录的 MySQL 查询

prepared-statement - 在 MySQL Python 准备语句中替换文字

python - 尚未在model.summary()上建立此模型错误

python - Pandas :groupby 和可变权重

php - 在 PHP 中如何获取包含 PREPARE 和 EXECUTE 的 SQL 查询返回的行?

php - PHP 的 MySQL password() 函数

python - 为多个数据库设置一个 MySQLdb 连接对象

python - 我无法使用 mysqldb 与 lampp 服务器连接我的 python 程序