如何在python3中为cursor.execute制作灵活的参数列表?

问题描述 投票:0回答:1

其实我想根据变量值的状态来增减mysql的查询项和值(参数)

根据我的场景,我需要使用其参数创建一个灵活的 mysql 查询。

    connection = pymysql.connect(host="", user="", passwd="", db="")
    myquery = connection.cursor()

    insARGS = ""
    content_id = 1097
    page_title = "test page title"

    insQuery = 'UPDATE ex_content SET'
    insARGS = ""
    insARGSdictionary = {}

    if (len(page_title) > 0):
        print("page_title: "+page_title)
        print(type(page_title))
        pageTitleQuery = " c_title=%s"
        insQuery = insQuery + pageTitleQuery
        insARGS = "page_title"
        insARGSdictionary['page_title'] = page_title
    else:
        print("page_title is not bigger than 0")

    if (len(page_desc) > 0):
        print("page_desc: "+page_desc)
        pageDescQuery = ", c_meta_desc=%s"
        insQuery = insQuery + pageDescQuery
        insARGS = insARGS + ", page_desc"
        insARGSdictionary['page_desc'] = page_desc
    else:
        print("page_desc is not bigger than 0")
    if (len(page_heading) > 0):
        page_heading = ""
        print("page_heading: "+page_heading)
        pageHeadingQuery = ", c_heading=%s"
        insQuery = insQuery + pageHeadingQuery
        insARGS = insARGS + ", page_heading"
        insARGSdictionary['page_heading'] = page_heading
    else:
        print("page_heading is not bigger than 0")
    if (len(page_link) > 0):
        print("page_link: "+page_link)
        pageLinkQuery = ", c_link=%s"
        insQuery = insQuery + pageLinkQuery
        insARGS = insARGS + ", page_link"
        insARGSdictionary['page_link'] = page_link
    else:
        print("page_link is not bigger than 0")
    
    if content_id:
        print("content_id: "+str(content_id))
        contentIdQuery = ' WHERE c_id=%s '
        insQuery = insQuery + contentIdQuery
        insARGS = insARGS + ", content_id"
        insARGSdictionary['content_id'] = content_id

        list_of_dict_keys = list(insARGSdictionary.keys())
        queryArguments = (', '.join(map(str, list_of_dict_keys)))
        myquery.execute(insQuery, (queryArguments))

此外,queryArguments 中的数据类型可能有所不同(例如 int 或 str 等)。 先谢谢您的建议

python list dictionary mysql-python string-concatenation
1个回答
0
投票

myquery.execute()
的第二个参数应该是值,而不是键。您甚至不需要字典,只需使用在添加到查询时附加的列表即可。

connection = pymysql.connect(host="", user="", passwd="", db="")
myquery = connection.cursor()

content_id = 1097
page_title = "test page title"

insQuery = 'UPDATE ex_content SET'
insARGSlist = []

if (len(page_title) > 0):
    print("page_title: "+page_title)
    print(type(page_title))
    pageTitleQuery = " c_title=%s"
    insQuery = insQuery + pageTitleQuery
    insARGSlist.append(page_title)
else:
    print("page_title is not bigger than 0")

if (len(page_desc) > 0):
    print("page_desc: "+page_desc)
    pageDescQuery = ", c_meta_desc=%s"
    insQuery = insQuery + pageDescQuery
    insARGSlist.append(page_desc)
else:
    print("page_desc is not bigger than 0")
if (len(page_heading) > 0):
    page_heading = ""
    print("page_heading: "+page_heading)
    pageHeadingQuery = ", c_heading=%s"
    insQuery = insQuery + pageHeadingQuery
    insARGSlist.append(page_heading)
else:
    print("page_heading is not bigger than 0")
if (len(page_link) > 0):
    print("page_link: "+page_link)
    pageLinkQuery = ", c_link=%s"
    insQuery = insQuery + pageLinkQuery
    insARGSlist.append(page_link)
else:
    print("page_link is not bigger than 0")

if content_id:
    print("content_id: "+str(content_id))
    contentIdQuery = ' WHERE c_id=%s '
    insQuery = insQuery + contentIdQuery
    insARGSlist.append(content_id)

    myquery.execute(insQuery, insARGSlist)
© www.soinside.com 2019 - 2024. All rights reserved.