MySQL 远程查询有效,但通过 Python 远程插入失败

MySQL remote query works, but remote insert fails via Python

我 运行宁 Ubuntu 16.04 MySQL。我已经为远程连接打开了 MySQL 服务器,我的远程 Python 脚本可以查询我的数据库,但是所有 INSERT 尝试都失败了,没有任何错误日志条目。

看起来我的远程 INSERT 也被看到了,因为当我 运行 Python INSERT 代码时,我的 AUTO_INCREMENT ID 增加而没有输入。

如有任何见解,我们将不胜感激!

简单table架构:

CREATE TABLE test (
    ID int NOT NULL AUTO_INCREMENT,
    x INT,
    PRIMARY KEY (ID)
);

这直接在服务器上工作:

INSERT INTO test (x) VALUES (10);

这是有效的 Python 查询:

try:
    connection = db.Connection(host=HOST, port=PORT, user=USER, passwd=PASSWORD, db=DB)
    cursor = connection.cursor()
    print("Connected to Server")

    cursor.execute("SELECT * FROM test")
    result = cursor.fetchall()
    for item in result:
        print(item)

except Exception as e:
    print('exception connecting to server db: ' + str(e))

finally:
    print('closing connection...')
    connection.close()

Python INSERT 无效:

try:
    connection = db.Connection(host=HOST, port=PORT, user=USER, passwd=PASSWORD, db=DB)
    cursor = connection.cursor()
    print("Connected to Server")

    cursor.execute("INSERT INTO test (x) VALUES (10);")

except Exception as e:
    print('exception connecting to server db: ' + str(e))

finally:
    print('closing connection...')
    connection.close()

谢谢

execute() 调用后添加此行:

cursor.execute("INSERT INTO test (x) VALUES (10)")
connection.commit()

对数据库进行更改时,要求您提交更改,任何更改都不会生效。