Quantcast
Channel: MySQL Forums - Connector/Python
Viewing all articles
Browse latest Browse all 384

Querying Data from MySQL with Python (no replies)

$
0
0
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

Viewing all articles
Browse latest Browse all 384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>