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

4dc9hkyq  于 12个月前  发布在  SQLite
关注(0)|答案(1)|浏览(153)

有人能帮我解下密码吗!因此,使用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
yiytaume

yiytaume1#

这里有两种可能的情况。一个是你试图看看登录的(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()

相关问题