pymysql执行execute(sql)报错1064

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import pymysql
import math

file_list = list()
base_path = 'E:\清洗后数据'
cnt = 0
index = 1


def get_name(path):
    global file_list

    if not os.path.exists(path):
        print(path)
    if os.path.exists(path):
        # print(os.path.isfile(path))
        if os.path.isdir(path):
            dir_list = os.listdir(path)
            # print(dir_list)
            for x in dir_list:
                new_path = path+'\\' + x
                # print(new_path)
                if x != '4月' and x != '5月':
                    get_name(new_path)
        else:
            file_list.append(path)


def get_write_pymysql():
    global cnt
    global index
    global file_list

    conn = pymysql.connect(host='localhost', user='root', password='admin', port=3306)
    cursor = conn.cursor()
    print('111')
    # 读取写入数据
    for x in file_list:
        print('{}开始进行读入工作:'.format(x))
        if 'xlsx' in x:
            df = pd.read_excel(x, index_col=0)
            # if 'Unnamed: 0' in df.columns:
            #     df = df.drop(['Unnamed'], axis=1, inplace=True)
            # if 'Unnamed' in df.columns:
            #     df = df.drop(['Unnamed'], axis=1, inplace=True)
            columns = df.columns.values.tolist()
            rows = df.count().count()

        elif 'csv' in x:
            df = pd.read_csv(x, encoding='ISO-8859-1', index_col=0)
            # if 'Unnamed: 0' in df.columns.values.tolist():
            #     df = df.drop(['Unnamed: 0'], axis=1, inplace=True)
            # if 'Unnamed' in df.columns.values.tolist():
            #     df = df.drop(['Unnamed'], axis=1, inplace=True)
            columns = df.columns.values.tolist()
            rows = df.count().count()
        else:
            print('{}不是合法的文件名!'.format(x))
            continue

        if cnt+rows >= 5000000:
            cnt = 0
            index += 1
        print(columns)
        print('222')
        # break
        try:
            for row in df.iterrows():
                row_value = tuple(row[1].values.tolist())
                print(222.111)
                # t = '%s'
                # sql = f"insert ignore into xhs0{index} ({columns}) values ({row_value})"
                print(222.222)
                cursor.execute(f"insert ignore into xhs0{index} ({tuple(columns)}) values ({tuple(row_value)})")
                print(222.333)
                conn.commit()
        except Exception as e:
            print('失败的原因为:{}'.format(e))
            conn.rollback()

        print('333')

        print('{}结束工作完成!'.format(x))


if __name__ == '__main__':
    get_name(base_path)
    get_write_pymysql()

错误信息如下:

失败的原因为:(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 ''word_name', 'read_count', 'account_type', 'ad_serving', 'brands_category', 'bra' at line 1")

已尝试的解决方案:

  1. 使用ignore来忽视错误,但是显然没有任何效果,因为代码部分已经加入

这个错误暂时没有解决,思考方式变一下,可以考虑将pandas中所有的数据追加成一个表,参考链接如下:pandas多表单连接

最终问题解决:中间出现了接口错误,类型错误等一大堆错误
解决代码如下:

conn = pymysql.connect(host='localhost', user='root', password='admin',database='xhs', port=3306)
for x in file_list:
    try:
        df = pd.read_excel(x)
        a = [0 for index in range(len(df))]
        b = [0 for index in range(len(df))]
        flag = True
        if '笔记类型1' not in df.columns:
            df['笔记类型1'] = a
        if '笔记id' not in df.columns:
            flag = False
            df['笔记id'] = b

        # 首先处理所有的空数据
        for column in df.columns:
            if df[column].isnull().any():
                df[column] = df[column].fillna(0)

        # 去除笔记id中的所有重复数据
        if flag:
            df[['笔记id']] = df[['笔记id']].drop_duplicates()
        df.dropna(axis=0, how='any', inplace=True)

        data_list = [tuple(i) for i in df.values]
        s_count = len(data_list) * '%s '

        """
        写入数据库
        """
        conn = pymysql.connect(host='localhost', user='root', password='admin',database='xhs', port=3306)
        cursor = conn.cursor()

        # 使用数据库
        sql_0 = 'use xhs'
        cursor.execute(sql_0)

        # mysql数据库检测,并且进行重新连接
        conn.ping(reconnect=True)

        print('测试1')

        cols = ",".join([str(i) for i in df.columns.tolist()])

        # print('测试2')
        # sql_1 = f'insert into test({cols_1}) values ({s_count[:-1]})'
        # print('测试3')
        # cursor.execute(sql_1, data_list)
        # print('测试4')

        for i, row in df.iterrows():
            sql = "insert into `test` (" + cols+")values(" + "%s,"*(len(row) - 1) + "%s)"
            cursor.execute(sql, tuple(row))
            conn.commit()

        print('测试5')
        conn.close()
    except Exception as e:
        conn.rollback()
        print(f'{x}有问题,请注意!!!!!!!!!!!!!!!')
        print(e)
    break
posted @ 2022-05-06 09:19  Lazy_tiger  阅读(597)  评论(1编辑  收藏  举报