当前位置:Gxlcms > 数据库问题 > Python从数据库中读取数据,并打印表格展示数据。

Python从数据库中读取数据,并打印表格展示数据。

时间:2021-07-01 10:21:17 帮助过:65人阅读

!/usr/bin/env python3 # -*- coding: utf-8 -*- #----------------------------------------------------------# # Date : xxxx-xx-xx # # Author : Created by zhouwanchun. # # Wechat : loveoracle11g # # Function: This scripts function is ... # # Version : 1.1 # #----------------------------------------------------------# ### Python从数据库中读取数据,并打印表格展示数据。 # 导入模块 import os import subprocess import mysql.connector import myloginpath import prettytable as pt # Linux终端清屏 os.system(clear) # 注释信息 print("""\033[1;36m ############################################################ # Date : 2020-05-22 # # Author : Created by zhouwanchun. # # Wechat : loveoracle11g # # Function: This scripts function is ... # # Version : v1.1 # ############################################################ \033[0m""") # 连接数据库 mylogin = myloginpath.parse(rds_dba) # print(mylogin, type(mylogin)) conn = mysql.connector.connect(**mylogin) # 创建SQL命令通道 sql_cmd = conn.cursor() # SQL语句 ### 检查实例参数 sql1 = "select user,host from mysql.user;" sql_cmd.execute(sql1) sql1_result = sql_cmd.fetchall() # 设置列头 tb.field_names = [‘user‘, ‘host‘] # 添加行 tb.add_row([‘1‘, ‘xx‘, ‘yy‘]) # 添加列 tb.add_column(‘status‘, [1, 1, 1]) # 设置对其方式:l左对齐,r右对齐,c居中(不设置默认是居中对齐) tb = pt.PrettyTable() tb.field_names = [user, host] tb.align[user] = l tb.align[host] = l for i in sql1_result: tb.add_row(list(i)) print("\033[1;32m查看MySQL账号名\033[0m") print(tb) sql_cmd.close() conn.commit() conn.close()

技术图片

 

Python从数据库中读取数据,并打印表格展示数据。

标签:sel   field   取数据   查看   python3   user   row   表格   通道   

人气教程排行