R
R
Redzonetree2022-03-14 10:04:06
Python
Redzonetree, 2022-03-14 10:04:06

How to set variable table name in postgresql python?

Hello!
I'm trying to create a table through code, using a variable as its name, but it doesn't work out.

the code :

db_object.execute(f'SELECT user_id FROM {message.chat.id} WHERE user_id')
  	result = db_object.fetchone()

  	print(result)
.

mistake :
syntax error at or near "-"
LINE 1: SELECT user_id FROM -1001532563722 WHERE user_id


The minus cannot be removed in any way, because this is the chat id and through it I will check the existence of tables.
(I thought about removing the minus through replaceand then just adding it when reading, but the code below speaks for itself)

An attempt to write the name manually also failed:
db_object.execute('CREATE TABLE IF NOT EXISTS {}'.format('test'))
  	result = db_object.fetchone()

  	print(result)


syntax error at end of input
LINE 1: CREATE TABLE IF NOT EXISTS test


The text in the title has changed several times, but the error is still the same.

Maybe I don't understand something?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-03-14
@Redzonetree

1. You need to create one table, not a bunch with different chat.id
2. CREATE TABLE wants, in addition to the name, also a description of the structure

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question