数据未插入mySQL表(MariaDB)

问题描述 投票:0回答:1

我目前正在尝试实现一个python脚本,它会将几个虚拟值插入到我的SQL数据库(MariaDB)的表中。我在我的Raspberry Pi上使用Stretch OS。

我首先在MariaDB的现有数据库中成功创建了一个表。

MariaDB [testdb]> CREATE table testdbtable (col1 char(1), col2 char(1), col3 char(1));
Query OK, 0 rows affected (0.12 sec)

使用show tables;我可以看到testdbtable作为我创建的表。

+------------------+
| Tables_in_testdb |
+------------------+
| testdbtable      |
+------------------+
1 row in set (0.00 sec)

使用show columns from testdbtable;我得到:

+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1  | char(1) | YES  |     | NULL    |       |
| col2  | char(1) | YES  |     | NULL    |       |
| col3  | char(1) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

然后我创建了一个python脚本:

#!/usr/bin/python
import mysql.connector as mariadb 

mariadb_connection = mariadb.connect(user='root', password='', database='testdb')

cursor = mariadb_connection.cursor()

cursor.execute("INSERT INTO testdbtable(col1, col2, col3) VALUES ('a', 'b', 'c')");

mariadb_connection.close()

此脚本在终端上运行,没有任何错误。但是,当我在终端上运行MariaDB时,我没有看到从我的脚本插入到该表中的虚拟值(即a,b,c):

MariaDB [testdb]> select * from testdbtable;
Empty set (0.00 sec)

我很感激,如果有人能在我的剧本中给我任何关于我可能做错的建议吗?

python mysql database raspberry-pi mariadb
1个回答
2
投票

我想你需要提交更改。看到这篇文章

https://mariadb.com/resources/blog/how-connect-python-programs-mariadb

cursor.execute("INSERT INTO testdbtable(col1, col2, col3) VALUES ('a', 'b', 'c')");

mariadb_connection.commit()
© www.soinside.com 2019 - 2024. All rights reserved.