在python中显示选择Mysql查询,输出美观且可读

时间:2016-09-27 09:41:22

标签: python mysql sql python-2.7 web

我需要python脚本来显示sql查询,输出很好并且可读,这对于重表来说是不可读的......

cnx = mysql.connector.connect(user='root', password='*****',
                              host='127.0.0.1',
                              database='dietetique')
c = cnx.cursor()
sys.stdout = open('mysql_data.log', 'w')
c.execute("SELECT * FROM administrations;")

for row in c:
    print row

2 个答案:

答案 0 :(得分:0)

import pypyodbc
ID=2
ConnectionDtl='Driver={SQL Server};Server=WIN7-297;Database=AdventureWorks2014;trusted_connection=yes'
connection = pypyodbc.connect(ConnectionDtl)

print("Retrieve row based on [FirstName]='Mani'")
cursor = connection.cursor()
SQLCommand = ("SELECT [FirstName],[LastName] "
               "FROM Person.SampleData "
               "WHERE FirstName =?")
Values = ['Mani']
print(SQLCommand)
cursor.execute(SQLCommand,Values)
i=1
for x in cursor :
  row = cursor.fetchone()
  print str(i) + ". FirstName: " + row[0] + " LastName: " + row[1]
  i=i+1

connection.close()

答案 1 :(得分:0)

只需向Sql Query添加限制即可执行相同的代码。

cnx = mysql.connector.connect(user='root', password='*****',
                      host='127.0.0.1',
                      database='dietetique')
c = cnx.cursor()
sys.stdout = open('mysql_data.log', 'w')
limitvalue=1000
for offsetvalue in range(0 , maximum_rows_you_want,1000):
    c.execute("SELECT * FROM administrations limit "+ limitvalue + " offset " + offsetvalue +";")
    for row in c:
        print row