python 数据库请求未更新
python database request not updating
我在 python 脚本中从数据库中提取数据,该脚本在启动时有效,但在我更改数据库中的值时不会更新。
脚本最初连接到数据库然后选择id等于1的所有内容(这有一些我打算使用的列)
然后用 garage 列中的内容更新全局变量 garageHeating,假设它在启动时等于 0。
while 循环打印出这个变量,该变量在开始时将为 0
如果我将数据库中的列车库更改为 10,python 脚本将保持为 0。它不会更新我认为应该执行的全局变量
下面是我的剧本
import mysql.connector as mariadb
mariadb_connection = mariadb.connect(
host="localhost",
user="garage",
passwd="*******",
database="mydb"
)
cursor = mariadb_connection.cursor()
garageHeating = 0
def readDbHeating():
global garageHeating
result = []
try:
cursor.execute ("SELECT * FROM heating WHERE id = '1'")
for reading in cursor.fetchall():
result.append (reading)
garageHeating = result[0][8]
except () as e:
print (e)
while 1:
readDbHeating()
print garageHeating
我已经编辑了下面显示的上面的代码,以在函数外部显示全局变量,并在 try 命令中打印出结果。在第一次从数据库中提取数据后,我仍然遇到同样的问题,脚本继续显示第一组数据,但如果我更改数据库记录,则不会更新到新数据。
import mysql.connector as mariadb
mariadb_connection = mariadb.connect(
host="localhost",
user="garage",
passwd="14Odiham",
database="mydb"
)
cursor = mariadb_connection.cursor()
global garageHeating
def readDbHeating():
result = []
try:
cursor.execute ("SELECT * FROM heating WHERE id = '1'")
for reading in cursor.fetchall():
result.append (reading)
garageHeating = result[0][8]
print garageHeating
print result
except () as e:
print (e)
while 1:
readDbHeating()
#print garageHeating
我好像可以用了。我必须打开数据库连接,然后关闭函数内的连接,如下所示。这与我过去使用 mysql 所做的不同,但目前至少可以使用
import mysql.connector as mariadb
import time
garageHeating = 0
def readDbHeating():
mariadb_connection = mariadb.connect(
host="localhost",
user="garage",
passwd="14Odiham",
database="mydb"
)
cursor = mariadb_connection.cursor()
result = []
try:
cursor.execute ("SELECT * FROM heating WHERE id = '1'")
for reading in cursor.fetchall():
result.append (reading)
except () as e:
print (e)
finally:
#closing database connection.
if(mariadb_connection.is_connected()):
mariadb_connection.close()
print("connection is closed")
return (result)
while 1:
test = readDbHeating()
print test
time.sleep(1)
您必须在执行查询之前调用 MySQLdb.connections.Connection.commit
或在连接对象上设置 autocommit
。
查询前提交
def readDbHeating():
global garageHeating
result = []
try:
# commit
mariadb_connection.commit()
cursor.execute ("SELECT * FROM heating WHERE id = '1'")
for reading in cursor.fetchall():
result.append (reading)
garageHeating = result[0][8]
except () as e:
print (e)
创建连接时自动提交
mariadb_connection = mariadb.connect(
host="localhost",
user="garage",
passwd="14Odiham",
database="mydb",
# Auto commit
autocommit=True
)
连接创建后自动提交
mariadb_connection.autocommit(True)
我在 python 脚本中从数据库中提取数据,该脚本在启动时有效,但在我更改数据库中的值时不会更新。
脚本最初连接到数据库然后选择id等于1的所有内容(这有一些我打算使用的列)
然后用 garage 列中的内容更新全局变量 garageHeating,假设它在启动时等于 0。
while 循环打印出这个变量,该变量在开始时将为 0
如果我将数据库中的列车库更改为 10,python 脚本将保持为 0。它不会更新我认为应该执行的全局变量
下面是我的剧本
import mysql.connector as mariadb
mariadb_connection = mariadb.connect(
host="localhost",
user="garage",
passwd="*******",
database="mydb"
)
cursor = mariadb_connection.cursor()
garageHeating = 0
def readDbHeating():
global garageHeating
result = []
try:
cursor.execute ("SELECT * FROM heating WHERE id = '1'")
for reading in cursor.fetchall():
result.append (reading)
garageHeating = result[0][8]
except () as e:
print (e)
while 1:
readDbHeating()
print garageHeating
我已经编辑了下面显示的上面的代码,以在函数外部显示全局变量,并在 try 命令中打印出结果。在第一次从数据库中提取数据后,我仍然遇到同样的问题,脚本继续显示第一组数据,但如果我更改数据库记录,则不会更新到新数据。
import mysql.connector as mariadb
mariadb_connection = mariadb.connect(
host="localhost",
user="garage",
passwd="14Odiham",
database="mydb"
)
cursor = mariadb_connection.cursor()
global garageHeating
def readDbHeating():
result = []
try:
cursor.execute ("SELECT * FROM heating WHERE id = '1'")
for reading in cursor.fetchall():
result.append (reading)
garageHeating = result[0][8]
print garageHeating
print result
except () as e:
print (e)
while 1:
readDbHeating()
#print garageHeating
我好像可以用了。我必须打开数据库连接,然后关闭函数内的连接,如下所示。这与我过去使用 mysql 所做的不同,但目前至少可以使用
import mysql.connector as mariadb
import time
garageHeating = 0
def readDbHeating():
mariadb_connection = mariadb.connect(
host="localhost",
user="garage",
passwd="14Odiham",
database="mydb"
)
cursor = mariadb_connection.cursor()
result = []
try:
cursor.execute ("SELECT * FROM heating WHERE id = '1'")
for reading in cursor.fetchall():
result.append (reading)
except () as e:
print (e)
finally:
#closing database connection.
if(mariadb_connection.is_connected()):
mariadb_connection.close()
print("connection is closed")
return (result)
while 1:
test = readDbHeating()
print test
time.sleep(1)
您必须在执行查询之前调用 MySQLdb.connections.Connection.commit
或在连接对象上设置 autocommit
。
查询前提交
def readDbHeating():
global garageHeating
result = []
try:
# commit
mariadb_connection.commit()
cursor.execute ("SELECT * FROM heating WHERE id = '1'")
for reading in cursor.fetchall():
result.append (reading)
garageHeating = result[0][8]
except () as e:
print (e)
创建连接时自动提交
mariadb_connection = mariadb.connect(
host="localhost",
user="garage",
passwd="14Odiham",
database="mydb",
# Auto commit
autocommit=True
)
连接创建后自动提交
mariadb_connection.autocommit(True)