Home > Back-end >  sqlite3.OperationalError: near "?": syntax error while trying to import data from csv into
sqlite3.OperationalError: near "?": syntax error while trying to import data from csv into

Time:01-05

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?

CodePudding user response:

Your syntax is missing VALUES:

INSERT INTO users VALUES(?,?,?)
  •  Tags:  
  • Related