0

I'm trying to do a simple insert with python

mydb = mysql.connector.connect(
  host="localhost",
  user="**",
  passwd="**"
)

mycursor = mydb.cursor()

sql = "INSERT INTO sponge_cakes.links (`url`) VALUES (%s)"
val = ("test1")

mycursor.executemany(sql, val)

mydb.commit()

This sort of works, but it inserts the string character by character (creating a new entry for each character)

How can I get it to insert the entire string so it looks like this

id  | link
1   | test1

Instead of

id  | link
1   | t
2   | e
...

2 Answers2

0

mycursor = mydb.cursor()

sql = "INSERT INTO sponge_cakes.links (url) VALUES (%s)"

val = ("Dora")

mycursor.execute(sql, val)

mydb.commit() #make the changes

print(mycursor.rowcount, "record inserted.")

try it!.

ABCD
  • 379
  • 1
  • 8
  • 25
0

The problem is you're using mycursor.executemany(sql, val) which is meant to run the same SQL statement with multiple values.

It might look like "test1" is just a single word, but a word can also be considered as a series of characters, and that's what executemany is interpreting it as.

Change it to mycursor.execute(sql, val) and it'll work as expected with a single value.

Mani Gandham
  • 7,688
  • 1
  • 51
  • 60