Django : How to use LIKE while fetching data from mysql DB










0















I need a way to fetch similar records from DB using LIKE from Django .
My code is :



def fetchProductsDate1(request):
query = request.session.get('query')
date1 = request.session.get('date1')
db = pymysql.connect(host=host,user=user,passwd=passwd,db=dbName)
# Create a Cursor object to execute queries.
cur = db.cursor()
# Select data from table using SQL query.
stmt = "SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)
log.info(stmt)
cur.execute(stmt)
rows = cur.fetchall()
json_data = rows[0][0]


The db statement looks like:



SELECT num FROM tab WHERE query LIKE 'soch sarees' AND DATE(updated_at) LIKE '2018-11-14'


I want the statement to be like :



SELECT num FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'


Any help will be really nice :)



Thanks.










share|improve this question



















  • 2





    You shouldn't be using string formatting for inserting parameters; this is open to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:40











  • cur.execute("SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s'", ('%' + query.replace("'", r"'") + '%', '%' + date1 + '%')). You may need to drop the quotation marks around %s, in the query string, not able to test.

    – roganjosh
    Nov 15 '18 at 5:44












  • I swear, the mysql library choosing %s rather than ? is about as gun-to-foot as things can get. The misunderstanding writes itself; the %s in the context of a query string here is not the same as a regular %s in string interpolation.

    – roganjosh
    Nov 15 '18 at 5:54















0















I need a way to fetch similar records from DB using LIKE from Django .
My code is :



def fetchProductsDate1(request):
query = request.session.get('query')
date1 = request.session.get('date1')
db = pymysql.connect(host=host,user=user,passwd=passwd,db=dbName)
# Create a Cursor object to execute queries.
cur = db.cursor()
# Select data from table using SQL query.
stmt = "SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)
log.info(stmt)
cur.execute(stmt)
rows = cur.fetchall()
json_data = rows[0][0]


The db statement looks like:



SELECT num FROM tab WHERE query LIKE 'soch sarees' AND DATE(updated_at) LIKE '2018-11-14'


I want the statement to be like :



SELECT num FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'


Any help will be really nice :)



Thanks.










share|improve this question



















  • 2





    You shouldn't be using string formatting for inserting parameters; this is open to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:40











  • cur.execute("SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s'", ('%' + query.replace("'", r"'") + '%', '%' + date1 + '%')). You may need to drop the quotation marks around %s, in the query string, not able to test.

    – roganjosh
    Nov 15 '18 at 5:44












  • I swear, the mysql library choosing %s rather than ? is about as gun-to-foot as things can get. The misunderstanding writes itself; the %s in the context of a query string here is not the same as a regular %s in string interpolation.

    – roganjosh
    Nov 15 '18 at 5:54













0












0








0








I need a way to fetch similar records from DB using LIKE from Django .
My code is :



def fetchProductsDate1(request):
query = request.session.get('query')
date1 = request.session.get('date1')
db = pymysql.connect(host=host,user=user,passwd=passwd,db=dbName)
# Create a Cursor object to execute queries.
cur = db.cursor()
# Select data from table using SQL query.
stmt = "SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)
log.info(stmt)
cur.execute(stmt)
rows = cur.fetchall()
json_data = rows[0][0]


The db statement looks like:



SELECT num FROM tab WHERE query LIKE 'soch sarees' AND DATE(updated_at) LIKE '2018-11-14'


I want the statement to be like :



SELECT num FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'


Any help will be really nice :)



Thanks.










share|improve this question
















I need a way to fetch similar records from DB using LIKE from Django .
My code is :



def fetchProductsDate1(request):
query = request.session.get('query')
date1 = request.session.get('date1')
db = pymysql.connect(host=host,user=user,passwd=passwd,db=dbName)
# Create a Cursor object to execute queries.
cur = db.cursor()
# Select data from table using SQL query.
stmt = "SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)
log.info(stmt)
cur.execute(stmt)
rows = cur.fetchall()
json_data = rows[0][0]


