0

I have a csv file and am trying to create a sqlite database with that data.

The csv is basically like this:

genre,singer,singer_email,enrol_datetime,Last booking made on
Blues | Craig Phillip,craig@gmailcom,2018-05-20,2021-03-03

I tried following this and here's what I came up with. What am I doing wrong?

import csv, sqlite3

con = sqlite3.connect("sqlite:///data.db") 
cur = con.cursor()
cur.execute("CREATE TABLE t (genre, singer, singer_email, enrol_datetime, last_booking);") 

with open('data.csv','r') as fin: 
    dr = csv.DictReader(fin)
    to_db = [(i['genre'], i['singer'], i['singer_email'],i['enrol_datetime'],i['last_booking']) for i in dr]

cur.executemany("INSERT INTO t (genre, singer, singer_email,  enrol_datetime, last_booking) VALUES (?, ?, ?, ?, ?);", to_db)
con.commit()
con.close()

This is the error

Traceback (most recent call last):
  File "create_sqlite_database.py", line 3, in <module>
    con = sqlite3.connect("sqlite:///data.db")
sqlite3.OperationalError: unable to open database file
3
  • 1
    just con = sqlite3.connect("data.db") will be fine. pls try. Commented Apr 28, 2021 at 14:11
  • it worked. Wanna add it as the answer? Commented Apr 28, 2021 at 14:50
  • glad it helped. Added, thanks Commented Apr 28, 2021 at 14:55

1 Answer 1

1

As per sqlite3 API reference, just put database name and error will go away.

con = sqlite3.connect("data.db")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.