views:

407

answers:

1

I am having issues with getting Guids to match in SQLite (0.4.8) when using parameters, when I use something like userGuid = 'guid here' it works, but userGuid = @GuidHere it does not. Anyone have any ideas?

Create:

CREATE TABLE Users
(
   UserGuid TEXT PRIMARY KEY NOT NULL, 
   FirstName TEXT, 
   LastName TEXT
)

Sample Data:

INSERT INTO Users (UserGuid, FirstName, LastName) 
VALUES ('e7bf9773-8231-44af-8d53-e624f0433943', 'Bobby', 'Bobston')

Delete Statement (Working):

DELETE FROM Users WHERE UserGuid = 'e7bf9773-8231-44af-8d53-e624f0433943'

Delete Statement (Not Working):

DELETE FROM Users WHERE UserGuid = @UserGuid

Here is a C# program showing my issue:

using System;
using System.Data.SQLite;

namespace SQLite_Sample_App
{
    class Program
    {
        static void Main(string[] args)
        {
            Do();
            Console.Read();
        }

        static void Do()
        {
            using(SQLiteConnection MyConnection = new SQLiteConnection("Data     Source=:memory:;Version=3;New=True"))
            {
                MyConnection.Open();
                SQLiteCommand MyCommand = MyConnection.CreateCommand();
                MyCommand.CommandText = @"
                    CREATE TABLE Users
                    (
                       UserGuid TEXT PRIMARY KEY NOT NULL, 
                       FirstName TEXT, 
                       LastName TEXT
                    );

                    INSERT INTO Users (UserGuid, FirstName, LastName) 
                    VALUES ('e7bf9773-8231-44af-8d53-e624f0433943', 'Bobby', 'Bobston');
                    ";
                MyCommand.ExecuteNonQuery();

                MyCommand.CommandText = "SELECT Count(*) FROM Users WHERE UserGuid = 'e7bf9773-8231-44af-8d53-e624f0433943'";
                Console.WriteLine("Method One: {0}", MyCommand.ExecuteScalar());

                MyCommand.Parameters.AddWithValue("@UserGuid", new Guid("e7bf9773-8231-44af-8d53-e624f0433943"));
                MyCommand.CommandText = "SELECT Count(*) FROM Users WHERE UserGuid = @UserGuid";
                Console.WriteLine("Method Two: {0}", MyCommand.ExecuteScalar());                    
            }
        }
    }
}

EDIT:

Well it seems that the AddParamWithValue translates to a 16byte rep of a Guid so I guess I really do have to translate all guids to strings first... kinda annoying.

+4  A: 

Try just passing the string of the GUID to your AddWithValue call, rather than the GUID object.

So instead of

MyCommand.Parameters.AddWithValue(
    "@UserGuid", new Guid("e7bf9773-8231-44af-8d53-e624f0433943"));

Do this:

MyCommand.Parameters.AddWithValue(
    "@UserGuid", "e7bf9773-8231-44af-8d53-e624f0433943");
Robert Harvey