python - 'type' object is not subscriptable error

Viewed 203

I'm trying to put the data file of schools.data which is just a file listing many universities. It says 'type' object is not subscriptable in terminal. Here is the code

import urllib
import sqlite3
import json
import time
import ssl

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

cur.execute('''CREATE TABLE IF NOT EXISTS Universitylocations (address TEXT, geodata TEXT)''')
fh = open("schools.data")
count = 0
for line in fh:
    if count > 200:
        print ('Retrieved 200 locations, restart to retrieve more')
        break
    address = line.strip()
    print('')
    cur.execute("SELECT geodata FROM Universitylocations WHERE address= ?",(bytes[address]))
    print("Resolving", data)
    url = fh + urllib.urlencode({"sensor":"false", "address": address})
    print("Retrieving", url)
    uh = urllib.urlopen(url, context=scontext)
    data = uh.read()
    print('Retrieved',len(data),'characters',data[:20].replace('\n',''))
    count = count + 1
    try:
        js = json.loads(str(data))
    except:
        continue

    if 'status' not in js or (js['status'] != 'OK' and js['status'] != 'ZERO_RESULTS') :
        print('==== Failed to Retrieve ====')
        print (data)
        continue

    cur.execute('''INSERT INTO Universitylocations (address, geodata) VALUES (?, ?)''', (bytes[address],bytes[data]))
    conn.commit()
    if count % 10 == 0 :
        print('Pausing for a bit...')
        time.sleep(5)
print("Run process.py to read the data on a database")

Can anyone help? I've been having this issue for a while.

1 Answers

This line is culprit:

cur.execute("SELECT geodata FROM Universitylocations WHERE address= ?",(bytes[address]))

change bytes[address] with (address,). Means:

cur.execute('''SELECT geodata FROM Universitylocations WHERE address= ?''',(address,))

Check what your data type is the database.

Related