Query = 'com pu'
Query = Query.split()
busi = ""
for data in Query:
busi += Business.objects.filter(Keywords__icontains=data)
When I use '+' symbol after busi variable for add all data according to filter that give error
Exception Type: TypeError
Exception Value:Can't convert 'QuerySet' object to str implicitly
How can I achieve that different Query part which split by that all give different-2 data and and both data in single busi object
from django.db.models import Q
Query = Query.split()
query = Q()
for data in Query:
query |= Q(keyword_name__icontains=data)
Business= Business.objects.filter(query)
Instead use &:
busi = None
q = Business.objects.filter(Keywords__icontains=data)
if busi is None:
busi = q
else:
busi = busi & q
Related
I am following the Snowflake Python Connector docs for variable binding to avoid SQL injection. I successfully set up a db connection with the following dict of credentials:
import snowflake.connector
CONN = snowflake.connector.connect(
user=snowflake_creds['user'],
password=snowflake_creds['password'],
account=snowflake_creds['account'],
warehouse=snowflake_creds["warehouse"],
database=snowflake_creds['database'],
schema=snowflake_creds['schema'],
)
cur = CONN.cursor(snowflake.connector.DictCursor)
The following block works fine and I get back query results, hard-coding the table name and using the standard format binding:
command = ("SELECT * FROM TEST_INPUT_TABLE WHERE batch_id = %s")
bind_params = (2)
results = cur.execute(command % bind_params).fetchall()
Similarly, this block works fine, using the pyformat binding:
command = ("SELECT * FROM TEST_INPUT_TABLE WHERE batch_id = %(id)s")
bind_params = {"id": 2}
results = cur.execute(command, bind_params).fetchall()
But the following two blocks both result in a ProgrammingError (pasted below the second block):
command = ("SELECT * FROM %s WHERE batch_id = %s")
bind_params = ("TEST_INPUT_TABLE", 2)
results = cur.execute(command, bind_params).fetchall()
command = ("SELECT * FROM %(tablename)s WHERE batch_id = %(id)s")
bind_params = {
"tablename": "TEST_INPUT_TABLE",
"id": 2
}
results = cur.execute(command, bind_params).fetchall()
ProgrammingError: 001011 (42601): SQL compilation error:
invalid URL prefix found in: 'TEST_INPUT_TABLE'
Is there some difference between how strings and ints get interpolated? I would
not think it would make a difference but that is all I can think of. Am I
missing something simple here? I don't want to have to choose between hard-coding the table name and putting the system at risk of SQL injection. Thanks for any guidance.
You should be wrapping your bind variables with an INDENTIFER() function when they reference an object, rather than a string literal. For example:
command = ("SELECT * FROM IDENTIFIER(%(tablename)s) WHERE batch_id = %(id)s")
https://docs.snowflake.com/en/sql-reference/identifier-literal.html
Give that a try.
When making query with OrderBy it always query wrong result whereby repeating first document multiple times added to list.
q := session.Advanced().DocumentQueryAllOld(reflect.TypeOf(&models.List{}), "", "user", false)
//q = q.WhereNotEquals("Doc.hh", "Tarzan")
q = q.OrderBy("Docnn")
q = q.Statistics(&statsRef)
//q = q.Skip(0)
//q = q.Take(6)
q.ToList(&queryResult)
If no index before it will query right result but if existing index auto created by a different OrderBy value it will query wrong result
I'm performing the postgres query with psycopg2, stored in function columnsearch(). My aim is to be able to view the data in each field by using the column name in the return statement. I'm receiving the error:
TypeError: 'int' object is not subscriptable
Does anyone know how I can rectify this?
def columnsearch():
con = psycopg2.connect(connectstring)
cur = con.cursor(cursor_factory=e.DictCursor)
cur.execute("SELECT * FROM radio_archive_ind.radio_archive_ind WHERE tape_number = 'TP00001'")
rows = cur.fetchone()
for r in rows:
return r["tape_number"]
print columnsearch()
Note that .fetchone() returns a single sequence so you can just do:
def columnsearch():
con = psycopg2.connect(connectstring)
cur = con.cursor(cursor_factory=e.DictCursor)
cur.execute("""SELECT *
FROM radio_archive_ind.radio_archive_ind
WHERE tape_number = 'TP00001'""")
row = cur.fetchone()
return row["tape_number"]
I am trying to make a query to work, but with no luck. I am developing on AppEngine, using JPA for data access.
When I set the query string as:
"SELECT m FROM ModelLocationJPA m WHERE m.latitude BETWEEN :lowerLatitude AND :higherLatitude"
it works, and also :
"SELECT m FROM ModelLocationJPA m WHERE m.longitude BETWEEN :lowerLongitude AND :higherLongitude"
works. So, I guess that is because I messed something with the multiple "AND"'s, but I can't figure out what.
I am trying to make the following work but I get the error: javax.persistence.PersistenceException: Illegal argument
EntityManager em = null;
List<ModelLocationJPA> dbModelLocations = null;
ArrayList<ModelLocation> modelLocations = new ArrayList<ModelLocation>();
String aQuery = "SELECT m FROM ModelLocationJPA m WHERE ( (m.latitude BETWEEN :lowerLatitude AND :higherLatitude) AND ( m.longitude BETWEEN :lowerLongitude AND :higherLongitude ))";
try {
em = EMF.get().createEntityManager();
Query query = em.createQuery(aQuery);
query.setParameter("lowerLatitude",box.getMinLatitude());
query.setParameter("higherLatitude",box.getMaxLatitude());
query.setParameter("lowerLongitude",box.getMinLongitude());
query.setParameter("higherLongitude",box.getMaxLongitude());
dbModelLocations = query.getResultList();
} finally {
em.close();
}
If someone could please tell me what is wrong, it would be great.
Thank you.
The documentation says:
A query may only use inequality filters (<, <=, >=, >, !=) on one
property across all of its filters.
If i want to run a GqlQuery with a variable i've set is that possible?
for example:
myNumber = 4
myResult = db.GqlQuery("SELECT * from myData WHERE filter = myNumber")
this results in:
Parse Error: Invalid WHERE condition at symbol
Am i going about this all wrong? Thanks for your time.
According to The Google Appengine Documentation, your code might look like this:
myNumber = 4
myResult = db.GqlQuery("SELECT * FROM myData WHERE filter = :num", num=myNumber)
or
myNumber = 4
myResult = db.GqlQuery("SELECT * FROM myData WHERE filter = :1", myNumber)
depending on whether you want to use a named or positional variable.