MySQL Python查询语法错误:在INSERT时似乎已满足?

2024-05-02 05:30:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在删除出租车服务的示例数据库,并尝试使用Python和mysql.connector接口库。PhoneCall表列包括:

  • idCall, int(11), PRIMARY KEY
  • Operator_idOperator, int(11), NOT NULL, FOREIGN KEY
  • Client_idClient, int(11), NOT NULL, FOREIGN KEY
  • datetimeValue, datetime
  • Duration, int(11)

我声明一个空列表并生成一些数据来填充表:

  • query = "INSERT INTO TaxiTest.PhoneCall (idCall, Operator_idOperator, Client_idClient, datetimeValue, Duration) VALUES (%s, %s, %s, %s, %s)"

之后,我生成数据

data = []
for i in range(1500):
    idCall = i + 1
    (Operator_idOperator, Client_idClient) = (20, 21) # Sample ints
    datetime_value = datetime.now()
    duration = 60
    t_tuple = (idCall, Operator_idOperator, Client_idClient, datetime_value, duration)
    data.append(t_tuple)

接下来,我尝试使用游标.executemany(查询,数据)语法:

cnx = mysql.connector.connect(**config)
cnx.autocommit = True
cursor = cnx.cursor()
try:
    cursor.executemany(query, data)
    log = cursor.fetchall()
    print("Result: " + log)
except mysql.connector.Error as err:
    print(err.msg)

结果是:

  • Data[0]: (1, 46, 2521, datetime.datetime(2018, 9, 17, 22, 44, 20, 159722), 55)
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TaxiTest.PhoneCall' at line 1

虽然我已经在这个数据库中填充了大约5个表,但是我没有发现这个INSERT语句中有任何错误。你知道吗

似乎唯一的解决方案是将所有1500个数据条目写入output.txt文件并使用MySQL Workbench执行,因为使用MySQL Workbench插入时没有错误,但这一点不值得赞赏。你知道吗

另外,这是我关于堆栈溢出的第一个问题。等了这么久才得到一些“硬币”评论和评级其他问题和答案。你知道吗


Tags: 数据keyclientdataconnectordatetimemysqloperator
1条回答
网友
1楼 · 发布于 2024-05-02 05:30:27

不用尝试,我会说它是datetime对象。你知道吗

您可以尝试以下代码:

from datetime import datetime
datetime_value = datetime.now()
formatted_date = datetime_value.strftime('%Y-%m-%d %H:%M:%S')

相关问题 更多 >