example_table
has id (int) auto increment, json_col (text)
This is the example records from my example_table
that I want to insert the dictionary rows to
+----+----------------------------------------------+
| id | json_col |
+----+----------------------------------------------+
| 1 | {"NO":"1","NIM":"4313010001","NAME":"BENJI"} |
| 2 | {"NO":"2","NIM":"4313010002","NAME":"KENJI"} |
| 3 | {"NO":"3","NIM":"4313010003","NAME":"RYUJI"} |
+----+----------------------------------------------+
This is the array of dictionaries that I have in my python script
dict_result =
[{'NIM': '4313010013','NAME': 'YUKA','STATUS': 'PASSED'},
{'NIM': '4313010008','NAME': 'YUKO','STATUS': 'PASSED'},
{'NIM': '4313010017','NAME': 'YUKI','STATUS': 'PASSED'}]
I'm trying to insert each of them into the example_table
like the example records above. This is what I do, I tried to cast it into a string to perhaps it could pass the row as it is just like the example records above but it raised error saying ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''NIM': '4313010013', 'NAME': 'YUKA', 'STATUS': 'PASSED'})' at line 1")
for i in dict_result:
x = str(i)
cursor.execute("INSERT INTO example_table (json_col) values ({x})".format(x=x))
I have read other questions but in other cases it makes the dictionaries into columns which in my case I want to insert it as it is just like a json. What should I do?
SOLVED With help from comments and added information from Danya02 this line solved the problem
for i in dict_hasil:
x = str(i)
cursor.execute("""INSERT INTO example_table (json_col) values ("{x}")""".format(x=x))
conn.commit()