[英]how to select from sqlite3.db where i compare a result of equation that has variables from the db?
if i have a table like this:如果我有这样一张桌子:
("CREATE TABLE table (name text,x real, y real, state text) ")
[('name_1' ,x, y, 'active'), ('name_2' ,x, y, 'active')]
And two variable ref_1 = math.cos(some_input)
, ref_2 = math.cos(some_input)
和两个变量
ref_1 = math.cos(some_input)
, ref_2 = math.cos(some_input)
and i want to SELECT
only state = 'active'
that their math.cos(x) < ref_1
and math.cos(y) < ref_2
我只想
SELECT
state = 'active'
他们的math.cos(x) < ref_1
和math.cos(y) < ref_2
what can i add to this line: curs.execute("SELECT * FROM table WHERE state = 'active #### code'
to endup with what i want. m not used to work a lot around databases我可以向这一行添加什么:
curs.execute("SELECT * FROM table WHERE state = 'active #### code'
以结束我想要的内容。我不习惯在数据库周围工作很多
This is the original line in my Code only lat and lon are variables inside the db这是我代码中的原始行,只有 lat 和 lon 是数据库中的变量
active = 'active'
curs.execute(f"SELECT * FROM drivers WHERE state = {active} and radius >= {math.sqrt( (lat - float(user_lat) )**2 + (lon - float(user_lon) )**2)} and min_radius =< {-math.sqrt( (lat - float(user_lat) )**2 + (lon - float(user_lon) )**2)} ")
i get this output:我得到这个 output:
curs.execute(f"SELECT * FROM drivers WHERE state = {active} and radius >= {math.sqrt( (lat - float(user_lat) )**2 + (lon - float(user_lon) )**2)} and min_radius =< {-math.sqrt( (lat - float(user_lat) )**2 + (lon - float(user_lon) )**2)} ")
NameError: name 'lat' is not defined
You can use af string to input different variables您可以使用 af 字符串输入不同的变量
curs.execute(f"SELECT * FROM table WHERE state = 'active and ref_1 < {math.cos(x)} code and ref_2 > {math.cos(y)}")
import sqlite3
import math
user_lat = #
user_lon = #
radius_length = #
state = 'active'
linktoauth = sqlite3.connect('DRIVERS.db')
curs = linktoauth.cursor()
def _radius_len(x, a, y, b):
result = math.sqrt((x - a)**2 + (y - b)**2)
return result
linktoauth.create_function('rad', 4, _radius_len)
curs.execute(f"SELECT * FROM drivers WHERE state = ? and rad(lat, ?, lon, ?) <= ? ",(state, user_lat, user_lon, radius_length,))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.