0

How to archive the following...

i need to pass two variable date which comes as long format and apply this variable in a subquery using raw sql django

def duplicatephoneno(request):

    cursor = connection.cursor()
    payload = json.loads(request.body.decode('utf-8'))
    print(payload)
    if payload:
        startdate=payload['from']
        enddate=payload['to']
        with connection.cursor() as cursor:
            cursor.execute(MYSQLdb.escape_string("SELECT * FROM `allergy` WHERE `allergy`.`patient_n_key` IN (SELECT `patient_masters`.`patient_n_key` FROM `patient_masters` WHERE `patient_masters`.`created_on`between %s AND %s)",[startdate], [enddate]))
            row = dictfetchall(cursor)
            patientuser=serializers.serialize('json', row)
        return HttpResponse(patientuser, content_type='application/json;charset=utf8')
3
  • So startdate and enddate are strings? Can you give an example of the format? Commented Sep 5, 2018 at 11:52
  • Tq Mr will keeling.Yes I need to give string only Ex.Varchar field.where am i going wrong. Commented Nov 27, 2018 at 10:45
  • Hi Mr pandelis i have run the above code which you edited but I am getting error MysqlDB is not defined but i have imported in my local but again i am getting the same error. Commented Nov 27, 2018 at 15:58

3 Answers 3

2

Try this

def duplicatephoneno(request):
    payload = json.loads(request.body.decode('utf-8'))
    print(payload)

    if payload:
        startdate=payload['from']
        enddate=payload['to']
        with connection.cursor() as cursor:

            queries="SELECT * FROM `allergy` WHERE `allergy`.`patient_n_key` IN (SELECT `patient_masters`.`patient_n_key` FROM `patient_masters` WHERE `patient_masters`.`created_on`between %s AND %s)"
            data_tuple=(startdate,enddate)
            cursor.execute(queries,data_tuple)
            connection.commit()
            row = cursor.fetchall()
            patientuser=serializers.serialize('json', row)
            return HttpResponse(patientuser, content_type='application/json;charset=utf8')
Sign up to request clarification or add additional context in comments.

Comments

0

The docs mention how to do this. Note you will not be able to use this method if you are using an SQLite database.

As a side note, using raw queries should be your last resort. The Django ORM is pretty good these days and you should really only use raw queries if there is a type of query that is unsupported by the ORM (rare) or the ORM generated querystring has a significant performance cost compared to your custom raw query. Querysets (the objects returned by ORM queries) are also pretty easy to turn into JSON like you seem to be doing here.

Comments

0
def duplicatephoneno(request):

    cursor = connection.cursor()
    payload = json.loads(request.body.decode('utf-8'))
    print(payload)
    if payload:
        startdate=payload['from']
        enddate=payload['to']
        with connection.cursor() as cursor:
            cursor.execute(MYSQLdb.escape_string("SELECT * FROM `allergy` WHERE `allergy`.`patient_n_key` IN (SELECT `patient_masters`.`patient_n_key` FROM `patient_masters` WHERE `patient_masters`.`created_on`between %s AND %s)",[startdate, enddate]))
            row = dictfetchall(cursor)
            patientuser=serializers.serialize('json', row)
        return HttpResponse(patientuser, content_type='application/json;charset=utf8')

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.