如何使用光标在Python中读取数据库中的多个文件

如何使用光标在Python中读取数据库中的多个文件

问题描述:

在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

我不明白你的问题(什么是文件?什么是你的表结构?),但在这里不用一个简单的示例:

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

下面是一个例子,假设你已经创建的表,你想要移动到描述性名称:

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