Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your syntax is missing VALUES:

INSERT INTO users VALUES(?,?,?)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading