首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python MySQL:带有默认值的选择

Python MySQL:带有默认值的选择
EN

Stack Overflow用户
提问于 2018-05-17 00:51:40
回答 2查看 1.3K关注 0票数 0

我有一个数据库是从

代码语言:javascript
复制
CREATE TABLE `ip` (
  `idip` int(11) NOT NULL AUTO_INCREMENT,
  `ip` decimal(45,0) DEFAULT NULL,
  `mask` int(11) DEFAULT NULL,
  PRIMARY KEY (`idip`),
  UNIQUE KEY `ip_UNIQUE` (`ip`)
)

我在这张桌子上做了一些插入

但是当我试图在python上执行时:

代码语言:javascript
复制
sql = "select idip from ip where ip=%s and mask=%s" % (long(next_hop), 'DEFAULT')
cursor.execute(sql)
idnext_hop = cursor.fetchone()[0]

我得到以下错误:

代码语言:javascript
复制
    Inserting routes into table routes (1/377)...('insere_tabela_routes: Error on insertion at table routes, - SQL: ', 'select idip from ip where ip=0 and mask=DEFAULT')
1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

有没有人知道问题出在哪里?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-17 01:39:45

as the parameter marker,但您可以通过使用Python格式来规避它。试试这个:

代码语言:javascript
复制
sql = "select idip from ip where ip=%s and mask=%s"
cursor.execute(sql, (long(next_hop), 'DEFAULT'))
idnext_hop = cursor.fetchone()[0]
票数 1
EN

Stack Overflow用户

发布于 2018-05-17 00:54:36

您正在使用参数来处理查询字符串,而不是将它们作为参数传递进来。代码应该如下所示:

代码语言:javascript
复制
sql = "select idip from ip where ip = ? and mask = ?"
cursor.execute(sql, (long(next_hop), 'DEFAULT'))
idnext_hop = cursor.fetchone()[0]

换句话说,您希望查询引擎在查询中执行替换。您不希望Python对查询字符串进行替换。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50381839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档