views:

139

answers:

1

I an attempting to use a SQLiteFunction from my C# and ADO.NET code. Can anyone say why I get this problem?

An unhandled exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll Additional information: SQLite error near "DEMOIT": syntax error

I am using .NET 3.5 x86 with SQLite ADO.NET 1.0.65 - Help!

public class Program
    {
        static void Main( string[ args )
        {
            test();
        }


        public static void test()
        {
            SQLiteConnection sqlConn = new SQLiteConnection( "Data Source=TestFoods.db;" );
            sqlConn.Open();
            SQLiteCommand sqlCmd = new SQLiteCommand( "PRAGMA integrity_check" , sqlConn);
            sqlCmd.ExecuteNonQuery();
            SQLiteFunction.RegisterFunction( typeof(DEMOIT) );
            sqlCmd = new SQLiteCommand( "SELECT * FROM Foods Where Foods.Name DEMOIT '$butter' " , sqlConn );
            sqlCmd.CommandType = CommandType.Text;
            SQLiteDataAdapter liteAdapter = new SQLiteDataAdapter( sqlCmd );
            DataSet dataSet = new DataSet();
            liteAdapter.Fill( dataSet , "Foods" );
        }

    }

    [SQLiteFunction( Name = "DEMOIT" , Arguments = 1 , FuncType = FunctionType.Scalar )]
    public class DEMOIT : SQLiteFunction
    {
        public override object Invoke( object[] args )
        {
            return Convert.ToString( args[0] ) ;
        }
    }

Thanks!

+3  A: 

DEMOIT is a function, but you are using it as if its an operator. Try this:

sqlCmd = new SQLiteCommand( "SELECT * FROM Foods Where Foods.Name = DEMOIT('$butter')" , sqlConn );

or:

sqlCmd = new SQLiteCommand( "SELECT * FROM Foods Where DEMOIT(Foods.Name) = '$butter'" , sqlConn );

An Sample from my old project http://war3share.codeplex.com/ :

SQL:

select replayHash from customData where key='Rating' and String2Int(value) < 8

Code:

using System.Data.SQLite;

namespace War3Share.Client.DAL
{
    [SQLiteFunction(Arguments = 1, FuncType = FunctionType.Scalar, Name = "String2Int")]
    class String2Int : SQLiteFunction
    {
        public override object Invoke(object[] args)
        {
            string s = args[0] as string;
            return int.Parse(s);
        }
    }
}
deerchao
I'm still getting a syntax error. I have reduced my code to a bare minimum (slightly different from my original post) but still no-go. Do you know where a working sample of this stuff exists? The phxsoftware forums are "silent" on SQLiteFunction.Thanks.
ScSub
Hi, I added an example, check it out.
deerchao
Perfect. Your sample worked out. Thanks!
ScSub