Mono and sqlite (again)

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Mono and sqlite (again)

Postby tedius » Wed Dec 07, 2005 4:23 pm

I'm having some more problems with mono and sqlite (version 3).

This time it is that I have a method to insert some values into the data base, and I want to be able to insert NULL values. But I can't find the way to do it.

This is my insert method
Code: Select all
public void Insert(string sName, object oVal)
{
    string sql = @"
        INSERT INTO _params
        (
            name,
            val
        ) VALUES
        (
             :name,
             :val
        )";

    SqliteCommand cmd = new SqliteCommand(sql, mConn);
    SqliteParameter param = new SqliteParameter();
    param.ParameterName = ":name";
    param.Value = sName;
    cmd.Parameters.Add(param);

    param = new SqliteParameter();
    param.ParameterName = ":val";
    param.Value = oVal;
    cmd.Parameters.Add(param);

    cmd.ExecuteNonQuery();
}


The table was created with
Code: Select all
CREATE TABLE _params
(
    name TEXT NOT NULL UNIQUE,
    val TEXT
)


When I run this code I get a NullRefrenceException, if I try and insert a null. I have also tried to changing the line 'param.Value = oVal;' to
Code: Select all
if (oVal == null)
    param.Value = "NULL"; // also tried "\0" and ""
else
    param.Value = oVal;


But the code either crashes or uses the value as a string and not a null value.

Can anyone help me insert NULL values via the Parameter object.
Last edited by tedius on Mon Dec 12, 2005 1:05 pm, edited 1 time in total.
User avatar
tedius
 
Posts: 84
Joined: Fri Apr 08, 2005 3:20 pm
Location: Cambridge, England

RE: Mono and sqlite (again)

Postby nordle » Sun Dec 11, 2005 2:41 pm

I can't program, so sorry if I've missed the obvious, but the database design sais that it won't allow null values into name. It sais "NOT NULL".

Also, your sql sais insert into _params, not insert into _internal.

Im not sure i've understood / read that right though :)
I think, therefore I compile
User avatar
nordle
LXF regular
 
Posts: 1500
Joined: Fri Apr 08, 2005 9:56 pm

Postby tedius » Mon Dec 12, 2005 1:05 pm

Nordle,

Thanks for that, I do seem to have made a typo there (I have edited it to correct it now) :) The code wasn't I direct copy as I have cut some stuff out to make it smaller, though essentially it is the same.

As for your comment
It says "NOT NULL"
This is for the name and not that value which is what I'm trying to set to null.

If people haven't guessed this is just a simple table that holds key:value pairs. As it happened I would never insert NULL values into this table, but I have other larger ones where inserting NULL values makes more sense than inserting an empty string (my current work around)
User avatar
tedius
 
Posts: 84
Joined: Fri Apr 08, 2005 3:20 pm
Location: Cambridge, England


Return to Programming

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests