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?

4
  • 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. Commented Aug 15, 2016 at 12:14
  • im not even sure thats a valid query either. SQL queries would be WHERE col = value Commented Aug 15, 2016 at 21:15
  • @Takarii It is a valid query in c# syntax. The original statement is var query = conn.Query<GPSPoint>(string.Format("SELECT * FROM GPSPoint WHERE SQRT( POW({0}-lat,2) + POW({1}-lng,2) ) < 5*5", lat, lng)); Commented Aug 16, 2016 at 5:25
  • @jj- But android developers can create new functions. please refer to this SO answer. Commented Aug 16, 2016 at 5:30

1 Answer 1

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

Comments

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.