M
M
MarkSmersh2021-07-06 05:39:09
MySQL
MarkSmersh, 2021-07-06 05:39:09

Why are mysql changes not being applied?

def scripts_send_messages_1(update: Update, context: CallbackContext):
    chat_id = update.callback_query.from_user['id'] #int значение
    mydb = mysql.connector.connect(
        host = 'host ',
        user = 'user',
        password = 'password',
        database = 'database')
    dbc.execute (f'SELECT vk_id FROM user WHERE chat_id={chat_id}') #dbc = mydb.cursor() (глобальная переменная)
    user_vk_id = dbc.fetchone()[0] #int значение
    dbc.execute = ('INSERT INTO history (thread_type, chat_id, vk_id, method) VALUES (%s, %s, %s, %s)'
                  % ('messages_send', chat_id, user_vk_id, 0))
    mydb.commit()
    bot.send_message (text = 'Отправьте *ссылку на группу* из которой будут использоваться посты:',
                      chat_id = chat_id,
                      parse_mode = ParseMode.MARKDOWN)
    return REPLY_IDS_GROUP


SELECT works fine

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
ScriptKiddo, 2021-07-06
@ScriptKiddo

The dbc cursor is from another connection, and you are committing to the local mydb connection.
Create a local cursor and run queries with it

M
MarkSmersh, 2021-07-06
@MarkSmersh

I decided to insert manually in MySQL Workbench, into the table + one of the values ​​was not quite an int, but this did not prevent the insert from working. After that everything started working. I understand that there is no logic in this, but the fact remains. The final code looks like this:

def scripts_send_messages_1(update: Update, context: CallbackContext):
    chat_id = update.callback_query.from_user['id']
    mydb = mysql.connector.connect(
        host = 'host ',
        user = 'user ',
        password = 'password ',
        database = 'database ')
    dbc = mydb.cursor()
    dbc.execute (f'SELECT vk_id FROM user WHERE chat_id={chat_id}')
    user_vk_id = dbc.fetchone()[0]
    sql = 'INSERT INTO history (thread_type, chat_id, vk_id, method) VALUES (%s, %s, %s, %s)'
    val = ('messages_send', chat_id, int(user_vk_id), 0)
    dbc.execute(sql, val)
    mydb.commit()
    bot.send_message (text = 'Отправьте *ссылку на группу* из которой будут использоваться посты:',
                      chat_id = chat_id,
                      parse_mode = ParseMode.MARKDOWN)
    return REPLY_IDS_GROUP

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question