2

I tried to execute this query in xamarin

SELECT * FROM GPSPoint WHERE SQRT( POW({0}-lat,2) + POW({1}-lng,2) ) < 5*5

But it gave me the error message

SQLite.SQLiteException: no such function: SQRT

And after searching I found that Android developers can use the sqlite3_create_function method to create new functions in SQLite.

How can I do the same thing in xamarin?

Community
  • 1
  • 1
Abozanona
  • 2,261
  • 1
  • 24
  • 60
  • AFAIK Just like with stored procedures, functions cannot be created in SQLite. If you find I'm wrong please LMK as would be beneficial for me. – Jared Kove Aug 15 '16 at 12:14
  • im not even sure thats a valid query either. SQL queries would be `WHERE col = value` – Takarii Aug 15 '16 at 21:15
  • @Takarii It is a valid query in c# syntax. The original statement is `var query = conn.Query(string.Format("SELECT * FROM GPSPoint WHERE SQRT( POW({0}-lat,2) + POW({1}-lng,2) ) < 5*5", lat, lng));` – Abozanona Aug 16 '16 at 05:25
  • @jj- But android developers can create new functions. please refer to [this SO answer](http://stackoverflow.com/a/8283265/4614264). – Abozanona Aug 16 '16 at 05:30

1 Answers1

0

In the following example i make my custom sqlite function called MySqliteAdd , which has two arguments as input and their sum as a result

[SqliteFunction(FuncType = FunctionType.Scalar, Arguments = 2, Name = "MySqliteAdd")]
public class MySqliteAdd : SqliteFunction
{
   public override object Invoke(object[] args){
        return System.Convert.ToInt32(args[0]) + System.Convert.ToInt32(args[1]);
   }
}

You can use it in a sqlite query like below

Select MySqliteAdd(25,35)

which will give you a result of 60

finally you have to register the function before you initialise your connection

SqliteFunction.RegisterFunction(typeof(MySqliteAdd));
Loukas
  • 616
  • 1
  • 6
  • 22