[Tutor] MySQLdb simple menu

David david at abbottdavid.com
Sun Aug 17 04:48:54 CEST 2008


David wrote:
> New to python and programming. This works but I don't like the way it 
> is set up. I would like to have a simple menu like;
> [code]
> def options():
>    print \
>    """ Options:
>        'p' Print Options
>        'l' Login
>        'c' Create account
>        'q' Quit
>    """
>
> options()
>
> def choice():
>    choice = "p"
>    while choice != "q":
>    choice = raw_input("Options: ")
>        if choice == "p":
>        print options()
>
>        if choice == "l":
>        login()
>
>            if choice == "c":
>                newlogin()
>
>            if choice == "q":
>                sys.exit(0)
>
>            else:
>               print "That option does not exsist!"
>               choice = raw_input("Option: ")
> choice()
> [/code]
>
> But I can't get it to work, I can't get past;
> [code]
> for row in rows:
> [/code]
>
>
> Works but I don't like that you have to try to login first, would like 
> it to give you the option to;
>
> login
> create account
> print menu
> exit
>
> [code]
> import sys
> import MySQLdb
> import MySQLdb.cursors
>
> conn = MySQLdb.Connect(
>        host='localhost', user='root',
>        passwd='atlantic', db='python', compress=1,
>        cursorclass=MySQLdb.cursors.DictCursor)
>
> cursor = conn.cursor()
> cursor.execute("SELECT * FROM login")
> rows = cursor.fetchall()
> cursor.close()
> conn.close()
> username = raw_input("Enter your username (q to exit): ")
> password = raw_input("Enter your password (q to exit): ")
>
> for row in rows:
>
>    if username == row['username'] and password == row['password']:
>        print "Hello", username
>        sys.exit(0)
>
> else:
>    r = raw_input("Login failed, do you want to create an account? [y/n]")
>    if r != 'y' : sys.exit(0)
>
> newname = ""
> data = []
> newname = raw_input("Please enter a username: ")
> newpw = raw_input("Please enter a password: ")
> tuple = (newname, newpw)
> data.append(tuple)
> db = MySQLdb.connect(
>        host="localhost",
>        user="root",
>        passwd="atlantic",
>        db="python")
> cursor = db.cursor()
>
> cursor.executemany(
>        "INSERT INTO login (username, password)VALUES (%s, %s)", data)
> [/code]
>
How does this look;

#!/usr/bin/python
#Filename : choices.py

import sys
import MySQLdb
import MySQLdb.cursors

def login():
    conn = MySQLdb.Connect(
            host='localhost', user='root',
            passwd='atlantic', db='python', compress=1,
            cursorclass=MySQLdb.cursors.DictCursor)

    cursor = conn.cursor()
    cursor.execute("SELECT * FROM login")
    rows = cursor.fetchall()
    cursor.close()
    conn.close()
    username = raw_input("Enter your username (q to exit): ")
    password = raw_input("Enter your password (q to exit): ")

    for row in rows:

            if username == row['username'] and password == row['password']:
                print "Hello", username
                sys.exit(0)
    print "Login failed!"
def newlogin():
    newname = ""
    data = []
    newname = raw_input("Please enter a username: ")
    newpw = raw_input("Please enter a password: ")
    tuple = (newname, newpw)
    data.append(tuple)
    db = MySQLdb.connect(
            host="localhost",
            user="root",
            passwd="atlantic",
            db="python")
    cursor = db.cursor()

    cursor.executemany(
            "INSERT INTO login (username, password)VALUES (%s, %s)", data)
    print "Account created for", newname
    sys.exit(0)


def options():
    print \
"""
     Please pick one:

 'p' Print Options
 'l' Login
 'c' Create account
 'q' Quit
"""
options()
choice = "p"
while choice != "q":
    choice = raw_input("Please pick an option: ")
    if choice == "p":
        options()

    elif choice == "l":
        login()

    elif choice == "c":
        newlogin()

    elif choice == "q":
        print "Goodbye!"

seems to work OK.

-- 
Have Fun,
David A.

Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com



More information about the Tutor mailing list