T
T
Tayrus02020-05-12 16:23:00
Python
Tayrus0, 2020-05-12 16:23:00

Why am I getting sqlite3.OperationalError: near "any word": syntax error?

Here is the function:

def write_comment(message):
        conn = sqlite3.connect("m.db")

        cursorObj = conn.cursor()
        cursorObj.execute(
            f'SELECT comment_text FROM settings WHERE id = {message.chat.id}')

        rows = cursorObj.fetchall()
        if len(rows) == 0:
            cursorObj.execute(
                'INSERT INTO settings(id,comment_text) VALUES(?,?)', (message.chat.id, message.text))
            conn.commit()
        cursorObj.execute(
            f'UPDATE settings SET comment_text = {message.text} WHERE id = {message.chat.id}')

        conn.commit()
        bot.send_message(
            message.chat.id, "✔️ Значение записано")


I created the table like this:

cursorObj.execute(
            "CREATE TABLE IF NOT EXISTS settings(comment_text text, post_text text, wall_user_id integer, id integer)")


When I enter text into the bot, I get:
File "run_server.py", line 136, in write_comment
    f'UPDATE settings SET comment_text = {message.text} WHERE id = {message.chat.id}')
sqlite3.OperationalError: no such column: tertert


But if I enter a number, then there is no error, why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-05-12
@Tayrus0

Because you should not use string interpolation to form SQL queries.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question