Python Forum
Need help on how to include single quotes on data of variable string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help on how to include single quotes on data of variable string
#1
I am not unable to figure out why Python cannot include single quotes on to the value of the variable string. I am trying to add single quote on the beginning and at the end of the of the data variable current_partnum on the line no. 14. This variable's value will then be use to make mysql query2 using this line of code:

[inline]
query2 = "SELECT PartNum, WareHouseCode, BinNum, TranQty FROM trans WHERE PartNum = {}".format(current_partnum)
[/inline]

So my final query should be:

SELECT PartNum, WareHouseCode, BinNum, TranQty FROM trans WHERE PartNum = '10670APRCONX22XXXX'

But instead I am getting this output:

Output:
'ELECT PartNum, WareHouseCode, BinNum, TranQty FROM trans WHERE PartNum = '10670APRCONX22XXXX
Below is my code and its output

for (var_partNum) in record:

    #Start of Debug
    print("\nHMS: Debug")
    print(var_partNum[0])
    print(var_partNum)
    #End of Debug
    current_partnum = ' '.join(var_partNum)

    print("\nWorking on record "+ str(i) + " From " + str(total_records)) #Display active record number to track the progress
    print(current_partnum)
    #current_partnum = "!!{}!!".format(current_partnum)
    print(current_partnum)
    current_partnum = "'" + current_partnum + "'"   
    print(type(current_partnum))
    print(current_partnum)
    query2 = "SELECT PartNum, WareHouseCode, BinNum, TranQty FROM trans WHERE PartNum = {}".format(current_partnum)
    print(query2) 
    cursor.execute(query2,(current_partnum)) #To get the details for each FG partnum and then process the query result to save into the excel file
    record2 = cursor.fetchall()
    found_record = cursor.rowcount
     #Start of Debug
    print("\nfound_record = " + str(found_record))
    #End of Debug
Output:
Found total records: 846 HMS: Debug 10670APRCONX22XXXX ('10670APRCONX22XXXX\r',) Working on record 1 From 846 10670APRCONX22XXXX 10670APRCONX22XXXX <class 'str'> '10670APRCONX22XXXX 'ELECT PartNum, WareHouseCode, BinNum, TranQty FROM trans WHERE PartNum = '10670APRCONX22XXXX
I don't understand why the SELECT statement the character S is replaced with single quotes.
Reply


Messages In This Thread
Need help on how to include single quotes on data of variable string - by hani_hms - Jan-10-2023, 04:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 341 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  How to include one script into another? MorningWave 8 574 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  how include a python code in notpad++ plugin akbarza 2 673 Sep-25-2023, 08:25 PM
Last Post: deanhystad
  Replacing String Variable with a new String Name kevv11 2 815 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Regex Include and Exclude patterns in Same Expression starzar 2 824 May-23-2023, 09:12 AM
Last Post: Gribouillis
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,992 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  python sql query single quote in a string mg24 1 1,113 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  python r string for variable mg24 3 2,905 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 985 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,378 Aug-23-2022, 01:15 PM
Last Post: louries

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020