How to create database and then consult them in excel through python?

0

Hello, I am trying to make a program in which I can add data in an excel file and later consult them, for this I must use the libraries openpyxl (for managing the .xlsx file) and tkinter (for the window where they should be enter the data), my problem is that I do not know how to indicate that the data is saved in each row and not overwritten and how to consult them. The data are (name, sex, age, telephone) these should be stored in a column.

    
asked by D2895 03.10.2017 в 05:52
source

2 answers

0

A simple way is to store the data in plain text as a csv file ( Comma separated value )

Software such as Excel or Libreoffice Calc have no problems opening these files and importing them into your spreadsheet

import csv
outFile = open('fichero.csv','wb')
writer = csv.writer(outFile, delimiter=',')
writer.writerow['columna1', 'columna2', 'columna3']

outFile.close()
    
answered by 03.10.2017 в 09:33
0

Well the truth is quite simple, you can make a for to iterate on each element of your database, you must take into account which library you use to manage the connection with your database, in this example use pymysql :

sql1 = "SELECT 'nombre' 'sexo' 'edad' 'telefono' FROM elnombre_de_tu_tabla"

cursor.execute(sql1)
all = cursor.fetchall() # Aca asignamos a la variable all el resultado

Then you have to add all those values in your application iterating over the elements, usually pymysql returns a dictionary:

indice = 1

for item in all:
    hoja['A%s' % indice].value = item['nombre']
    hoja['B%s' % indice].value = item['sexo']
    hoja['C%s' % indice].value = item['edad']
    hoja['D%s' % indice].value = item['telefono']
    indice += 1

Your question is very broad but I leave you this little guide, here I leave the pymysql link if you are working with MySQL. Remember to pass the variable cursorclass=pymysql.cursors.DictCursor in the connector, so that you return the dictionary

link

    
answered by 04.10.2017 в 15:37