0

I am trying to continuously log whatever a specific value (stock) in my database is at all times, the value is changed 20 times per second (confirmed by looking at results manually)

Yet for some reason, this code here, once it is started, always prints the same output and doesnt change. What am I doing wrong?

import mysql.connector
import keyboard
from time import sleep

connection = mysql.connector.connect(host='localhost',
                                        database='minecraft',
                                        user='root',
                                        password='password')

sql_select_Query = "select stock from xdata where id = \"1\""
file = open("mysqldump.txt", "a")

print("Connection started")

while 1:
    cursor = connection.cursor()
    cursor.execute(sql_select_Query)
    records = cursor.fetchone()[0]
    print(records)
    file.write(str(records) + "\n")
    if(keyboard.is_pressed("x")):
        break
    sleep(0.1)

print("Connection stopped")

file.close()
connection.close()
  • Welcome to StackOverflow! Questions: a) you're doing `INSERT INTO xdata` or `UPDATE xdata SET stock ..` ? b) are you sure that you connect to the same instance? `localhost` will be different in e.g. host and inside container – Alex Yu Aug 22 '21 at 22:56
  • 1
    You have a MySQL database with a row that changes 20 times a second??? Why? Why wouldn't you just have a TCP server that keeps the information in memory and delivers it on demand? Why beat up your disks? – Tim Roberts Aug 23 '21 at 01:55

1 Answers1

0

You have specified the condition of id being 1

where _id=1