The db statement looks like:



SELECT num FROM tab WHERE query LIKE 'soch sarees' AND DATE(updated_at) LIKE '2018-11-14'


I want the statement to be like :



SELECT num FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'


Any help will be really nice :)



Thanks.







python mysql django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:45









petezurich

3,76581936




3,76581936










asked Nov 15 '18 at 5:38









ArchieArchie

244




244







  • 2





    You shouldn't be using string formatting for inserting parameters; this is open to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:40











  • cur.execute("SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s'", ('%' + query.replace("'", r"'") + '%', '%' + date1 + '%')). You may need to drop the quotation marks around %s, in the query string, not able to test.

    – roganjosh
    Nov 15 '18 at 5:44












  • I swear, the mysql library choosing %s rather than ? is about as gun-to-foot as things can get. The misunderstanding writes itself; the %s in the context of a query string here is not the same as a regular %s in string interpolation.

    – roganjosh
    Nov 15 '18 at 5:54












  • 2





    You shouldn't be using string formatting for inserting parameters; this is open to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:40











  • cur.execute("SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s'", ('%' + query.replace("'", r"'") + '%', '%' + date1 + '%')). You may need to drop the quotation marks around %s, in the query string, not able to test.

    – roganjosh
    Nov 15 '18 at 5:44












  • I swear, the mysql library choosing %s rather than ? is about as gun-to-foot as things can get. The misunderstanding writes itself; the %s in the context of a query string here is not the same as a regular %s in string interpolation.

    – roganjosh
    Nov 15 '18 at 5:54







2




2





You shouldn't be using string formatting for inserting parameters; this is open to SQL injection.

– roganjosh
Nov 15 '18 at 5:40





You shouldn't be using string formatting for inserting parameters; this is open to SQL injection.

– roganjosh
Nov 15 '18 at 5:40













cur.execute("SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s'", ('%' + query.replace("'", r"'") + '%', '%' + date1 + '%')). You may need to drop the quotation marks around %s, in the query string, not able to test.

– roganjosh
Nov 15 '18 at 5:44






cur.execute("SELECT FSN FROM tab WHERE query LIKE '%s' AND DATE(updated_at) LIKE '%s'", ('%' + query.replace("'", r"'") + '%', '%' + date1 + '%')). You may need to drop the quotation marks around %s, in the query string, not able to test.

– roganjosh
Nov 15 '18 at 5:44














I swear, the mysql library choosing %s rather than ? is about as gun-to-foot as things can get. The misunderstanding writes itself; the %s in the context of a query string here is not the same as a regular %s in string interpolation.

– roganjosh
Nov 15 '18 at 5:54





I swear, the mysql library choosing %s rather than ? is about as gun-to-foot as things can get. The misunderstanding writes itself; the %s in the context of a query string here is not the same as a regular %s in string interpolation.

– roganjosh
Nov 15 '18 at 5:54












1 Answer
1






active

oldest

votes


















0














You can use %% to do that



stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)


Which will give us




SELECT FSN FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'




Also, I think it' a bad practice to concat parameters like this.This opens path to SQL Injection



You could try:



select_stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%(name)%%%' AND DATE(updated_at) LIKE '%(date)%'"
cursor.execute(select_stmt, 'name': 'soch sarees','date':'2018-11-14' )





share|improve this answer




















  • 1





    No, please don't. This is vulnerable to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:51











  • stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)

    – Archie
    Nov 15 '18 at 5:54











  • Please see my comments under the question

    – roganjosh
    Nov 15 '18 at 5:54











  • @roganjosh yep. It is.

    – Madhan Varadhodiyil
    Nov 15 '18 at 6:00











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313070%2fdjango-how-to-use-like-while-fetching-data-from-mysql-db%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use %% to do that



stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)


Which will give us




SELECT FSN FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'




Also, I think it' a bad practice to concat parameters like this.This opens path to SQL Injection



You could try:



