简体   繁体   中英

sqlite3.OperationalError: near "?": syntax error while trying to import data from csv into sqlite

I am trying to transfer data from postgresql db table to sqlite table using python. Here is my code:

import sqlite3
import csv

connect = sqlite3.connect("server.db")
cursor = connect.cursor()

sql = """CREATE TABLE users (
        name TEXT,
        id INT,
        xp INT
        )"""

cursor.execute(sql)
connect.commit()

with open('users.csv', 'r', encoding="utf-8") as f:
    no_records = 0
    for row in f:
        cursor.execute(f"INSERT INTO users (?,?,?)", row.split(','))
        connect.commit()
        no_records += 1

connect.close()

But when running this script I get sqlite3.OperationalError: near "?": Syntax error:

Traceback (most recent call last):
  File "C:\Users\belog\sort_hat\cr.py", line 19, in <module>
    cursor.execute(f"INSERT INTO users (?,?,?)", row.split(','))
sqlite3.OperationalError: near "?": syntax error

How to fix it and is it possible to import data is easier without using python?

Your syntax is missing VALUES :

INSERT INTO users VALUES(?,?,?)

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