Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing python list into mysql and accessing it

Tags:

python

mysql

How can I store python 'list' values into MySQL and access it later from the same database like a normal list?

I tried storing the list as a varchar type and it did store it. However, while accessing the data from MySQL I couldn't access the same stored value as a list, but it instead it acts as a string. So, accessing the list with index was no longer possible. Is it perhaps easier to store some data in the form of sets datatype? I see the MySQL datatype 'set' but i'm unable to use it from python. When I try to store set from python into MySQL, it throws the following error: 'MySQLConverter' object has no attribute '_set_to_mysql'. Any help is appreciated

P.S. I have to store co-ordinate of an image within the list along with the image number. So, it is going to be in the form [1,157,421]

like image 275
user5177957 Avatar asked Oct 21 '25 06:10

user5177957


1 Answers

Use a serialization library like json:

import json

l1 = [1,157,421]
s = json.dumps(l1)
l2 = json.loads(s)
like image 69
luoluo Avatar answered Oct 23 '25 22:10

luoluo