Python Mysql 连接未提交

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

我正在学习从 Python 连接到 MySQL,并设置了一个 XAMPP 服务器来在本地主机上托管我的数据库。我设法连接并创建表,但无论我做什么,我都无法向创建的表提交插入。

这是我的代码:

kapcsolat = pymysql.connect(host='localhost',
                            user='pythonhoz_user',
                            password='*I had the right password here as creating tables worked*',
                            database='pythonhoz_db',
                            cursorclass=pymysql.cursors.DictCursor)
kurzor = kapcsolat.cursor()
parancs = """insert into international_student_test_scores (country, first_name, last_name, test_score)
                    values
                    ('United States', 'Marshall', 'Bernadot', 54);"""

kurzor.execute(parancs)
kapcsolat.commit
parancs = """select *
             from international_student_test_scores;"""
kurzor.execute(parancs)
output_all = kurzor.fetchall()
for row_all in output_all:
  print(row_all)
kapcsolat.close

如下图所示,select 确实运行正常并输出一行数据:

Select works properly from Python

但是,尽管我确实发出了提交和关闭,但这些行不在数据库中:

Rows not in the database

我已经尝试重新启动一切,但没有帮助:((( .

实际上,如果我将“kapcsolat.commit”行之后的“kapcsolat.close”行向上移动,选择仍然有效,这很奇怪......

如果有人能提供帮助,我会很高兴...

python mysql commit
1个回答
0
投票

您忘记在提交方法后面包含括号,这就是提交未执行的原因。将 kapcsolat.commit 更新为 kapcsolat.commit(),将 kapcsolat.close 更新为 kapcsolat.close()

© www.soinside.com 2019 - 2024. All rights reserved.