mycursor.execute не будет вставлять строки - PullRequest
0 голосов
/ 07 апреля 2019

У меня есть диск-бот, который возьмет некоторый пользовательский ввод и вставит его как строку в 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 ничего не меняется. Скорее всего, я маппет, упускающий что-то глупое, но некоторая помощь будет принята с благодарностью. Спасибо

Ответы [ 2 ]

1 голос
/ 07 апреля 2019

Другой способ зафиксировать это

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()
0 голосов
/ 07 апреля 2019

Как оказалось, мне нужно было зафиксировать изменения.Я сделал это с помощью autocommit = True в соединении, извините за потраченное время.

...