I would like to execute a query with a variable
I tried to do this but it doesn't work
def requeteDB(sql_request):
with connection.cursor() as cursor:
cursor.execute(sql_request)
row = cursor.fetchall()
return row
def queryWithVar():
var = 'something with spaces'
return('''
Select col1
From table1
Where col2 = %s
''', [var])
queryWithVar()
('\n Select col1\n From table1\n Where col2 = %s\n ', ['something'])
requeteDB(queryWithVar())
TypeError: argument 1 must be a string or unicode object: got tuple instead
The problem is that your function returns tuple, so you need to unpack it when provide it's result to execute method, just use * syntax:
def requeteDB(sql_request):
with connection.cursor() as cursor:
cursor.execute(*queryWithVar())
row = cursor.fetchall()
return row
Related
I'm using Python dbf-0.99.1 library from Ethan Furman. This approach to add record to table fails:
tab = dbf.Table( "MYTABLE" )
tab.open(mode=dbf.READ_WRITE)
idx = tab.create_index(lambda rec: (rec.id if not is_deleted(rec) else DoNotIndex ) ) # without this, append works
rec = { "id":id, "col2": val2 } # some values, id is numeric and is not None
tab.append( rec ) # fails here
My table contains various character and numeric columns. This is just an example. The exceptions is:
line 5959, in append
newrecord = Record(recnum=header.record_count, layout=meta, kamikaze=kamikaze)
line 3102, in __new__
record._update_disk()
line 3438, in _update_disk
index(self)
line 7550, in __call__
vindex = bisect_right(self._values, key)
TypeError: '<' not supported between instances of 'NoneType' and 'int'
Any help appreciated. Thanks.
EDIT: Here is testing script
import dbf
from dbf import is_deleted, DoNotIndex
tab = dbf.Table('temptable', "ID N(12,0)" )
tab.open(mode=dbf.READ_WRITE)
rc = { "id":1 }
tab.append( rc ) # need some data without index first
idx = tab.create_index(lambda rec: (rec.id if not is_deleted(rec) else DoNotIndex ) )
rc = { "id":2 }
tab.append( rc ) # fails here
So, I was working on API with flask. The data is in DB2. I tried to connect with pyodbc as below
#app.route('/api/acrdkl/all', methods=['GET'])
def api_all():
conn = pyodbc.connect("DSN=AS400;UID=....;PWD=....")
cur = conn.cursor()
all_books = cur.execute(""" select trim(dkkdcb), trim(dkkdps), trim(dkcob), trim(dkureg), trim(dkbktg), trim(dkblrg), trim(dkthrg)
from simdta.ACRDKL where dkkdcb=1402 and dkblrg=10 and dkthrg=2020""")
rows = cur.fetchall()
result = []
for dt in rows:
result.append([x for x in dt])
return jsonify(result)
Result are shown as JSON.
But when I tried to use some parameter as below
#app.route('/api/acrdkl/filter', methods=['GET'])
def api_filter():
dkkdcb = request.args.get('DKKDCB', 0)
dkblrg = request.args.get('DKBLRG', 0)
dkthrg = request.args.get('DKTHRG', 0)
query = """selecttrim(dkkdcb),trim(dkkdps),trim(dkcob),trim(dkureg),
trim(dkbktg), trim(dkblrg), trim(dkthrg)
from simdta.ACRDKL WHERE """
conn = pyodbc.connect("DSN=AS400;UID=.....;PWD=.....")
cur = conn.cursor()
rows = cur.execute(query, [int(dkkdcb), int(dkblrg), int(dkthrg)])
rows.fetchall()
print("rows 2 ", rows)
result = []
for dt in rows:
result.append([x for x in dt])
return jsonify(results)
And I go to this http://127.0.0.1:5000/api/acrdkl/filter?DKKDCB=1402&DKBLRG=10&DKTHRG=2020 and it throws error like this
pyodbc.DataError: ('22023', '[22023] [Microsoft][ODBC DB2 Driver]Data
exception - SQLSTATE 22023, SQLCODE -302. SQLSTATE: 22023, SQLCODE:
-302 (-302) (SQLExecDirectW)')
How do I get the desired result? Where is my mistake? Any help would be appreciate. Thanks
I don't see that you are accessing the request data provided by Flask, e.g.:
dkbrlg=request.args.get('dkbrlg',0)
This is a continuous of previous post .
How to get result from BigQuery based on user input parameters .
I tried to use EXECUTE IMMEDIATE and USING as these article say.
https://cloud.google.com/bigquery/docs/parameterized-queries
https://towardsdatascience.com/how-to-use-dynamic-sql-in-bigquery-8c04dcc0f0de
But when I run the sql , I got syntax error . I'd like to get my sql checked . I guess this error is caused by the line breaks but I want to do that for the readability . Sorry for my poor coding skill . Could you give me advice ??
I'm little bit worry about BigQuery doesn't support dynamic parameter in Python. Because the article above seems to use these statement in Console not in Python .
The error
File "/srv/main.py", line 14 SELECT EXISTS(SELECT 1
SyntaxError: invalid syntax
SQL
query = """EXECUTE IMMEDIATE format("""
SELECT EXISTS(SELECT 1
FROM `test-266778.conversion_log.conversion_log_2020*` as p
WHERE p.luid = #request_luid AND orderid != '' limit 1000)""")"""
USING "request_luid" as request_luid;
/home/user/api_dev/main.py
from flask import Flask, request, jsonify
from google.cloud import bigquery
app = Flask(__name__)
#app.route('/')
def get_request():
request_luid = request.args.get('luid') or ''
client = bigquery.Client()
query = """EXECUTE IMMEDIATE format("""
SELECT EXISTS(SELECT 1
FROM `test-266778.conversion_log.conversion_log_2020*` as p
WHERE p.luid = #request_luid AND orderid != '' limit 1000)""")"""
USING "request_luid" as request_luid;
job_config = bigquery.QueryJobConfig(
query_parameters=[
bigquery.ScalarQueryParameter("request_luid", "STRING", request_luid)
]
)
query_job = client.query(query, job_config=job_config)
query_res = query_job.result()
first_row = next(iter(query_job.result()))
for row in query_res:
return str(row)
#return jsonify({request_luid:query_res.total_rows})
if __name__ == "__main__":
app.run()
You can try this:
def get_request():
request_luid = request.args.get("luid") or ""
client = bigquery.Client()
query = """SELECT EXISTS(
SELECT 1
FROM `test-266778.conversion_log.conversion_log_2020*` as p
WHERE p.luid = {}
AND p.orderid is not null limit 1000)""".format(request_luid)
query_job = client.query(query)
query_res = query_job.result()
first_row = next(iter(query_job.result()))
for row in query_res:
return str(row)
Notes: If the luid is non-numeric, then use '{}'.
You can try this:
EXECUTE IMMEDIATE
"""SELECT EXISTS(SELECT 1 FROM `test-266778.conversion_log.conversion_log_2020*` WHERE luid = ? AND orderid is not null limit 1000)"""
USING
"string-value";
For numeric input value, don't use double quotes
Getting an error while making an assertion
field1 is value from DB and I want to match them with field2 (value from response xml)
* def field1 = get TABLE[*].COLUMN
Field2 is result of below operation
* def field2 = get Response SOME_XML_XPATH
it may contains single value of list of value like
field2 = 19.2 or field2 = ["188.3","281.11"]
Issue is when I try to compare both fields when they are list like
field1 = [1850, 700, 30] field2 = ["1850.0","30.0","700.0"]
To resolve this, I used below code,
* def field1 = [1850, 700, 30]
* def field2 = ["1850.0","30.0","700.0"]
* def field3 = karate.map(field2, function(x){ return ~~x })
* match field1 contains only field3
This works fine however, When field2 is single value, and I want to match it with field1, i get below error
Cannot cast java.lang.String to java.util.List
Please read the docs. There is something called contains: https://github.com/intuit/karate#match-contains
* def field1 = [1850, 700, 30]
* def field2 = "30.0"
* def field2 = ~~field2
* match field1 contains field2
When I am using raw sql for a variable say myvar = some rawsql and when I am checking in if condition in query for myvar it is always true.
{% if myvar %}
do this;
{% else %}
do this;
{% endif %}
For instance my raw sql returns 0 records then I want to display some message, but I could not able to do that. When I debugged whats happening in the background, my raw sql always returns some object (<RawQuerySet: "sel) eventhough sql fetches empty records. So thats why myvar is always true because its holding some object of raw queryset.
Is there any way to getrid of this situation
Thanks in advance
>>> Author.objects.raw("""select * from stack_author where id = 5""")
<RawQuerySet: 'select * from stack_author where id = 5'>
>>> list(_)
[]
>>>
>>> if Author.objects.raw("""select * from stack_author where id = 5"""):
... print 'yes'
...
yes
You can avoid this situation by passing a list instead of a raw queryset:
>>> if list(Author.objects.raw("""select * from stack_author where id = 5""")):
... print 'yes'
...
>>>
Slicing would also work:
>>> if Author.objects.raw("""select * from stack_author where id = 5""")[:]:
... print 'yes'
...
>>>
You could also evaluate the qs in the view and pass a boolean result to the template.
Careful that Indexing will raise an IndexError on an empty raw qs:
>>> if Author.objects.raw("""select * from stack_author where id = 5""")[0]:
... print 'yes'
...
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/dennisting/.virtualenvs/django-sb/lib/python2.7/site-packages/django/db/models/query.py", line 1379, in __getitem__
return list(self)[k]
IndexError: list index out of range
Using a for loop if you are iterating works as well depending on what you are trying to do:
>>> for author in Author.objects.raw("""select * from stack_author where id = 5"""):
... print author
...
>>>