3

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!

1
  • There is a syntax error in your SQL. Near DEMOIT. Commented Dec 22, 2009 at 1:36

1 Answer 1

6

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Hi, I added an example, check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.