【问题标题】:python mysql in order number input to table idpython mysql中的订单号输入到表ID
【发布时间】:2016-04-22 16:04:14
【问题描述】:

我正在尝试使用字符串和订单号将 id 添加到我的表中。代码如下:

import pymysql.cursors
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='xxx', db='python')


with conn.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `arzugulgundogdu2` (`id`) VALUES ('dgNotListesi_txtGelisimDurumu_'%s)"  


        y = int("0")
        while y < 10:

            cursor.execute(sql, (y))
            y = y+1


conn.commit()

另外,当我为 y 选择一个常量值以进行 while 循环时,它看起来像 dgNotListesi_txtGelisimDurumu_'myconstanthere。 为什么字符串和我的 y 变量之间有一个 ' 签名? 谢谢。我对python很陌生:)

【问题讨论】:

    标签: python mysql string int pymysql


    【解决方案1】:

    为什么字符串和我的 y 变量之间有一个 ' 签名?

    那是因为你在占位符之前有这个额外的单引号:

    ... 'dgNotListesi_txtGelisimDurumu_'%s
                                   HERE^
    

    而是在 Python 中生成完整的值并用它参数化查询:

    sql = "INSERT INTO `arzugulgundogdu2` (`id`) VALUES (%s)" 
    
    value = "dgNotListesi_txtGelisimDurumu_%s" % y
    cursor.execute(sql, (y, ))
    

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 2019-01-14
      • 2015-11-08
      • 2019-04-20
      • 2013-04-03
      • 2012-10-30
      相关资源
      最近更新 更多