如何使Python Msqldb模块使用?代替%s查询参数? [英] How do you make the Python Msqldb module use ? in stead of %s for query parameters?
问题描述
MySqlDb是一个奇妙的Python模块 - 但一部分是令人难以置信的烦人。
查询参数如下所示:
MySqlDb is a fantastic Python module -- but one part is incredibly annoying. Query parameters look like this
cursor.execute("select * from Books where isbn=%s", (isbn,))
而在已知Universe中的其他地方(oracle,sqlserver,access,sybase .. 。)
它们看起来像这样
whereas everywhere else in the known universe (oracle, sqlserver, access, sybase...) they look like this
cursor.execute("select * from Books where isbn=?", (isbn,))
这意味着如果你想要移植你必须以某种方式切换
在两个符号之间?和%s,
这是真的很烦人。 (请不要告诉我使用
ORM图层 - 我会扼杀你)。
This means that if you want to be portable you have to somehow switch between the two notations ? and %s, which is really annoying. (Please don't tell me to use an ORM layer -- I will strangle you).
假设你可以说服mysqldb使用标准语法,但我还没有
使它的工作。任何建议?
Supposedly you can convince mysqldb to use the standard syntax, but I haven't yet made it work. Any suggestions?
推荐答案
我发现很多关于 paramstyle
这似乎意味着它可能是你想要的,但根据到这个维基,你必须使用你的库使用的 paramstyle
,并且他们大多数不允许你改变它:
I found a lot of information out there about paramstyle
that seemed to imply it might be what you wanted, but according to this wiki you have to use the paramstyle
your library uses, and most of them do not allow you to change it:
paramstyle
特定于您使用的库,并且提供信息 - 您必须使用它使用的库。这可能是这个标准最恼人的部分。 (少数允许您设置不同的paramstyles
,但这不是标准行为)
paramstyle
is specific to the library you use, and informational - you have to use the one it uses. This is probably the most annoying part of this standard. (a few allow you to set differentparamstyles
, but this isn't standard behavior)
$ b b
我发现一些帖子谈到MySQLdb允许这个,但显然它不是因为有人表示它不适合他们。
I found some posts that talked about MySQLdb allowing this, but apparently it doesn't as someone indicated it didn't work for them.
这篇关于如何使Python Msqldb模块使用?代替%s查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!