【问题标题】:i need to add try in my python script but not sure how to go about it我需要在我的 python 脚本中添加尝试,但不知道如何去做
【发布时间】:2021-01-20 22:59:05
【问题描述】:

我需要在我的脚本中添加尝试,以便当输入为空白或错误​​时,它会在不显示空白表格但不知道如何操作的情况下这么说,将不胜感激

import mysql.connector
from prettytable import PrettyTable
username = input('what user are you looking for?: ')
bd = mysql.connector.connect(host="localhost", user="root", password="", database="comptes_linux")
cursor = bd.cursor()
cursor.execute("SELECT * FROM users WHERE user LIKE %s", [username])
resultat = cursor.fetchall()
x = PrettyTable()
x.field_names = ["User","Filler","Uid","Gid","Gecos","Home","Login"]
x.align["User"]  = "l"
x.align["Gecos"] = "l"
x.align["Home"]  = "l"
x.align["Login"] = "l"
x.align["Uid"]   = "r"
x.align["Gid"]   = "r"
x.add_rows(resultat)
print(x)
cursor.close()
bd.close()

【问题讨论】:

    标签: python mysql try-catch


    【解决方案1】:

    我输入了一个 if 语句,还输入了“try:..except”以防止进一步的错误:

    import mysql.connector
    from prettytable import PrettyTable
    username = input('what user are you looking for?: ')
    if not username:
        print("Please enter a valid username")
    
    else:    
        try:
            bd = mysql.connector.connect(host="localhost", user="root", password="", database="comptes_linux")
            cursor = bd.cursor()
            cursor.execute("SELECT * FROM users WHERE user LIKE %s", [username])
            resultat = cursor.fetchall()
            x = PrettyTable()
            x.field_names = ["User","Filler","Uid","Gid","Gecos","Home","Login"]
            x.align["User"]  = "l"
            x.align["Gecos"] = "l"
            x.align["Home"]  = "l"
            x.align["Login"] = "l"
            x.align["Uid"]   = "r"
            x.align["Gid"]   = "r"
            x.add_rows(resultat)
            print(x)
            cursor.close()
            bd.close()
        except:
            print("Couldn't find ",username)
    

    “If not username”应该检测到空输入,如图here

    【讨论】:

    • 这无济于事,因为空输入会产生错误
    • @nbk 我现在已经更改了它。这希望现在可以工作,尽管很难回答,因为我只有用户提供的代码。
    【解决方案2】:

    只有在您输入任何用户名时才会继续。

    它还会检测错误并向您显示错误

    import mysql.connector
    from prettytable import PrettyTable
    try:
      username = input('what user are you looking for?: ')
    except SyntaxError:
      username = None
      print(username)
    if username is not None and username != '':
      try:
        bd = mysql.connector.connect(host="localhost", user="root", password="", database="comptes_linux")
        cursor = bd.cursor()
        rows_count = cursor.execute("SELECT * FROM users WHERE user LIKE %s", [username])
        if rows_count > 0:
          resultat = cursor.fetchall()
          x = PrettyTable()
          x.field_names = ["User","Filler","Uid","Gid","Gecos","Home","Login"]
          x.align["User"]  = "l"
          x.align["Gecos"] = "l"
          x.align["Home"]  = "l"
          x.align["Login"] = "l"
          x.align["Uid"]   = "r"
          x.align["Gid"]   = "r"
          x.add_rows(resultat)
          print(x)
          cursor.close()
          bd.close()
        else:
          print("username not found")
      except Exception as e:
        print("error: " + str(e))
    else:
      print("username is empty")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      相关资源
      最近更新 更多