[Solved] Mono and Sqlite

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

[Solved] Mono and Sqlite

Postby tedius » Mon Nov 28, 2005 2:11 pm

I'm having some problems with a database application I'm writing in C# on mono. To be more precise it is the SqliteParameter object that is causing me hassle.

This is a snippet of my code;
Code: Select all
    public static class Connection
    {
        private static SqliteConnection mConn = null;

        public static bool Connect(string filename)
        {
            if (mConn != null)
                mConn.Close();
               
            string connString = "version=3,URI=file:" + filename;
           
            mConn = new SqliteConnection(connString);
           
            return true;
        }
       
        public static void Create()
        {
            string sql = @"CREATE TABLE _table
                               (param TEXT NOT NULL,
                                value TEXT NOT NULL);

            SqliteCommand cmd = new SqliteCommand(sql, mConn);

            // insert the default values
            Insert("version", mdVersion);
            Insert("ready", true);         
        }
       
        private static void Insert(string var, object val)
        {
            string sql = "INSERT INTO _table VALUES (@Param, @Val)";

            SqliteCommand cmd = null;
            SqliteParameter param = null;
           
            try
            {
                mConn.Open();
               
                cmd = new SqliteCommand(sql, mConn);
           
                param = new SqliteParameter();
                param.ParameterName = "@Param";
                param.Value = var;
           
                cmd.Parameters.Add(param);
           
                param = new SqliteParameter();
                param.ParameterName = "@Val";
                param.Value = val.ToString();
           
                cmd.Parameters.Add(param);
               
                cmd.ExecuteNonQuery();
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
                mConn.Close();
            }
        }
    }


Can any one see what I'm doing wrong with this. When I run it I get the following error;

Code: Select all
Unhandled Exception: System.ApplicationException: unrecognized token: "@"
in <0x00186> Mono.Data.SqliteClient.SqliteCommand:Prepare ()
in <0x00085> Mono.Data.SqliteClient.SqliteCommand:ExecuteReader (CommandBehavior behavior, Boolean want_results, System.Int32 rows_affected)
in <0x00023> Mono.Data.SqliteClient.SqliteCommand:ExecuteNonQuery ()
in <0x001a9> PiggyBank.Database.Connection:Insert (System.String var, System.Object val)
in <0x00040> PiggyBank.Database.Connection:Create ()
in <0x00033> MainClass:Main (System.String[] args)


Some one please help, as I'm pulling out my heir :?

[edit] ups I mean hair :oops: [/edit]
Last edited by tedius on Tue Nov 29, 2005 10:39 am, edited 2 times in total.
User avatar
tedius
 
Posts: 83
Joined: Fri Apr 08, 2005 3:20 pm
Location: Cambridge, England

Re: Mono and Sqlite

Postby M-Saunders » Mon Nov 28, 2005 2:23 pm

tedius wrote:Some one please help, as I'm pulling out my heir :?


Hey, just because it won't run, doesn't mean you need to cut people out of your will.

(Sorry. Had to do it.)

M :-)
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

RE: Re: Mono and Sqlite

Postby tommieb » Mon Nov 28, 2005 3:12 pm

Hmmm....have you tried taking out the @ from the param.ParameterName = "@Param"; line??
HTH,
Tom.
tommieb
 
Posts: 6
Joined: Sat Oct 22, 2005 1:08 pm

RE: Re: Mono and Sqlite

Postby tommieb » Mon Nov 28, 2005 3:16 pm

Oh yeah, I'd also guess you'd have to declare the parameters in the line that contains the SQL....INSERT INTO _table VALUES (@Param, @Val), from my mssql exp, the db manager would need to know what are the parameter typings for each of them...perhaps something like this - DECLARE @Param Text, DECLARE @Val Text prior to the INSERT sql?
tommieb
 
Posts: 6
Joined: Sat Oct 22, 2005 1:08 pm

Postby tedius » Mon Nov 28, 2005 3:42 pm

From what I have understand from the Mono and .NET documentation then the parameter substitution is happening before the sql is passed to Sqlite. As for the other suggestion, I tried that and it had no effect.
User avatar
tedius
 
Posts: 83
Joined: Fri Apr 08, 2005 3:20 pm
Location: Cambridge, England

[Solved] Mono and Sqlite

Postby tedius » Tue Nov 29, 2005 10:38 am

I have found out what the problem is.

I'm using sqlite3 and for this they have changed the @ for :. So once I had replaced all my @ signs with : signs it worked fine.
User avatar
tedius
 
Posts: 83
Joined: Fri Apr 08, 2005 3:20 pm
Location: Cambridge, England


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests