X
X
xxxfdd2021-12-17 22:23:31
Python
xxxfdd, 2021-12-17 22:23:31

An error occurred while creating the table, how to solve it?

Hello everyone, I want to create a table in postgresql using python, but an error occurs( cur.execute("""CREATE TABLE {}
psycopg2.errors.SyntaxError: ERROR: syntax error (approximate position: "688093622")), here is the code:

if user_id_v not in mas_user_id:
                mas_user_id.append(user_id_v)

                print('Новый пользователь БОТА:  ', mas_user_id[-1])
                conn = psycopg2.connect(
                    host='localhost',
                    database='',
                    user='',
                    password='',
                    port=5432
                )
                cur = conn.cursor()

                # Создание таблицы
               # Создание таблицы
                cur.execute(f'''CREATE TABLE {user_id}  
                     (ADMISSION INT PRIMARY KEY NOT NULL,
                     NAME TEXT NOT NULL,
                     AGE INT NOT NULL,
                     COURSE CHAR(50),
                     DEPARTMENT CHAR(50));''')

                print("Table created successfully")
                conn.commit()
                conn.close()
                conn.commit()
                conn.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tomulevich, 2021-12-20
@phoinixrw

cur.execute(f'''CREATE TABLE "{user_id}"  
                     (ADMISSION INT PRIMARY KEY NOT NULL,
                     NAME TEXT NOT NULL,
                     AGE INT NOT NULL,
                     COURSE CHAR(50),
                     DEPARTMENT CHAR(50));''')

If you already use anything in table names, then use double quotes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question