select_stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%(name)%%%' AND DATE(updated_at) LIKE '%(date)%'"
cursor.execute(select_stmt, 'name': 'soch sarees','date':'2018-11-14' )





share|improve this answer




















  • 1





    No, please don't. This is vulnerable to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:51











  • stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)

    – Archie
    Nov 15 '18 at 5:54











  • Please see my comments under the question

    – roganjosh
    Nov 15 '18 at 5:54











  • @roganjosh yep. It is.

    – Madhan Varadhodiyil
    Nov 15 '18 at 6:00















0














You can use %% to do that



stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)


Which will give us




SELECT FSN FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'




Also, I think it' a bad practice to concat parameters like this.This opens path to SQL Injection



You could try:



select_stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%(name)%%%' AND DATE(updated_at) LIKE '%(date)%'"
cursor.execute(select_stmt, 'name': 'soch sarees','date':'2018-11-14' )





share|improve this answer




















  • 1





    No, please don't. This is vulnerable to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:51











  • stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)

    – Archie
    Nov 15 '18 at 5:54











  • Please see my comments under the question

    – roganjosh
    Nov 15 '18 at 5:54











  • @roganjosh yep. It is.

    – Madhan Varadhodiyil
    Nov 15 '18 at 6:00













0












0








0







You can use %% to do that



stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)


Which will give us




SELECT FSN FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'




Also, I think it' a bad practice to concat parameters like this.This opens path to SQL Injection



You could try:



select_stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%(name)%%%' AND DATE(updated_at) LIKE '%(date)%'"
cursor.execute(select_stmt, 'name': 'soch sarees','date':'2018-11-14' )





share|improve this answer















You can use %% to do that



stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)


Which will give us




SELECT FSN FROM tab WHERE query LIKE '%soch sarees%' AND DATE(updated_at) LIKE '2018-11-14'




Also, I think it' a bad practice to concat parameters like this.This opens path to SQL Injection



You could try:



select_stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%(name)%%%' AND DATE(updated_at) LIKE '%(date)%'"
cursor.execute(select_stmt, 'name': 'soch sarees','date':'2018-11-14' )






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 5:57

























answered Nov 15 '18 at 5:51









Madhan VaradhodiyilMadhan Varadhodiyil

1,3521614




1,3521614







  • 1





    No, please don't. This is vulnerable to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:51











  • stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)

    – Archie
    Nov 15 '18 at 5:54











  • Please see my comments under the question

    – roganjosh
    Nov 15 '18 at 5:54











  • @roganjosh yep. It is.

    – Madhan Varadhodiyil
    Nov 15 '18 at 6:00












  • 1





    No, please don't. This is vulnerable to SQL injection.

    – roganjosh
    Nov 15 '18 at 5:51











  • stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)

    – Archie
    Nov 15 '18 at 5:54











  • Please see my comments under the question

    – roganjosh
    Nov 15 '18 at 5:54











  • @roganjosh yep. It is.

    – Madhan Varadhodiyil
    Nov 15 '18 at 6:00







1




1





No, please don't. This is vulnerable to SQL injection.

– roganjosh
Nov 15 '18 at 5:51





No, please don't. This is vulnerable to SQL injection.

– roganjosh
Nov 15 '18 at 5:51













stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)

– Archie
Nov 15 '18 at 5:54





stmt = "SELECT FSN FROM tab WHERE query LIKE '%%%s%%%' AND DATE(updated_at) LIKE '%s' " % (query.replace("'", r"'"), date1)

– Archie
Nov 15 '18 at 5:54













Please see my comments under the question

– roganjosh
Nov 15 '18 at 5:54





Please see my comments under the question

– roganjosh
Nov 15 '18 at 5:54













@roganjosh yep. It is.

– Madhan Varadhodiyil
Nov 15 '18 at 6:00





@roganjosh yep. It is.

– Madhan Varadhodiyil
Nov 15 '18 at 6:00



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313070%2fdjango-how-to-use-like-while-fetching-data-from-mysql-db%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo