Python MySQL : Select with default value

标签 python mysql sql

我创建了一个数据库

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 上执行时:

sql = "select idip from ip where ip=%s and mask=%s" % (long(next_hop), 'DEFAULT')
cursor.execute(sql)
idnext_hop = cursor.fetchone()[0]

我收到以下错误:

    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

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

最佳答案

mysql.connector uses %s instead of ? as the parameter marker ,但是您通过使用 Python 字符串格式来规避它。试试这个:

sql = "select idip from ip where ip=%s and mask=%s"
cursor.execute(sql, (long(next_hop), 'DEFAULT'))
idnext_hop = cursor.fetchone()[0]

关于Python MySQL : Select with default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381839/

相关文章:

python - 如何键入具有不同类型值的字典提示

javascript - Flask - 让 JavaScript 和 Python 进行通信

php - 分页未传递总页值

sql - 在 OPENQUERY 中包含参数

mysql - 昂贵的查询导致数据库服务器瘫痪——寻找缓解方法

python - 我的 Python 类中的奇怪行为

python - 如何在Hadoop中读取相应文件中的文件名和字数?

mysql - 使用 sailsJS 未将值插入 mysql 数据库

php - 在 GitHub 上 stash 公共(public)协作的数据库登录信息的最佳方法是什么?

sql - 在 1 个表扫描中对多个列操作进行分组