【问题标题】:I am working in terminal application(Python+MySQL)我在终端应用程序中工作(Python+MySQL)
【发布时间】:2020-03-31 00:52:36
【问题描述】:
def view_empdetails():  #this is my function: it works great
    conn = mysql.connector.connect(host="localhost",user="root",passwd="#####",database="#DB")
    cursor = conn.cursor()  # this is database connection
    viw = """select * from employees"""
    cursor.execute(viw)
    for emp_no,first_name,last_name,gender,DOB,street,city,state,zipcode,email,phone,hire_date in cursor.fetchall():  # fetch all data from employee table in DB
        print('-'*50)
        print(emp_no)
        print(first_name)
        print(last_name)
        print(gender)
        print(DOB)
        print(street)
        print(city)
        print(state)        # I need all these output be in a table or organize format 
        print(zipcode)      #not only list of records
        print(email)
        print(phone)
        print(hire_date)
        print('-'*50)
    conn.commit()
    conn.close()
    return menu2()

我需要一张表中的所有记录||代码从数据库中逐行获取数据而不进行格式化>我需要它们在表中

【问题讨论】:

    标签: python mysql terminal


    【解决方案1】:

    我不确定您是否熟悉 pandas 库,但我相信它在这里会有所帮助。我从来没有用过mysql,但我用过psycopg2和pyodbc,所以我认为基本的想法应该可行:

    data = pd.DataFrame(cur.fetchall(),columns = colnames)
    

    创建一个使用您正在查询的表中的列名的数据框(想想 python 电子表格或 python 表)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      相关资源
      最近更新 更多