当我插入数据时,语法错误接近

时间:2019-09-08 12:20:25

标签: excel python-3.x sqlite

当我在sqlite数据中插入excel数据时,它将显示一些语法错误,我不知道这是哪里错误?

import sqlite3
from openpyxl import *
from sqlite3 import Error


def create_db(db_file):
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)


def execute_sql(conn, sql):
    try:
        c = conn.cursor()
        c.execute(sql)
    except Error as e:
        print(e)


if __name__ == '__main__':
    create_table_inspections = "CREATE TABLE if not exists inspections(" \
                               "activity_date DATE NOT NULL," \
                               "employee_id VARCHAR(15) NOT NULL," \
                               "facility_address VARCHAR(100) NOT NULL," \
                               "facility_city VARCHAR(80) NOT NULL," \
                               "facility_id VARCHAR(15) NOT NULL," \
                               "facility_name VARCHAR(100) NOT NULL," \
                               "facility_state VARCHAR(10) NOT NULL," \
                               "facility_zip VARCHAR(15) NOT NULL," \
                               "grade VARCHAR(1) NOT NULL," \
                               "owner_id VARCHAR(15) NOT NULL," \
                               "owner_name VARCHAR(80) NOT NULL," \
                               "pe_description VARCHAR(80) NOT NULL," \
                               "program_element_pe VARCHAR(4) NOT NULL," \
                               "program_name VARCHAR(80) NOT NULL," \
                               "program_status VARCHAR(10) NOT NULL," \
                               "record_id VARCHAR(15) NOT NULL," \
                               "score VARCHAR(10) NOT NULL," \
                               "serial_number VARCHAR(20) NOT NULL," \
                               "service_code VARCHAR(15) NOT NULL," \
                               "service_description VARCHAR(80) NOT NULL);"


    conn = create_db("data2.db")

    if conn is not None:

        execute_sql(conn, create_table_inspections)



        print("loading inspections")
        data_inspections = load_workbook("inspections.xlsx")
        data_inspections_ws = data_inspections['inspections']

        print("done")
        print("read inspections")
        for i in data_inspections_ws:
            sql = """INSERT INTO inspections(
            activity_date,
            employee_id,
            facility_address,
            facility_city,
            facility_id,
            facility_name,
            facility_state,
            facility_zip,
            grade,
            owner_id,
            owner_name,
            pe_description,
            program_element_pe,
            program_name,
            program_status,
            record_id,
            score,
            serial_number,
            service_code,
            service_description) 
            VALUES
            ("{vactivity_date}",
            "{vemployee_id}",
            "{vfacility_address}",
            "{vfacility_city}",
            "{vfacility_id}",
            "{vfacility_name}",
            "{vfacility_state}",
            "{vfacility_zip}",
            "{vgrade}",
            "{vowner_id}",
            "{vowner_name}",
            "{vpe_description}",
            "{vprogram_element_pe}",
            "{vprogram_name}",
            "{vprogram_status}",
            "{vrecord_id}",
            "{vscore}",
            "{vserial_number}",
            "{vservice_code}",
            "{vservice_description}")"""

            sql = sql.format(vactivity_date=i[0].value,
                             vemployee_id=i[1].value,
                             vfacility_address=i[2].value,
                             vfacility_city=i[3].value,
                             vfacility_id=i[4].value,
                             vfacility_name=i[5].value,
                             vfacility_state=i[6].value,
                             vfacility_zip=i[7].value,
                             vgrade=i[8].value,
                             vowner_id=i[9].value,
                             vowner_name=i[10].value,
                             vpe_description=i[11].value,
                             vprogram_element_pe=i[12].value,
                             vprogram_name=i[13].value,
                             vprogram_status=i[14].value,
                             vrecord_id=i[15].value,
                             vscore=i[16].value,
                             vserial_number=i[17].value,
                             vservice_code=i[18].value,
                             vservice_description=i[19].value)
            execute_sql(conn, sql)
            pass
        print("Done")
    conn.commit()
    conn.close()

0 个答案:

没有答案