快速将大量数据插入数据库

#插入数据
Excel_Len = excel_raw_data.values.__len__()#获取读取的Excle数据表中数据的长度
i = 0
for yf in range(1, 11):#将数据分为10个数据块插入
    JD = int(Excel_Len / (11 - yf))
    db = MySQLdb.connect(host='127.0.0.1', user='root', passwd='mima', port=3306)
    # 使用cursor()方法获取操作游标
    cur = db.cursor()
    cur.execute('use OilData')#选择数据库
    sql = "insert into " + User_Table + " (Row_ID,ID,YLJBH,SJ,YL,WD) values "
    j = 0
    for key in excel_raw_data.values:
        if (j < i):
            j += 1
        else:
            if (i < (JD - 1)):
                sql += "(" + str(Start_Place + i + 1) + ",'" + str(int(key[0])) + "','" + str(
                    key[1]) + "'," + str(key[2]) + "," + str(key[3]) + ",'" + str(key[4]) + "'),"
                i += 1
                j += 1
            else:
                sql += "(" + str(Start_Place + i + 1) + ",'" + str(int(key[0])) + "','" + str(
                    key[1]) + "'," + str(key[2]) + "," + str(key[3]) + ",'" + str(key[4]) + "')"
                i += 1
                j += 1
                break
    cur.execute(sql)
    db.commit()
    cur.close()
    db.close()

你可能感兴趣的:(快速将大量数据插入数据库)