sql - OperationalError when inserting into sqlite 3 in Python -


i'm populating database data parse json. when execute insert statement, error: sqlite3.operationalerror: no such column: none. of json data returns null, cause python insert none table, believe should fine? know problem is?

traceback: traceback (most recent call last): file "productinfoscraper.py", line 71, in <module> ")")

my insert statement in python:

cursor.execute("insert productinfo values(" +         str(data["data"]["product_id"]) + ", " +         "'" + str(data["data"]["product_name"]) + "'" + ", " +         "'" + str(data["data"]["ingredients"]) + "'" + ", " +         "'" + str(data["data"]["serving_size"]) + "'" + ", " +         str(data["data"]["calories"]) + ", " +         str(data["data"]["total_fat_g"]) + ", " +         str(data["data"]["total_fat_percent"]) + ", " +         str(data["data"]["fat_saturated_g"]) + ", " +         str(data["data"]["fat_saturated_percent"]) + ", " +         str(data["data"]["fat_trans_g"]) + ", " +         str(data["data"]["fat_trans_percent"]) + ", " +         str(data["data"]["cholesterol_mg"]) + ", " +         str(data["data"]["sodium_mg"]) + ", " +         str(data["data"]["sodium_percent"]) + ", " +         str(data["data"]["carbo_g"]) + ", " +         str(data["data"]["carbo_percent"]) + ", " +         str(data["data"]["carbo_fibre_g"]) + ", " +         str(data["data"]["carbo_fibre_percent"]) + ", " +         str(data["data"]["carbo_sugars_g"]) + ", " +         str(data["data"]["protein_g"]) + ", " +         str(data["data"]["vitamin_a_percent"]) + ", " +         str(data["data"]["vitamin_c_percent"]) + ", " +         str(data["data"]["calcium_percent"]) + ", " +         str(data["data"]["iron_percent"]) + ", " +         "'" + str(data["data"]["micro_nutrients"]) + "'" +  ", " +         "'" + str(data["data"]["tips"]) + "'" + ", " +         str(data["data"]["diet_id"]) + ", " +         "'" + str(data["data"]["diet_type"]) + "'" +         ")") 

my create table statement:

cursor.execute("create table productinfo(product_id int, product_name text,\   ingredients text, serving_size text, calories int, total_fat_g int,\   total_fat_percent int, fat_saturated_g int, fat_saturated_percent int,\   fat_trans_g int, fat_trans_percent int, cholesterol_mg int, sodium_mg\   int, sodium_percent int, carbo_g int, carbo_percent int, carbo_fibre_g\   int, carbo_fibre_percent int, carbo_sugars_g int, protein_g int,\   vitamin_a_percent int, vitamin_c_percent int, calcium_percent int,\   iron_percent int, micro_nutrients text, tips text, diet_id int, diet_type\   text)") 

first, should never build sql statements way. the documentation explicitly says:

usually sql operations need use values python variables. shouldn’t assemble query using python’s string operations because doing insecure; makes program vulnerable sql injection attack (see http://xkcd.com/327/ humorous example of can go wrong).

instead, use db-api’s parameter substitution.

and there other problems besides security problem—it's hard quoting , converting right, easy make simple mistake somewhere, , hard debug such mistakes when make them.

the right way use named placeholder each key, , pass data["data"] parameter mapping:

cursor.execute("""insert productinfo values(     :product_id,      :product_name,     ...     :diet_type)""", data["data"]) 

i have no idea whether solve problem without enough sample code , data test it, eliminate many potential sources of error, i'd give odds will.

the problem 1 of values you're trying insert , aren't wrapping in quotes, str(data["data"]["sodium_mg"]), python none value.

just putting unquoted none sql statement means want copy value of column named "none" column. if want use null value, or string "none", or else, have write properly.

parameter binding take care of automatically inserting null value.

but 1 example of meant "hard quoting , converting right", can't sure it's 1 you're seeing.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -