数据未插入mySQL表(MariaDB)

2024-04-20 03:32:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试实现一个python脚本,它将在我的SQL数据库(MariaDB)的表中插入两个虚拟值。我在我的覆盆子皮上使用拉伸操作系统。在

我首先在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视为我创建的表。在

^{pr2}$

使用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)

如果有人能就我在剧本中可能犯的错误给我一些建议,我会很感激的?在


Tags: 脚本数据库secconnectionnullcursoryescol2