【问题标题】:Python Mysql Queary not returning resultsPython Mysql 查询不返回结果
【发布时间】:2023-02-09 00:26:29
【问题描述】:

我有一个数据库,当我查询一个表时,我得到 67 个结果。 SQL 是:

SELECT lower(UserName) from Participants ;

我尝试连接到数据库,但没有出现连接错误。

def db_connect ():                                                                                                    
    try:                                                                                                              
        cnx = mysql.connector.connect(user=db_user, password=db_password,host=db_host,database=db_name)               
        return cnx                                                                                                    
    except mysql.connector.Error as err:                                                                              
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:                                                             
            print("Something is wrong with your user name or password")                                               
        elif err.errno == errorcode.ER_BAD_DB_ERROR:                                                                  
            print("Database does not exist")                                                                          
        else:                                                                                                         
            print(err)  

                                                                                                                      
def main():                                                                                                           
    cnx = db_connect()                                                                                                
    cursor = cnx.cursor()                                                                                             
    query = ("SELECT lower(UserName) from Participants ;")                                                            
    cursor.execute(query)                                                                                             
    print(cursor.rowcount)    

它为行数打印出 -1。与数据库的连接似乎正常工作,SQL 是一个简单的查询...

【问题讨论】:

    标签: python mysql sql mysql-connector


    【解决方案1】:
    • 尝试在 print(cursor.rowcount) 之前添加 cursor.fetchall()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2015-01-29
      • 2017-05-18
      • 2023-03-08
      • 1970-01-01
      • 2013-07-05
      • 2015-06-11
      相关资源
      最近更新 更多