J
J
JustMRZ2020-12-15 21:34:53
Python
JustMRZ, 2020-12-15 21:34:53

How to correctly compose a SQL query using python for SQLite?

Good afternoon. I don't know much about this topic, so I ran into a problem: the query produces an error "sqlite3.OperationalError: near ",": syntax error". I have a list_files list which has a name, description, path to the file. When I click a button, I have to create a database, which adds all the information from the list, including the picture, which is written to the BLOB. Here is my code, tell me how to fix it.

if name_bd.get() != "":
    button_create_bd.configure(state = tk.DISABLED)
    with sq.connect(f"{name_bd.get()}.bd") as con:
      cur = con.cursor()
      sql_ex = f"CREATE TABLE IF NOT EXISTS {name_bd.get()} (name TEXT, info TEXT, file BLOB)"
      cur.execute(sql_ex)
      for i in list_files:
        with open(i[2], "rb") as file:
          binary = sq.Binary(file.read())
        sql_ex = f"INSERT INTO {name_bd.get()} VALUES ({i[0]},{i[1]},{[binary]})"
        cur.execute(sql_ex)
  else:
    tmb.showerror("Ошибка", "Имя для БД не выбрано")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2020-12-15
@JustMRZ

at least quotes
'{i[0]}' are needed,
and with binary, most likely only through query parameters, it won't
work
.
cur.execute(sql_ex, ({i[0]},{i[1]},{[binary]}))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question