SQL SELECT语句不起作用?

时间:2016-04-04 06:53:47

标签: python sql select

def showallres():
    sql = '''SELECT ResidentID,FirstName,SurName,Age,MDisability,History,Impairment,Money,Contact
    FROM tblResidentM'''
    results = run_sql2(sql)
    print(results)
    return results

由于某种原因它只打印'无'?但它以前工作过。表和所有字段都正确命名,所以我不确定它是什么。

以下是'run_sql2'的代码

def run_sql2(sql):
    db = db_connect()
    c = db.cursor() 
    c.execute(sql)
    results=c.fetchall()
    db.commit()
    c.close()
    db.close()

我正在连接到一个在线mysql数据库。

db_connect如下

def db_connect():
    try:
        db = mysql.connector.connect(user = 'user', password = 'pass', host = 'host', database = 'db', port = 'port')
        print('connected')
        return db
    except mysql.connector.Error as error:
        print(error)

1 个答案:

答案 0 :(得分:2)

你的函数run_sql2()没有返回任何内容, 在Python中,函数默认返回None,这就是results为None

的原因
def run_sql2(sql):
    db = db_connect()
    c = db.cursor() 
    c.execute(sql)
    results = c.fetchall()
    db.commit()
    c.close()
    db.close()
    return results  # <---- you must return the results