Search code examples
pythonmysqlpymysql

columns with default NULL values returns as (None,) on SELECT python pymysql


I'm using pymysql module to connect the database to the python project.But I'm getting an error when I run a SELECT query to get a value of a column This is my code block for sql query.

sql_check_record = 'SELECT `suggestions` FROM `temp_suggestions` WHERE `word`= "%s"' %key
cursor.execute(sql_check_record) 
new_val = cursor.fetchone() 
print(new_val)

When the value of key exists in the word column, and suggestion column is empty it returns this (None,). But when the value of key is not exist in the word column it returns None

I want to check whether the suggestion is NULL. As when the value of suggestion is NULL it returns (None,) how can I check whether it's NULL or not(if anything exists in the column). Because I want to concatenate to the existing value if a value exists.


Solution

  • sql_check_record = 'SELECT `suggestions` FROM `temp_suggestions` WHERE `word`= "%s"' %key
    cursor.execute(sql_check_record) 
    new_val = cursor.fetchone() 
    print(new_val)
    

    You can use

    (new_val,)=cursor.fetchone()
    if(new_val is None)
    

    to check. If it returns (None,) then it's true.