MySQL Forums
Forum List  »  Connector/Python

Querying Data from MySQL with Python
Posted by: Tiago Silva
Date: January 17, 2017 04:30AM

Sorry Im new at python, and I couldnt find help elsewhere.

Here's my situation: I got a table on a database, as following:

image_name state type
57260-tracker-_tracker_face awake 0
57261-tracker-_tracker_face drowsiness 1
57268-tracker-_tracker_face noface 2
57289-tracker-_tracker_face distracted 3
57290-tracker-_tracker_face awake 1
57291-tracker-_tracker_face drowsiness 2
57293-tracker-_tracker_face noface 3

And I got a folder full of .txt files with the same name as the images, example:

test_img/
--57260-tracker-_tracker_face.txt
--57261-tracker-_tracker_face.txt
--57268-tracker-_tracker_face.txt
--etc

Every .txt file contains information like this,

face=1 lefteye=closed righteye=closed status=drowsiness

So my goal is, get the name from the table, and read the .txt file with the same name, then update the status on the table with which is on the .txt file.

Got it finding and opening the respective txt file, now how do I read only the status? So I can send it to the table

import mysql.connector
from mysql.connector import errorcode
import os

cnx = mysql.connector.connect(user='root', database='healthyroad')
cursor = cnx.cursor()


fileDir = os.path.dirname(os.path.realpath(__file__))
textDir = os.path.join(fileDir, "test_img")


query = ("SELECT nome_imagem, estado, type FROM alertas ")

cursor.execute(query)

for (nome_imagem, estado, type) in cursor:
print nome_imagem
my_file_name = nome_imagem+'.txt'
my_file = open("test_img/"+my_file_name, 'r')
content = my_file.readlines()
print content

Options: ReplyQuote


Subject
Written By
Posted
Querying Data from MySQL with Python
January 17, 2017 04:30AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.