gpt4 book ai didi

python - 为什么我收到 MySQL 语法错误?

转载 作者:行者123 更新时间:2023-11-29 06:27:59 24 4
gpt4 key购买 nike

请帮帮我。我正在编写一个 cdr avaya 日志解析器,将数据写入 mysql 数据库。解析器本身工作正常,但它不写入数据库。该程序本身是用python 3.7编写的,分为2个文件。 cdr.py 解析器本身和 db.py 数据库记录文件。它会产生这样的错误:

pymysql.err.ProgrammingError: (1064, "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 '-dur, in-trk-code, in-crt-id, code-used, out-crt-id, clg-num-in-tag, dialed-num,' at line 1")

所有表字段都是 INT 类型,并且可以写入 NULL。

cdr.py

import socket
import db

# Задаем адрес сервера
SERVER_ADDRESS = ('', 5100)

# Настраиваем сокет
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(SERVER_ADDRESS)
server_socket.listen(5)
print('server is running, please, press ctrl+c to stop')

# Слушаем запросы
while True:
connection, address = server_socket.accept()

data = connection.recv(1024)
if not(b'\x00\x00\x00' in data):
str = data.decode("utf-8")
item=(str[0:6],str[7:11],str[12:17],str[18:22],str[23:26],str[27:30],str[31:34],str[35:50],str[51:74],str[75:76],str[77:90],str[91:92])
print(item)
db.write_db(item)

connection.close()

db.py

import pymysql.cursors

def write_db(item, *agrs):
connection = pymysql.connect(host='localhost',
user='acdr',
password='it8ejokd',
db='avaya_cdr',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
DBTBL = "cdr102019"
DBFLD = "Date, Time, Sec-dur, in-trk-code, in-crt-id, code-used, out-crt-id, clg-num-in-tag, dialed-num, cond-code, vdn, frl"


try:
with connection.cursor() as cursor:
sql = "INSERT INTO "+DBTBL+" ("+DBFLD+") VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
cursor.execute(sql, (item))
connection.commit()
finally:
connection.close()

最佳答案

您需要在包含 - 的列名称周围加上反引号。

DBFLD = "Date, Time, `Sec-dur`, `in-trk-code`, `in-crt-id`, `code-used`, `out-crt-id`, `clg-num-in-tag`, `dialed-num`, `cond-code`, vdn, frl"

更多详情,请参阅 When to use single quotes, double quotes, and backticks in MySQL

关于python - 为什么我收到 MySQL 语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58383460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com