使用tex将Python序列id转换为mysql

2024-09-30 00:24:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用常量字符串创建id号。但当我执行这个程序时,它是把输入数乘以我的输入值。我只想加上:

dgNotListesi\ U txtGelisimDurumu\ U 0公司 第1页 第二阶段 dgNotListesi\ U txtGelisimDurumu\ 3号 ... (输入值。)

以下是m代码:

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


with conn.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `deneme` (`id_1`) VALUES (%s)"  
        x=0
        y = eval(input("please insert number of students:  "))
        while x<y:

            value = "dgNotListesi_txtGelisimDurumu_%s" %y
            x = x+1
            cursor.execute(sql, (value, ))




conn.commit()

非常感谢。你知道吗


Tags: 字符串代码import程序idsqlvalue公司
1条回答
网友
1楼 · 发布于 2024-09-30 00:24:08

您需要使用x变量来构造value。替换:

value = "dgNotListesi_txtGelisimDurumu_%s" %y

使用:

value = "dgNotListesi_txtGelisimDurumu_%s" % x

作为旁注,使用eval()-it is dangerous通常不是一个好主意,请参见:

相关问题 更多 >

    热门问题