AddressBook in python

Hi to all…here is the code for address book in python


#!usr/bin/python

import sys
import MySQLdb
conn=MySQLdb.connect(host="localhost",user="root",passwd="password",db="balaji")
cursor=conn.cursor()
cursor.execute("create table if not exists users (name varchar(20),address varchar(50),email varchar(30),mobile varchar(12))")

def printt():
ch=int (raw_input("1.Add New 2.Search 3.Exit \n Enter Your Choice"))
while(ch!=4):

if(ch==1):
name=raw_input("Enter a Name")
address=raw_input("Enter address")
email=raw_input("enter Email address")
mobile=raw_input("enter mobile no.")
cursor.execute("""insert into users (name,address,email,mobile) values (%s,%s,%s,%s)""",(name,address,email,mobile))
option=raw_input("Add Another Record y/n")
if(option=='y'):
ch=1
else:
printt()

if(ch==2):
name1=raw_input("Enter a name to Search")
cursor.execute("""select * from users where name=(%s)""",(name1))
result=cursor.fetchall()
for row in result:
name=row[0]
address=row[1]
email=row[2]
mobile=row[3]
print " name=%s \n address=%s \n email=%s \n mobile=%s" % (name,address,email,mobile)
option=raw_input("Search Another Name (y/n)")
if(option=='y'):
ch=2
else:
printt()
if(ch==3):
sys.exit()

printt()

Advertisement

To generate random characters

Hi..today i learnt how to generate  the ranom charecters in python..

As per the ascii type starts with range 32 to 126. use any variable for iteration..

>>> for i in xrange(32, 127):
…    print chr(i),
...
  ! ” # $ % & ‘ ( ) * + , – . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~

if you don’t want space means start at 33.

 

string conversion

Hi..today i learnt how to convert string to code.instead for getting input from user everytime and placing into a variable...follow this... (e.x) a to z or 1 to 26 variables..

>>> vars = {}
>>> for x in xrange(26):
...  vars[x] = ''
...
>>> for x in xrange(26):
...  vars[x] = raw_input("What is %s? " % x)
...
>>> print vars

{0: ‘a’, 1: ‘b’, 2: ‘c’, 3: ‘d’, 4: ‘e’, 5: ‘f’, 6: ‘g’, 7: ‘h’, 8: ‘i’, 9: ‘j’, 10: ‘k’, 11: ‘l’, 12: ‘m’, 13: ‘n’, 14: ‘o’, 15: ‘p’, 16: ‘q’, 17: ‘r’, 18: ‘s’, 19: ‘t’, 20: ‘u’, 21: ‘v’, 22: ‘w’, 23: ‘x’, 24: ‘y’, 25: ‘z’}
>>>