Hi guys,
I'm new to forum and to Python.
I need some help with the python - MySQL interaction. I'm creating a program for my conclusion project in College and it's not working. I gotta present it on Wed, 11/24, and I'm really desperate for some help.
It's suposed to work like this:
I have an RFID reader and 4 tags wich will send the reader some information. The most important info are the TAG_ID and RSSI value. The program is retrieving and displaying these values correctly.
After each time the program reads the data from the tags, it should insert the values into a table in MySQL.
After the program inserts the values into the table, I want to be able to choose to retrieve and display all values in the table or only the values for a specific tag.
In the last part, I want to be able to make a graph with the values that are in the table. I want to make the graph RSSI x iteration
I have not started on the graph part yet. I still need to read more about this and I'm actually thinking about removing this part because I won't have enough time.
I'll put what I got so far below so that someone can try to help me out. Please disregard the comments because they are in portuguese.
# -*- coding: latin1 -*-
# Declarando as bibliotecas necessárias
import serial
import os
import thread
import MySQLdb
# Rotina para interpretação dos dados lidos
global Alive
ValoresLidos = {}
lista = []
k = 1
Alive = False
# ========================================== *** ROTINA DO MENU PRINCIPAL *** ===============================================
os.system('clear')
print""
print"Menu Principal:"
print"---------------"
print""
print"1 - Mostrar dados lidos;"
print"2 - Mostrar dados ja inseridos na tabela;"
print"3 - Fazer grafico com dados da tabela;"
print"\n"
opcao=raw_input("Escolha uma opção e tecle enter: ")
# Executa o programa escolhido
if int(opcao == 1) :
ser = serial.Serial(port='COM4', baudrate=115200, timeout=2)
while True:
try:
for i in ser.read():
lista.append(ord(i))
if k == 17:
k = 0
ValoresLidos['RSSI'] = lista[4]
ValoresLidos['TAG_ID'] = lista[9]
lista = []
print ValoresLidos
#Rotina para criar conexão com o MySQL.
db = MySQLdb.connect(host="localhost",port=3306,user="root", passwd="xxxxxx",db="mydb")
#Após a conexão ser estabelecida, os dados de cada leitura são inseridos na tabela
c = db.cursor()
c.execute("""
INSERT INTO RFID (TAG_ID, RSSI)
VALUES
('TEST ID', 'TEST RSSI')
""")
#O comando rowcount faz uma contagem de quantas linhas foram inseridas na tabela.
print "Linhas inseridas: %d" % c.rowcount
k+=1
except KeyboardInterrupt:
ser.close()
break
elif int(opcao == 2) :
print""
print"Dados inseridos na tabela:"
print"---------------"
print""
print"1 - Mostrar tabela inteira;"
print"2 - Mostrar dados para uma tag especifica;"
print"\n"
opcao2=raw_input("Sua escolha: ")
#Rotina para mostrar todos os dados da tabela
if int(opcao2 == 1) :
c.execute("""SELECT * FROM RFID""")
while (1):
row = c.fetchone()
if row == None:
break
print "%s, %s, %s" % (row[0], row[1], row[2])
print "Numero de linhas retornadas: %d" % c.rowcount
#Rotina para mostrar os dados na tabela para uma tag específica
elif int(opcao2 == 2) :
print""
print"Tags disponiveis:"
print"---------------"
print""
print"1 - 25;"
print"2 - 26;"
print"3 - 29;"
print"4 - 35;"
print"\n"
opcao3=raw_input("Sua escolha: ")
#Rotina para imprimir dados da tag 25
if int(opcao3 == 1) :
c.execute("""SELECT * FROM RFID WHERE TAG_ID = '25'""")
while (1):
row = c.fetchone()
if row == None:
break
print "%s, %s, %s" % (row[0], row[1], row[2])
print "Numero de linhas retornadas: %d" % c.rowcount
#Rotina para imprimir dados da tag 26
elif int(opcao 3 == 2) :
c.execute("""SELECT * FROM RFID WHERE TAG_ID = '26'""")
while (1):
row = c.fetchone()
if row == None:
break
print "%s, %s, %s" % (row[0], row[1], row[2])
print "Numero de linhas retornadas: %d" % c.rowcount
#Rotina para imprimir dados da tag 29
elif int(opcao 3 == 3) :
c.execute("""SELECT * FROM RFID WHERE TAG_ID = '29'""")
while (1):
row = c.fetchone()
if row == None:
break
print "%s, %s, %s" % (row[0], row[1], row[2])
print "Numero de linhas retornadas: %d" % c.rowcount
#Rotina para imprimir dados da tag 35
elif int(opcao 3 == 4) :
c.execute("""SELECT * FROM RFID WHERE TAG_ID = '35'""")
while (1):
row = c.fetchone()
if row == None:
break
print "%s, %s, %s" % (row[0], row[1], row[2])
print "Numero de linhas retornadas: %d" % c.rowcount
The program is not working. After I enter an option in the menu it doesn't display the data nor adds it to the table. It just takes me to the second menu.
The first menu is the main menu. The options are:
1 - Show read data (and put them in the table)
2 - Show data already in the table
3 - Make the graph
The second menu is the sub-menu for option 2. The options are:
1 - Show the entire table
2 - Show data only for a specific tag
The third menu is where the user selects the tag ID whose data he wants to see.
Can someone please help me out?
Thanks in advance.