Error with insert value in SQLite3 with python 2.7 script

Question:

I made a script to insert values taken from a json into a database.
The script works but if it encounters an EMPTY value (example: "") then it returns error without inserting anything. How can I avoid this error?

Here is the script

connection = sqlite3.connect(PATH_DB)
cursor = connection.cursor()
table='''CREATE TABLE IF NOT EXISTS trieste(
   id          INTEGER  PRIMARY KEY
  ,farmaciaId  INTEGER  
  ,codOpenData INTEGER  
  ,ragSoc      VARCHAR(40) 
  ,distanza    NUMBER(10,0) 
  ,indirizzo   VARCHAR(48) 
  ,latitudine  NUMERIC(9,6) 
  ,longitudine NUMERIC(9,6) 
  ,telefono    VARCHAR(11) 
  ,paese       VARCHAR(6) 
  ,provincia   VARCHAR(2) 
  ,localita    VARCHAR(22) 
  ,cap         INTEGER  
  ,stato       VARCHAR(1) 
  ,data        DATE  
  ,oraInizio   VARCHAR(8) 
  ,oraFine     VARCHAR(8)
);'''
cursor.execute(table)

traffic = json.load(open('/var/www/html/src/trieste.json'))
columns = ['farmaciaId','codOpenData','ragSoc','distanza','indirizzo','latitudine','longitudine','telefono','paese','provincia','localita','cap','stato','data','oraInizio','oraFine']
for row in traffic:
    keys= tuple(row[c] for c in columns)
    cursor.execute('INSERT OR REPLACE INTO trieste(farmaciaId,codOpenData,ragSoc,distanza,indirizzo,latitudine,longitudine,telefono,paese,provincia,localita,cap,stato,data,oraInizio,oraFine) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',keys)

Thanks

Error:
Traceback (most recent call last):
  File "riempidatabase.py", line 32, in <module>
    keys= tuple(row[c] for c in columns)
  File "riempidatabase.py", line 32, in <genexpr>
    keys= tuple(row[c] for c in columns)
KeyError: 'oraInizio'

json oraInizio and oraFine are MISSING in some cases

 {
      "farmaciaId": 558,
      "codOpenData": 5496,
      "ragSoc": "AL CENTAURO",
      "distanza": 0,
      "indirizzo": "Via Domenico Rossetti, 33",
      "latitudine": 45.649959,
      "longitudine": 13.78522,
      "telefono": "040 633080",
      "paese": "Italia",
      "provincia": "1",
      "localita": "Trieste",
      "cap": 34139,
      "stato": "C",
      "data": "2023-03-11"
   },
Asked By: NicolaIta

||

Answers:

To avoid the KeyError when encountering empty values in the JSON, you can use a default value for missing keys using the dictionary get method. For example, you can set the default value of the oraInizio and oraFine keys to an empty string if they are missing in the JSON.

traffic = json.load(open('/var/www/html/src/trieste.json'))
columns = ['farmaciaId','codOpenData','ragSoc','distanza','indirizzo','latitudine','longitudine','telefono','paese','provincia','localita','cap','stato','data','oraInizio','oraFine']
for row in traffic:
    keys = []
    for c in columns:
        # set default value to empty string if key is missing in JSON
        value = row.get(c, '')
        keys.append(value)
    cursor.execute('INSERT OR REPLACE INTO trieste(farmaciaId,codOpenData,ragSoc,distanza,indirizzo,latitudine,longitudine,telefono,paese,provincia,localita,cap,stato,data,oraInizio,oraFine) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', tuple(keys))

This should allow the script to insert a default value of an empty string for any missing keys in the JSON, avoiding the KeyError when trying to access a missing key.

Answered By: Engr Fahad Safi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.