CODe
CODe

Reputation: 2301

How to: Representing NULL in SQLite queries

I have a database winforms application using SQLite written in C#. I'm attempting to do some SQLite queries with the C# wrapper, but am having some issues with the query where I'm checking for NULL values. Here's the calling statement.

sqliteQuery.selectFromDatabase("*", "WHERE (FirstNotify = NULL) AND (SecondNotify != NULL) AND (ThirdNotify = NULL)");

And here's the code behind it.

public DataTable selectFromDatabase(String column, String filter)
    {
        string SQL = "SELECT " + column + " FROM SUBCONTRACTOR " + filter;
        SQLiteCommand cmd = new SQLiteCommand(SQL);
        cmd.Connection = connection;
        SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
        DataSet ds = new DataSet();
        try
        {
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            return dt;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
            return null;
        }
        finally
        {
            cmd.Dispose();
            connection.Close();
        }
    }

The query is not returning anything at all, when it should be returning a few records. Am I handling the NULL checks correctly? I've found a few other posts that use WHERE (VAR IS NULL) instead of using equals. I've tried both, but am unsure as to how to handle "not equals" when I was using "IS" instead of "equals". Anyone have any insight?

Thanks!

Upvotes: 1

Views: 2956

Answers (2)

Femaref
Femaref

Reputation: 61447

You already got it right - to compare for NULL you have to use var is NULL. if you want to compare for unequality, use var is not NULL.

Upvotes: 1

Anthony Pegram
Anthony Pegram

Reputation: 126892

You want to use IS NULL and IS NOT NULL when comparing equality/inequality to null.

Upvotes: 5

Related Questions