开发者

How can I write a Python list to persistent storage?

I am currently trying to solve this problem in MySQL. I have MySQLdb installed. What is the best way to write a list in Python, e.g.

[1, 2, -1, -2]

to a column of a MySQL database table? I would like each consecutive entry of the list to be put into consecutive rows of a MySQL column. I can obviously run individual queries in a loop, but I was wondering if there is any better/faster way.

I am also willing to experiment with different data storage techniques if this isn't possible using MySQLdb. I am working with lists that contain several million entries each, and need to read/write quickly to some form of persistent storage that can store these many different lists as columns.

UPDATE: Thanks for yo开发者_JAVA百科ur responses. After being led to search for execute many, I found this link, which had a useful example of executemany().


You're looking for executemany().


Caution: There is no notion of consecutive rows in SQL.

The order of input is not recorded in the SQL database unless you also have an auto increment column that is automatically updated as you insert data or some other way to order the data on input, and the order of output depends on your ORDER BY clause.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