我已在 Windows 桌面上安装了 MySQL。 安装成功。 我从github下载了
employees
数据并将其添加到我的MySql服务器中。 我通过MySQL 8.4命令行客户端确认了这一点
我正在阅读 MySQL 连接器/Python 开发人员指南的文档。 我必须将数据插入员工数据库。 我收到此错误:
DatabaseError: 1364 (HY000): Field 'emp_no' doesn't have a default value
这个问题的答案是关于PHP的,而不是Python:mysql错误1364字段没有默认值
这个问题是关于
Datetime
的默认值:如何为 MySQL 日期时间列设置默认值?
除了
**config
之外,此代码是从 dev.mysql.com 超链接剪切粘贴的。 除了 'password': '****'
之外,这是我使用的代码:
from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector
config = {
'user': 'root',
'password': '****',
'host': '127.0.0.1',
'database': 'employees'
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
tomorrow = datetime.now().date() + timedelta(days=1)
add_employee = ("INSERT INTO employees "
"(first_name, last_name, hire_date, gender, birth_date) "
"VALUES (%s, %s, %s, %s, %s)")
add_salary = ("INSERT INTO salaries "
"(emp_no, salary, from_date, to_date) "
"VALUES (%(emp_no)s, %(salary)s, %(from_date)s, %(to_date)s)")
data_employee = ('Geert', 'Vanderkelen', tomorrow, 'M', date(1977, 6, 14))
# # Insert new employee
# cursor.execute(add_employee, data_employee)
# emp_no = cursor.lastrowid
# # Insert salary information
# data_salary = {
# 'emp_no': emp_no,
# 'salary': 50000,
# 'from_date': tomorrow,
# 'to_date': date(9999, 1, 1),
# }
# cursor.execute(add_salary, data_salary)
# # Make sure data is committed to the database
# cnx.commit()
cursor.close()
cnx.close()
现在,我知道它是连接的,因为如果我注释掉两个
Insert
块,它就可以正常运行。 但是,这是按照此问题中发布的方式运行时的错误消息。
Traceback (most recent call last):
File "C:\Users\User\Documents\Python\Tutorials\.venv\Lib\site-packages\mysql\connector\connection_cext.py", line 697, in cmd_query
self._cmysql.query(
_mysql_connector.MySQLInterfaceError: Field 'emp_no' doesn't have a default value
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\User\Documents\Python\Tutorials\Add_to_Employees.py", line 27, in <module>
cursor.execute(add_employee, data_employee)
File "C:\Users\User\Documents\Python\Tutorials\.venv\Lib\site-packages\mysql\connector\cursor_cext.py", line 372, in execute
result = self._cnx.cmd_query(
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\Documents\Python\Tutorials\.venv\Lib\site-packages\mysql\connector\opentelemetry\context_propagation.py", line 102, in wrapper
return method(cnx, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\Documents\Python\Tutorials\.venv\Lib\site-packages\mysql\connector\connection_cext.py", line 705, in cmd_query
raise get_mysql_exception(
mysql.connector.errors.DatabaseError: 1364 (HY000): Field 'emp_no' doesn't have a default value
免责声明:我知道不要将密码放入文件中。 我试图一次解决一个问题。
我认为问题始于 mysql 数据库员工表。该表没有 emp_no 的默认值或自动增量。那么你必须在插入数据时手动添加 emp_no ,如下所示。
add_employee = ("INSERT INTO employees "
"(emp_no, first_name, last_name, hire_date, gender, birth_date) "
"VALUES (%s, %s, %s, %s, %s, %s)")
或者,您可以更改表以将 emp_no 作为自动增量字段:
ALTER TABLE employees MODIFY emp_no INT NOT NULL AUTO_INCREMENT;
或者,删除表并重新创建它,并将 emp_no 设置为自动增量:
DROP TABLE employees;
CREATE TABLE employees (
emp_no INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE,
gender ENUM('M', 'F'),
birth_date DATE,
PRIMARY KEY (emp_no)
);