M
M
Morrowind2021-06-08 10:56:19
Python
Morrowind, 2021-06-08 10:56:19

How to write different types of data with Python to SQL table?

Hey!

I have a list with different types of values:
[561906993, 52262538, '2003-12-12 21:52:09', 'Fall', '0.0000320', '0.01929000', '0.00147425']

And a way to add new information to the table

cursor = connection.cursor()
        print(addinfo)
        # Выполнение SQL-запроса для вставки данных в таблицу
        insert_query = f""" INSERT INTO postgreetest_db (t1, t2, t3, t4,t5,t6,t7) VALUES (561906993, 52262538, '2003-12-12 21:52:09', 'Fall', '0.0000320', '0.01929000', '0.00147425')"""


An example of what I would like:

resultat = [561906993, 52262538, '2003-12-12 21:52:09', 'Fall', '0.0000320', '0.01929000', '0.00147425']

 cursor = connection.cursor()
        print(addinfo)
        # Выполнение SQL-запроса для вставки данных в таблицу
        insert_query = f""" INSERT INTO postgreetest_db (t1, t2, t3, t4,t5,t6,t7) VALUES ({resultat })"""
cursor.execute(insert_query)
connection.commit()
cursor.execute("SELECT * from postgreetest_db")
 record = cursor.fetchall()
 print("Результат", record)


In the above example, naturally, an error occurs referring to '[' at the beginning and at the end of the list.

The solution using for to add to the record in turn 1 value from the list will not help speed up the work in my opinion.
Or to translate everything into a str value and then, when reading back into the desired format, it is also not desirable to translate.

I ask for advice or an example of how to enter the necessary data at once using a variable?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-06-08
@hekkaaa

This is done through value substitution

insert_query = """ INSERT INTO postgreetest_db (t1, t2, t3, t4, t5, t6, t7) VALUES (%s, %s, %s, %s, %s, %s, %s)"""
cursor.execute(insert_query, resultat)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question