0

我的程序在执行插入数据库的函数后向我发送以下错误消息:

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)
            …
4

1 回答 1

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()
于 2013-10-03T01:06:19.453 回答