【问题标题】:mycursor.execute wont insert rowsmycursor.execute 不会插入行
【发布时间】:2019-08-28 20:10:29
【问题描述】:

我有一个不和谐的机器人,它将接受一些用户输入并将其作为一行插入 mysql。数据已经过验证,但 mysql 库拒绝将其作为行插入。如果我让它打印命令而不是发送它,然后从 mysql 主机执行它,它插入就好了。

async def addsourceserver(ctx, name=None, description=None, ip=None, port=None, query_port=None):
    mydb = msc(ctx.guild.id)
    mycursor = mydb.cursor(buffered=True)
    try:
        # Checking if the database has been made (theres a another command to make the db
    if name == 0 or description == 0 or ip == 0 or port == 0 or query_port == 0:
        #some more checking
    mycursor.execute("select * from source_server_info")
    existingservers = len(mycursor.fetchall()) # finds the instance number it should use when inserting (primary key kind of)
    mydb=msc(ctx.guild.id)
    mycursor = mydb.cursor(buffered=False)
    mycursor.execute(f"use `{str(ctx.guild.id)}`")
    mycursor.execute(f"""INSERT INTO source_server_info (instance, name, description, ip, port, query_port) VALUES ({existingservers+1}, '{name}', '{description}', '{ip}', {port}, {query_port})""")

它应该只是将行插入到 mysql 中。它实际上做什么它绝对没有。没有给出错误,mysql上没有任何改变。我很可能是一个布偶,缺少一些愚蠢的东西,但我们将不胜感激。谢谢

【问题讨论】:

    标签: python-3.x mysql-python discord.py-rewrite


    【解决方案1】:

    事实证明,我需要提交更改。 我通过在连接中使用 autocommit=True 来做到这一点 很抱歉浪费了您的时间。

    【讨论】:

      【解决方案2】:

      另一种提交方式是

      async def addsourceserver(ctx, name=None, description=None, ip=None, port=None, query_port=None):
          mydb = msc(ctx.guild.id)
          mycursor = mydb.cursor(buffered=True)
          try:
              # Checking if the database has been made (theres a another command to make the db
          if name == 0 or description == 0 or ip == 0 or port == 0 or query_port == 0:
              #some more checking
          mycursor.execute("select * from source_server_info")
          existingservers = len(mycursor.fetchall()) # finds the instance number it should use when inserting (primary key kind of)
          mydb=msc(ctx.guild.id)
          mycursor = mydb.cursor(buffered=False)
          mycursor.execute(f"use `{str(ctx.guild.id)}`")
          mycursor.execute(f"""INSERT INTO source_server_info (instance, name, description, ip, port, query_port) VALUES ({existingservers+1}, '{name}', '{description}', '{ip}', {port}, {query_port})""")
          mycursor.commit()
      

      【讨论】:

      • 可以只添加最后一行mycursor.commit() 并解释它而不是复制粘贴整个代码。
      • 我承认这有点过头了,但我是新手。我很抱歉。
      猜你喜欢
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多