我的程序在执行要插入数据库的函数后发送以下错误消息:
Traceback:
`Traceback (most recent call last):
File "C:\Python33\Archive\MySQL-teste12.py", line 275, in <module>
inserir(cursor, cx2)
File "C:\Python33\Archive\MySQL-teste12.py", line 193, in inserir
cursor.execute(add_produto)
File "C:\Python33\lib\site-packages\mysql\connector\cursor.py", line 415, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 593, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 515, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'd' in 'field list'`
正在尝试插入下表:
produto = """
CREATE TABLE produto (
prod_nu int(11) NOT NULL AUTO_INCREMENT,
nome varchar(30) NOT NULL,
data_lcm date NOT NULL,
preco int(11) NOT NULL,
PRIMARY KEY (prod_nu)
) ENGINE=InnoDB
"""
以下是发生错误的过程。我相信它不接受任何格式的约会。我尝试了很多变化,但没有一个变化:
def inserir (cursor, db):
# excess code removed
vb = input('Digite a data de Lançamento do Produto (Ano/mês/dia).')
now = time.strftime('vb')
…
add_produto = ("""INSERT INTO produto(nome,
data_lcm, preco)
VALUES (%s, %s, %s)""" % (va,vb,vc))
cursor.execute(add_produto)
…
答案 0 :(得分:1)
有两个问题,第一个问题是你永远不会创建一个date
对象。根据你的指示(Ano /mês/ dia),这就像是
input_date = datetime.strptime(input_string, '%Y/%m/%d')
第二个问题是
now = time.strftime('vb')
将now
设置为'vb'
,因为它没有strftime指令。如果您正在寻找标准时间格式,date.isoformat()很难出错
now = input_date.isoformat()