0

I'm writing this code in python3 to change the 'job' column by assigning codes to job titles and then updating those codes in the table instead of the title.

But I'm getting this error when I try to run it.

It's not giving an error if I remove the loop. It works fine for the first row, but when I try to do it in a loop for all the rows, it gives the error.

Can someone help where I'm going wrong?

import sqlite3

#created a connection to the "try2" DB and opened a cursor to that connection
conn = sqlite3.connect('try2.sqlite3')
cur = conn.cursor()

#ran SQL query : SELECT job FROM info GROUP BY job
#this returned 12 rows with the type of jobs
#created a dictionary for all the types of jobs
JOB_DICT = {
    "admin." : 1,
    "blue-collar" : 2,
    "entrepreneur": 3,
    "housemaid": 4,
    "management": 5,
    "retired" : 6,
    "self-employed":7,
    "services": 8,
    "student":9,
    "technician":10,
    "unemployed": 11,
    "unknown": '-1'
}

#function to change the job title to job code 
def CHANGE_JOB(job):
    if job in JOB_DICT:
        job_code = JOB_DICT.get(job)
    return job_code

#counted the no. of rows
cur.execute('SELECT count(*) FROM info')
length = cur.fetchone()[0]

#ran the query to get job title from info table
#saved the output of the query to variable job
#called the function and saved it's output in variable new_job
for i in range(length):
    cur.execute('SELECT job FROM info')
    job = cur.fetchone()[0]
    new_job = CHANGE_JOB(job)
    cur.execute('UPDATE OR IGNORE info SET job=? WHERE job=?',(new_job,job))

conn.commit()
conn.close()
Pingolin
  • 3,161
  • 6
  • 25
  • 40
Soumya Pandey
  • 321
  • 3
  • 19
  • Please provide the full error message, with the stacktrace. – AlexisBRENON Nov 26 '19 at 08:00
  • You should print your `job` variable in the `CHANGE_JOB` function. I suspect that you get a value that is not in `JOB_DICT` so `job_code` is not set, and you try to return an "undefined" variable. – AlexisBRENON Nov 26 '19 at 08:04
  • 1
    `job_code` is defined inside an `if` in a function only if it's True. When the condition is False, it doesn't exist. Add `job_code = None` before the `if`. – Aryerez Nov 26 '19 at 08:04
  • this is the error I'm getting: Traceback (most recent call last): File "C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.7\assignments\cleaning_data.py", line 41, in new_job = CHANGE_JOB(job) File "C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.7\assignments\cleaning_data.py", line 29, in CHANGE_JOB return job_code UnboundLocalError: local variable 'job_code' referenced before assignment – Soumya Pandey Nov 26 '19 at 08:08
  • @SoumyaPandey Re-thinking about it, `job_code = JOB_DICT.get(job)` will anyway give you `None` if it's not in the dictionary, so you can just drop the `if` condition, and don't need to pre-define `job_code`. – Aryerez Nov 26 '19 at 08:11
  • adding job_code = None before the if helped. But it made changes in the DB to only the first row. ``` def CHANGE_JOB(job): job_code = None if job in JOB_DICT: job_code = JOB_DICT.get(job) return job_code ``` – Soumya Pandey Nov 26 '19 at 08:11

0 Answers0