How to fix the error when trying to add a row in mysql: TypeError: must be string or read-only buffer, not tuple?

Hello!
Inevitably get the following error:

TypeError: must be string or read-only buffer, not tuple.


Code:
for ii in profiles_groups:
cntr+=1
 name = ii["name"]
 gid = ii["gid"]
 screen_name = ii["screen_name"]

print(type(name))
print(type(gid))
print(type(name))

 sql = "INSERT INTO groups(group_id, group_name, short_link) VALUES (%s, %s, %s)", (gid, name, screen_name)

cursor.execute(sql)
db.commit()

db.close()


In this case, all variables of the row:

<type 'str'>
<type 'str'>
<type 'str'></type></type></type>
July 8th 19 at 11:51
1 answer
July 8th 19 at 11:53
Solution
You do NOT need
There is not PHP
sql = "INSERT INTO groups(group_id, group_name, short_link) VALUES (%s, %s, %s)" , (gid, name, screen_name)
print(type(sql))

sql = "INSERT INTO groups(group_id, group_name, short_link) VALUES (%s, %s, %s)" % (gid, name, screen_name)
print(type(sql))


You need to do through
https://dev.mysql.com/doc/connector-python/en/conn...

sql = "INSERT INTO groups(group_id, group_name, short_link) VALUES (%s, %s, %s)" 

cursor.execute(sql, (gid, name, screen_name))


or so
sql_prepared_text = "INSERT INTO groups(group_id, group_name, short_link) VALUES (%s, %s, %s)", (gid, name, screen_name)

cursor.execute(*sql_prepared_text)
Thank you very much for the answer!
Helped:
cursor.execute(sql, (gid, name, screen_name)) - sierra.Mr commented on July 8th 19 at 11:56

Find more questions by tags MySQLPython