mycursor.execute 不会插入行
mycursor.execute wont insert rows
我有一个 discord 机器人,它会接受一些用户输入并将其作为一行插入 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 上没有任何更改。很可能我是一个布偶,遗漏了一些愚蠢的东西,但我们将不胜感激。谢谢
事实证明,我需要提交更改。
我通过在连接中使用 autocommit=True 来做到这一点
抱歉浪费了您的时间。
另一种提交方式是
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()
我有一个 discord 机器人,它会接受一些用户输入并将其作为一行插入 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 上没有任何更改。很可能我是一个布偶,遗漏了一些愚蠢的东西,但我们将不胜感激。谢谢
事实证明,我需要提交更改。 我通过在连接中使用 autocommit=True 来做到这一点 抱歉浪费了您的时间。
另一种提交方式是
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()