有人能告诉我为什么这段代码似乎有效但不起作用......拜托?
Can someone tell me why this code seems to work but doesn't... please?
当我 运行 这段代码时,我没有收到任何错误,一切似乎都正常,但实际上并没有写入数据库。如果我复制由 print 函数生成的 sql 字符串并将其直接粘贴到 MariaDB 终端会话中,它会完美运行。这是生成的sql:
插入 msg_store(缺点、产品、文件)值('XXX'、'YYY'、'ZZZ');
如果我更改数据库名称或任何必需的参数,我会收到预期的错误。
我已经在其他应用程序中成功地使用了大部分代码。我 运行 在 Raspberry Pi 4 上 Python3.7
import pymysql
class C_DataBase: # Database Communication Class
def __init__(self, host_ip, db_name, user_name, password, poll_time = None):
self.ip = host_ip
self.db = db_name
self.un = user_name
self.pw = password
if poll_time is not None:
self.pt = poll_time
def connect(db):
try:
con = pymysql.connect(host=db.ip, # host
user=db.un, # username
passwd=db.pw, # password
db=db.db) # name of the data base
return con.cursor() # Open the database and return
except Exception as e:
print('DBOpenError, Failed to Connect to={}, IP={}, user={}, pw={}'.format(db.db, db.ip, db.un, db.pw))
def pub_sql(db, cons, prod, msgb):
con = connect(db)
col = "cons, prod, file"
val ="'" + cons + "','" + prod + "','" + msgb + "'"
sql = "INSERT INTO msg_store (" + col + ") VALUES (" + val + ");"
try:
rslt = con.execute(sql)
print(rslt, sql)
con.close()
except Exception as e:
print('SQLWriteError={}'.format(e))
if __name__ == '__main__':
db = C_DataBase('localhost', 'testdb', 'rpi', 'rpi') # Create and populate a database object
pub_sql(db, 'XXX', 'YYY', 'AAA') # Call the publishing function
mariaDB 终端会话
;MariaDB [testdb]> INSERT INTO msg_store (cons, prod, file) VALUES ('XXX','YYY','AAA');
Query OK, 1 row affected (33.852 sec)
MariaDB [testdb]>
MariaDB [testdb]> select * from testdb.msg_store;
+------+------+---------+
| cons | prod | file |
+------+------+---------+
| ABC | DEF | message |
| ABC | DEF | message |
| XXX | DEF | message |
| XXX | YYY | ZZZ |
| XXX | YYY | AAA |
+------+------+---------+
5 rows in set (0.001 sec)
Run status screen from Pycharm
在 con.execute()
之后你还应该做 con.commit()
然后你可以关闭连接
当我 运行 这段代码时,我没有收到任何错误,一切似乎都正常,但实际上并没有写入数据库。如果我复制由 print 函数生成的 sql 字符串并将其直接粘贴到 MariaDB 终端会话中,它会完美运行。这是生成的sql:
插入 msg_store(缺点、产品、文件)值('XXX'、'YYY'、'ZZZ');
如果我更改数据库名称或任何必需的参数,我会收到预期的错误。 我已经在其他应用程序中成功地使用了大部分代码。我 运行 在 Raspberry Pi 4 上 Python3.7
import pymysql
class C_DataBase: # Database Communication Class
def __init__(self, host_ip, db_name, user_name, password, poll_time = None):
self.ip = host_ip
self.db = db_name
self.un = user_name
self.pw = password
if poll_time is not None:
self.pt = poll_time
def connect(db):
try:
con = pymysql.connect(host=db.ip, # host
user=db.un, # username
passwd=db.pw, # password
db=db.db) # name of the data base
return con.cursor() # Open the database and return
except Exception as e:
print('DBOpenError, Failed to Connect to={}, IP={}, user={}, pw={}'.format(db.db, db.ip, db.un, db.pw))
def pub_sql(db, cons, prod, msgb):
con = connect(db)
col = "cons, prod, file"
val ="'" + cons + "','" + prod + "','" + msgb + "'"
sql = "INSERT INTO msg_store (" + col + ") VALUES (" + val + ");"
try:
rslt = con.execute(sql)
print(rslt, sql)
con.close()
except Exception as e:
print('SQLWriteError={}'.format(e))
if __name__ == '__main__':
db = C_DataBase('localhost', 'testdb', 'rpi', 'rpi') # Create and populate a database object
pub_sql(db, 'XXX', 'YYY', 'AAA') # Call the publishing function
mariaDB 终端会话
;MariaDB [testdb]> INSERT INTO msg_store (cons, prod, file) VALUES ('XXX','YYY','AAA');
Query OK, 1 row affected (33.852 sec)
MariaDB [testdb]>
MariaDB [testdb]> select * from testdb.msg_store;
+------+------+---------+
| cons | prod | file |
+------+------+---------+
| ABC | DEF | message |
| ABC | DEF | message |
| XXX | DEF | message |
| XXX | YYY | ZZZ |
| XXX | YYY | AAA |
+------+------+---------+
5 rows in set (0.001 sec)
Run status screen from Pycharm
在 con.execute()
之后你还应该做 con.commit()
然后你可以关闭连接