-1

I am trying to connecting and inserting a data from json to sqlite. But it sucks me

I have tried to move and avoiding white spaces and copmleting with the databases

conn=sqlite3.connect('rosterdb.sqlite')
cur=conn.cursor()
cur.execute(
'''
DROP TABLE IF EXISTS USER;
DROP TABLE IF EXISTS Course;
DROP TABLE IF EXISTS Member;

CREATE TABLE User (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name   TEXT UNIQUE
);

CREATE TABLE Course (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    title  TEXT UNIQUE
);

CREATE TABLE Member (
    user_id     INTEGER,
    course_id   INTEGER,
    role        INTEGER,
    PRIMARY KEY (user_id, course_id)
)
'''
)
fname=input('Enter file name')
if len(fname)<1:
    fname=roster_data.json
str_data=open(fname).read()
json_data=json.loads(str_data)

for entry in json_data:
    name=entry[0]
    title=entry[1]
    role=entry[2]
    print(name,title,role)

    cur.execute('''INSERT OR IGNORE INTO User (name)
        VALUES ( ? )''', ( name, ) )
    cur.execute('SELECT id FROM User WHERE name = ? ', (name, ))
    user_id = cur.fetchone()[0]

    cur.execute('''INSERT OR IGNORE INTO Course (title)
        VALUES ( ? )''', ( title, ) )
    cur.execute('SELECT id FROM Course WHERE title = ? ', (title, ))
    course_id = cur.fetchone()[0]

    cur.execute('''INSERT OR IGNORE INTO Member(user_id,course_id,role) 
        VALUES(?,?,?)''',(user_id,course_id,role))

    conn.commit()

File "roster.py", line 51 cur.execute('''INSERT OR IGNORE INTO Member(user_id,course_id,role) ^ IndentationError: unindent does not match any outer indentation level

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • You're mixing tabs and spaces in your indentation. Use one or the other consistently. – khelwood Jun 29 '19 at 05:49
  • Possible duplicate of [What to do with "Unexpected indent" in python?](https://stackoverflow.com/questions/1016814/what-to-do-with-unexpected-indent-in-python) – khelwood Jun 29 '19 at 05:50
  • Possible duplicate of [IndentationError: unindent does not match any outer indentation level](https://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level) – khelwood Jun 29 '19 at 06:04
  • This is weird.... If you start an indented block using spaces and then mix a line that uses tabs you get a `TabError: inconsistent use of tabs and spaces in indentation` however if you start with a line indented with tabs and mix a line indented with spaces it raises that error instead of the correct `TabError`... maybe this is a bug in the interpreter? It should *always* raise `TabError` in this situation. I just tried increasing the space-indent of the `INSERT OR IGNORE` line and then it raises `TabError`. So `TabError` is raised only when indentation is "correct" but mixed? – Bakuriu Jun 29 '19 at 07:08

1 Answers1

0

You are probably mixing tabs and spaces

copy this to your editor:

cur.execute('''INSERT OR IGNORE INTO Member(user_id,course_id,role) 
VALUES(?,?,?)''',(user_id,course_id,role))

And manually press space bar 4 times before cur.execute And manually press space bar 8 times before VALUES

cur.execute('''INSERT OR IGNORE INTO Member(user_id,course_id,role) 
    VALUES(?,?,?)''',(user_id,course_id,role))

Please don't use tabs in combination with spaces

Ajinkya
  • 1,797
  • 3
  • 24
  • 54