views:

183

answers:

2

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 your responses. After being led to search for execute many, I found this link, which had a useful example of executemany().

+2  A: 

You're looking for executemany().

Ignacio Vazquez-Abrams
+2  A: 

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.

Paul
If it doesnt need to be in an SQL database, then you could write to a file. Couldn't you just use pickle() when the list is short, or in the large case, shelve() ?
Paul