简体   繁体   中英

python sqlite3.OperationalError: near “-”: syntax error

I know there are other questions like this but I cannot find a working solution. I have a db file on my desktop, i get the DB column names from the DB file. I create the values list from an excel file with the same column name as the DB file, then i try to update the database file with the script below but I keep getting the error in the title. This worked on Monday... now it is throwing this error

    values = ['111-222-333-44', 'xxxxx', '8W 1 CPD FR with ESD Guard', 'STEVES Power INC', '6.01', '9.05', 'beach', 'None', '7000008']
    #values is autocreated but here is the list
    conn = sqlite3.connect(databasefile)

    cursor = conn.execute('select * from powersupplies')
    names = list(map(lambda x: x[0], cursor.description))
    col_names = ', '.join(names)
    print col_names

    col_spaces = ','.join(['?'] * len(names))
    print col_spaces
    c = conn.cursor()

    sql = 'INSERT INTO powersupplies (%s) values(%s)' % (col_names, col_spaces)
    c.execute(sql, values)

    conn.commit()
    conn.close()

cursor = conn.execute('select * from powersupplies')

This line here. If any of these values you are selecting have a "-" in them, then this might be your problem. I have run into this exact error before and getting rid of dashes has fixed it for me.

However, someone else may have a better solution for you of how to include them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM