簡體   English   中英

如何使用python 3.5將mysql數據導入.txt文件?

[英]How to import mysql data to .txt file using python 3.5?

我試圖使用python 3.x將mysql數據導入.txt文件,但看起來我錯過了什么。期望是,數據應該以表格/列格式導入文件。 我嘗試了最好的水平以獲得解決方案,但我沒有得到我需要的東西。

以下是我的代碼:

import pymysql.cursors
import pymysql
import sys
import os

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='root',
                             password="",
                             db='jmeterdb',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        # Select all records
        sql = "select * from emp"
        cursor.execute(sql)

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    result = cursor.fetchall()
    newfile = open("db-data.txt","a+")
    for row in result:
        newfile.writelines(row)

    print(result)
    newfile.close()

finally:
    connection.close()

在終端python上顯示執行print(result)時的數據,但是在db-data.txt文件中,它只顯示列名。

預期結果 :

Column_Name1 Column_Name2 Column_Name3
data1        data2        data3
data1        data2        data3

此代碼產生的上述問題的預期輸出如下:

import pymysql.cursors
import pymysql
import sys
import os

# Open database connection
connection = pymysql.connect(host='localhost',
                             user='root',
                             password="",
                             db='jmeterdb',
                             cursorclass=pymysql.cursors.DictCursor)
# prepare a cursor object using cursor() method
with connection.cursor() as cursor:
# Prepare SQL query to select a record into the database.
    try:

        sql = "SELECT * FROM EMP order by ename asc"
# Execute the SQL command
        cursor.execute(sql)
# Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        # print(results)
        if results:
            newfile = open("db-data.txt","a+")
            newfile.write('ename'+"\t"+'jobs'+"\t"+"salary"+"\t"+'comm'+"\t"+'manager'+"\t"+'hiredate'+"\t"+'deptno'+"\t"+'empno'+"\n")

        for index in results:
            ltr=[]
            ltr.append(index['ename'])
            ltr.append(index['job'])
            ltr.append(index['sal'])
            ltr.append(index['comm'])
            ltr.append(index['mgr'])
            ltr.append(index['hiredate'])
            ltr.append(index['deptno'])
            ltr.append(index['empno'])
            lenltr=len(ltr)
            for i in range(lenltr):
                newfile.write('{}'.format(ltr[i]))
                newfile.write("\t")
                print(ltr[i])
            newfile.write("\n")


# # Now print fetched result
        #print("ename=%s,empno=%d,job=%d,hiredate=%d,comm=%s,sal=%d,deptno=%d,mgr=%d" %(ename, empno, job, hiredate, comm, sal, deptno, mgr))
        # print(index)
    except:
        print ("Error: unable to fecth data")
# disconnect from server
connection.close()
newfile.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM