Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Mono and sqlite (again)

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
tedius



Joined: Fri Apr 08, 2005 4:20 pm
Posts: 83
Location: Cambridge, England

PostPosted: Wed Dec 07, 2005 4:23 pm    Post subject: Mono and sqlite (again) Reply with quote

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:
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:
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:
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
Back to top
View user's profile Send private message MSN Messenger
nordle
LXF regular


Joined: Fri Apr 08, 2005 10:56 pm
Posts: 1500

PostPosted: Sun Dec 11, 2005 2:41 pm    Post subject: RE: Mono and sqlite (again) Reply with quote

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 Smile
_________________
I think, therefore I compile
Back to top
View user's profile Send private message
tedius



Joined: Fri Apr 08, 2005 4:20 pm
Posts: 83
Location: Cambridge, England

PostPosted: Mon Dec 12, 2005 1:05 pm    Post subject: Reply with quote

Nordle,

Thanks for that, I do seem to have made a typo there (I have edited it to correct it now) Smile 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
Quote:
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)
Back to top
View user's profile Send private message MSN Messenger
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast