2008-10-22 43 views
0

在Python中你怎麼從一個使用光標或一環一個MySQL數據庫中讀取多個文件和輸出存儲在一個單獨的表?如何使用光標在Python中讀取數據庫中的多個文件

+0

請參閱http://stackoverflow.com/questions/221533/how-to-read-multiple-files-from-mysql-database-in-python。同樣的問題。不同的一天。 – 2008-10-22 09:39:00

回答

1

我不明白你的問題(什麼是文件?什麼是你的表結構?),但在這裏不用一個簡單的示例:

>>> 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 
0

下面是一個例子,假設你已經創建的表,你想要移動到描述性名稱:

>>> 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 
相關問題