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!
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With