0
I have the following script:
import pymysql
cobertura = (10, 20)
base_de_dados = pymysql.connect("localhost", "root", "senha", "base_de_dados")
cursor = base_de_dados.cursor()
cursor.executemany("INSERT INTO individuo VALUES (NULL, %s)", (cobertura))
base_de_dados.commit()
base_de_dados.close()
When I run, instead of the values (10, 20) being saved as tuples in my database, they are written on different lines. For example, value 10 in column 1 row 1 and value 20 in column 1 row 2.
How do I save values as tuples in the same cell?
Thank you who can help me!
You want to save in the bank to string
(10, 20)
?– Woss
Exactly that @Andersoncarloswoss
– Danilo
Try placing a comma after the
cobertura
:(cobertura,)
. Without it, Python does not define a tuple, but rather uses parentheses to control the preceedences of the expression. With the comma he will understand that what he wants to do is create a tuple.– Woss
I edited the code this way: cursor.executemany("INSERT INTO individual VALUES (NULL, %s)", (coverage,) Presented error: Typeerror: not all Arguments converted During string formatting
– Danilo
Then you’ll need to convert the tuple to string:
(str(cobertura),)
– Woss
Complicated like that. The message says that the content has been truncated, so you probably set the table wrong. Type and length of column
cobertura
?– Woss
@Andersoncarloswoss, first excuse, the solution you passed : Then you need to convert the tuple to string: (str(coverage),). It worked correctly, I had forgotten to modify the data type in my table to VARCHAR. Thank you very much! Do not want to post as response?
– Danilo