无法使用pymsql

时间:2019-02-23 18:25:27

标签: mysql database python-3.x pymsql

我一直在尝试从数据库中检索数据。但是,这次我在if语句中成功。代码如下:

cur_msql = conn_mysql.cursor(cursor=pymysql.cursors.DictCursor)
select_query = """select x,y,z from table where type='sample' and code=%s"""
cur_msql.execute(select_query, code)
result2 = cur_msql.fetchone()
if(result2==None):
    insert_func(code)
    select_query = f"""select x,y,z from table where type='sample' and code='{code}'"""
    mycur = conn_mysql.cursor(cursor=pymysql.cursors.DictCursor)
    print(select_query)
    mycur.execute(select_query)
    result3 = mycur.fetchone()
    if(result2==None):
         result2=result3

现在,我看到insert_func确实已成功插入到“表”中。但是,在尝试获取该行时,在插入后立即返回None,就好像该行不存在一样。在调试时,我发现result3也是None。在我看来,什么都没错,但没有用。

1 个答案:

答案 0 :(得分:0)

您没有以正确的方式执行它,在cur_msql.execute中,您将发送查询和值的元组,而仅发送一个值:

cur_msql = conn_mysql.cursor(cursor=pymysql.cursors.DictCursor)
select_query = "select learnpath_code,learnpath_id,learnpath_name from contentgrail.knowledge_vectors_test where Type='chapters' and code=%s"
cur_msql.execute(select_query, (meta['chapter_code'],))
result2 = cur_msql.fetchone()