为什么我的MySQL语法出错?

问题描述 投票:0回答:1

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

pymysql.err.ProgrammingError:(1064,“您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以获取正确的语法以在'-dur,in-trk代码,in- crt-id,已使用代码,out-crt-id,clg-num-in-tag,dialed-num,'位于第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()
python mysql python-3.x parsing mysql-python
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.