1

If for example I had this database:

id  result
__________
1     55
2     67

And a function isPrime(number) which returns a boolean true or false if the number is prime,

Is there anyway for me to select numbers based on if isPrime = true;

something like this..

"SELECT * FROM DB WHERE result.isPrime()" 

Thanks for any help

4
  • if isPrime() is a Python function, then no, you cannot call that function inside your query because the query is executed inside the database, and there is not Python interpreter or python data types inside the database. Commented Mar 7, 2019 at 11:46
  • IIRC, the python sqlite bindings let you write user defined functions in Python that can be called from SQL statements. Commented Mar 7, 2019 at 12:18
  • @Shawn do you know where get documentation on this? Commented Mar 7, 2019 at 12:25
  • The python sqlite documentation: docs.python.org/3/library/sqlite3.html Commented Mar 7, 2019 at 12:27

1 Answer 1

2

Assuming conn is a sqlite connection object, something like:

conn.create_function("isprime", 1, isPrime)

should let you call the function in SQL statements executed by that connection.

conn.execute("SELECT * FROM db WHERE isprime(result)")
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.