I have the following model:
class test_data_urls(models.Model):
url = models.CharField(max_length=200, db_index=True)
I want to insert a value into mysql:
cursor = connection.cursor()
url = "hiya"
cursor.execute("insert into my_table(url) values (%s)", (url))
I get an error:
‘str’ object has no attribute ‘items’
This works:
cursor.execute("insert into my_table(url) values ('test')")
but I want to do it with %s. To me, it looks exactly like how I have always done this, so what am I missing?
Try using
[and]instead of(and):The reason that (url) does not work is because it’s not a tuple, it’s a single string. (url,) would be a tuple.