views:

514

answers:

3

In python how to read multiple files from mysql database using cursor or loop one by one and store in separate table

+1  A: 

I don't understand your question (what are files?, what's your table structure?), but here goes a simple sample:

>>> import MySQLdb
>>> conn = MySQLdb.connect(host="localhost",
                           user="root",
                           password="merlin",
                           db="files")
>>> cursor = conn.cursor()
>>> cursor.execute("SELECT * FROM files")
5L
>>> rows = cursor.fetchall()
>>> cursor.execute("CREATE TABLE destination (file varchar(255))")
0L
>>> for row in rows:
...   cursor.execute("INSERT INTO destination VALUES (%s)" % row[0])
...
1L
1L
1L
1L
1L
Vinko Vrsalovic
A: 

Here is an example, assuming you have created the table you want to move to, with descriptive names:

>>> import MySQLdb
>>> conn = MySQLdb.connect(user='username', db='dbname')
>>> cur = conn.cursor()
>>> cur.execute('select files from old_table where conditions=met')
>>> a = cur.fetchall()
>>> for item in a:
...     cur.execute('update new_table set new_field = %s' % item) # `item` should be tuple with one value, else use "(item,)" with comma
bvmou
A: 

@Vinko, @bvmou: Have a look at his other post (check also this one).

If I understand that post, he has a MySQL table where 'urlid' is the primary key, and I guess it is an URL. He wants do download the corresponding page/document/whatever, to compute its md5 or another hash function ("fingerprint"), and to store that md5 in a different table having the same URL as the primary key (I can't see why there should be another table).

Perhaps he also wants to store the files directly in the MySQL database, one per row, in the second table.

I think he's trying to monitor the changes in a set of web pages, or to find very similar pages within that set, in which case hash functions wouldn't do the job.

Federico Ramponi