首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中如何使用游标读取数据库中的多个文件

在python中如何使用游标读取数据库中的多个文件
EN

Stack Overflow用户
提问于 2008-10-22 07:41:51
回答 2查看 1.3K关注 0票数 0

在python中,如何使用游标或循环逐个从mysql数据库中读取多个文件,并将输出存储在单独的表中?

EN

回答 2

Stack Overflow用户

发布于 2008-10-22 08:05:51

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

代码语言:javascript
复制
>>> 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
票数 1
EN

Stack Overflow用户

发布于 2008-10-22 08:19:48

下面是一个示例,假设您已经创建了要移动到的表,并使用描述性名称:

代码语言:javascript
复制
>>> 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/224771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档