What does cur.fetchone()[0] do?
<p>I am using the following code for an educational purpose:</p>

<pre><code>import json
import sqlite3

conn = sqlite3.connect('rosterdb.sqlite')
cur = conn.cursor()


# Do some setup
cur.executescript('''
DROP TABLE IF EXISTS User;
DROP TABLE IF EXISTS Member;
DROP TABLE IF EXISTS Course;

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 = raw_input('Enter file name: ')

if ( len(fname) &lt; 1 ) : fname = 'roster_data.json'

# [
#   [ "Charley", "si110", 1 ],
#   [ "Mea", "si110", 0 ],

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 REPLACE INTO Member
        (user_id, course_id, role) VALUES ( ?, ?, ? )''', 
        (user_id, course_id, role) )

    conn.commit()
</code></pre>

<p>What does cur.fetchone()[0] do? 
Why the cur.fetchnone()[0] is not necessary when populating the Member table?</p>

<p>You can download the json file which is read by the program at:</p>

<p><a href="https://pr4e.dr-chuck.com/tsugi/mod/sql-intro/roster_data.php?PHPSESSID=7530ba71982b2e10c3f61bd0cb1bfcb5" rel="nofollow">https://pr4e.dr-chuck.com/tsugi/mod/sql-intro/roster_data.php?PHPSESSID=7530ba71982b2e10c3f61bd0cb1bfcb5</a></p>

<p><code>cur.fetchone()</code> retrieves one result row for the query that was executed on that cursor. A row is always a sequence of 1 or more columns, and <code>[0]</code> indexes that row to get the first element.</p>

<p>It only makes sense to retrieve results from a <code>SELECT</code> query; in <code>INSERT</code> adds data to the database and there are no results to retrieve for that operation.</p>

<p>The <code>SELECT</code> queries retrieve extra information to insert into the Member table. The <code>id</code> columns in the User and Course tables are <em>autoincrementing</em>, so each time you insert a row a new id is generated for those new rows. The <code>SELECT</code> statements retrieve these id values so they can be used in the Member table. </p>

python sql python-2.7 sqlite mooc