将表从数据库文件连接到 python

问题描述

有人可以帮我处理那个代码吗!因此,使用 if 子句,我想让程序仅在我使用 admin 登录时打开操作员菜单。通过这种方式的代码,这是一个错误:未定义管理员。我该如何解决这个问题

def operator_menu():
    print("Operator Menu")
    print("1. Show all bookings")
    print("2. Register a new driver")
    print("3. Remove a booking")
    print("4. View available drivers")
    print("5. logout")

    menu_input = input("What would you like to do?: ")
    if menu_input == "1":
        show_bookings()
    if menu_input == "2":
        register_newdriver()
    if menu_input == "3":
        remove_booking()
    if menu_input == "4":
        available_drivers()
    if menu_input == "5":
        exit()
      


def show_bookings():
    if not(current_user == create_company(conn,administrator)):
        bookings = get_trips(conn)
        if not bookings:
            print("No bookings.")
        else:
            print("Available bookings:")
            for i,booking in enumerate(bookings):
                print(str(i)+". "+str(booking[4])+" -> "+str(booking[5]) + " -> "+str(booking[6])+ " - "+str(booking[7]))

        input("Press any key to continue...")
        operator_menu()
    else:
        driver_menu()

def create_company(conn,administrator):
    sql = ''' INSERT INTO administrator(name)
              VALUES(?) '''
    cur = conn.cursor()
    cur.execute(sql,administrator)
    conn.commit()
    return cur.lastrowid

解决方法

这里有两种可能的情况。一是您正在尝试查看登录(Windows)用户是否是管理员,二是您正在创建一个具有登录窗口的应用程序,因此有两种用户:应用程序管理员和其他用户。

如果是第一种情况,那么您可以使用如下子流程模块

import subprocess
current_user = subprocess.check_output("if %errorlevel% equ 0 (echo admin) else (echo limited)",shell=True).decode("utf-8").strip()
if current_user == 'admin':
    operator_menu()
else:
    user_menu()

如果是第二种情况,那么我认为这段代码可以帮助您并且易于理解。您的数据库中必须有一个以用户名命名的表,用于存储用户信息,包括确定登录用户权限的列。那么您应该从现有的表中检索用户信息,即用户。如果你没有创建这个表,这里是代码:

import sqlite3
conn = sqlite3.connect("example.db")
cur = conn.cursor()
sql = "create table if not exists users (username text,password text,privilages bool)"
cur.execute(sql)
conn.commit()
conn.close()

那么你应该从这个表中检索用户信息,看看用户是否有权限。

import sqlite3

def user_menu():
    pass#put some code here

def show_bookings():
    pass#put some code here

def register_newdriver():
    pass#put some code here

def remove_booking():
    pass#put some code here

def available_drivers():
    pass#put some code here

def operator_menu():
    while True:
        print("Operator Menu:")
        print(" 1. Show all bookings")
        print(" 2. Register a new driver")
        print(" 3. Remove a booking")
        print(" 4. View available drivers")
        print(" 5. Logout")

        menu_input = input("What would you like to do?: ")
        if menu_input == "1":
            show_bookings()
        elif menu_input == "2":
            register_newdriver()
        elif menu_input == "3":
            remove_booking()
        elif menu_input == "4":
            available_drivers()
        elif menu_input == "5":
            quit()
        else:
            print("invalid choice: ",menu_input)

# define some code that get username from login form. for example
username = miroslav
sql = '"select * from users where username="' + username + '"'
cur.execute(sql)
user_info = cur.fetchall()[0]
if user_info[3] == True:
    operator_menu()
else:
    user_menu()