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 

[Solved] Mono and Sqlite

 
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: Mon Nov 28, 2005 2:11 pm    Post subject: [Solved] Mono and Sqlite Reply with quote

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:
    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:
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 Confused

[edit] ups I mean hair Embarassed [/edit]


Last edited by tedius on Tue Nov 29, 2005 10:39 am; edited 2 times in total
Back to top
View user's profile Send private message MSN Messenger
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Mon Nov 28, 2005 2:23 pm    Post subject: Re: Mono and Sqlite Reply with quote

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


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 Smile
Back to top
View user's profile Send private message Visit poster's website
tommieb



Joined: Sat Oct 22, 2005 2:08 pm
Posts: 6

PostPosted: Mon Nov 28, 2005 3:12 pm    Post subject: RE: Re: Mono and Sqlite Reply with quote

Hmmm....have you tried taking out the @ from the param.ParameterName = "@Param"; line??
HTH,
Tom.
Back to top
View user's profile Send private message
tommieb



Joined: Sat Oct 22, 2005 2:08 pm
Posts: 6

PostPosted: Mon Nov 28, 2005 3:16 pm    Post subject: RE: Re: Mono and Sqlite Reply with quote

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?
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 Nov 28, 2005 3:42 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message MSN Messenger
tedius



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

PostPosted: Tue Nov 29, 2005 10:38 am    Post subject: [Solved] Mono and Sqlite Reply with quote

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.
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