Python: use mysqldb to import a MySQL table as a dictionary?(Python:使用 mysqldb 将 MySQL 表作为字典导入?)
问题描述
有谁知道我如何使用 mysqldb 将包含大量行的 MySQL 表转换为 Python 中的字典对象列表?
Anyone know how I can use mysqldb to turn a MySQL table, with lots of rows, into a list of dictionary objects in Python?
我的意思是将一组包含a"、b"和c"列的 MySQL 行转换为如下所示的 Python 对象:
I mean turning a set of MySQL rows, with columns 'a', 'b', and 'c', into a Python object that that looks like this:
data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 }, { 'a':'Q', 'b':(1, 4), 'c':5.0 }, { 'a':'T', 'b':(2, 8), 'c':6.1 } ]
谢谢:)
推荐答案
MySQLdb 为此有一个单独的游标类,即 DictCursor.您可以将要使用的游标类传递给 MySQLdb.connect():
MySQLdb has a separate cursor class for this, the DictCursor. You can pass the cursor class you want to use to MySQLdb.connect():
import MySQLdb.cursors
MySQLdb.connect(host='...', cursorclass=MySQLdb.cursors.DictCursor)
这篇关于Python:使用 mysqldb 将 MySQL 表作为字典导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python:使用 mysqldb 将 MySQL 表作为字典导入?


基础教程推荐
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01