Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果我们使用DictCursor,在pymysql中如何定义键?_Python_Mysql_Python 3.x_Pymysql - Fatal编程技术网

Python 如果我们使用DictCursor,在pymysql中如何定义键?

Python 如果我们使用DictCursor,在pymysql中如何定义键?,python,mysql,python-3.x,pymysql,Python,Mysql,Python 3.x,Pymysql,在通过pymysql对MySQL表运行查询后,我正在寻找一种更好地表示我们得到的输出的方法,无意中发现了DictCursor 我尝试了一下,并实现了以下目标 import pymysql # Connect to mysql database conn = pymysql.connect('db_conn_str', 'user_r', 'pwd_r', 'db_name') # Run a query to list all tables in

在通过pymysql对MySQL表运行查询后,我正在寻找一种更好地表示我们得到的输出的方法,无意中发现了
DictCursor

我尝试了一下,并实现了以下目标

import pymysql

# Connect to mysql database
conn = pymysql.connect('db_conn_str', 'user_r',
                       'pwd_r', 'db_name')

# Run a query to list all tables in database
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute('show tables')
tables = cur.fetchall()

# We get a list of dictionaries
print(tables)
我得到如下输出

[
    {'Tables_in_db_name': 'table_x'}, 
    {'Tables_in_db_name': 'table_y'}, 
    {'Tables_in_db_name': 'table_z'}
]

我想知道我们是否可以定义自己的名字中的键
Tables\u?如果没有,则如何选择关键点的名称。

关键点来自结果集的标题

如果运行命令
SHOW TABLES
,则无法更改标题。以下是mysql客户端中此命令的示例:

mysql> show tables from test;
+----------------+
| Tables_in_test |
+----------------+
| bar            |
| foo            |
+----------------+
如果需要更多的控制,可以查询信息模式(这正是
showtables
在内部实际执行的操作):

mysql> select table_name as `my_custom_key` from information_schema.tables 
  where table_schema='test';
+---------------+
| my_custom_key |
+---------------+
| bar           |
| foo           |
+---------------+