一、配置好yum源之后安装MySQL、MySQLdb、mysql-devel。

yum -y install mysql-server MySQL-python mysql-devel

安装完成后在python环境下执行如下命令,没有输出则证明安装成功。

import MySQLdb

 

二、编写一个模拟用户登录的程序进一步加强学习。

代码思路:

从输入设备接收用户名和密码然后去库中查询,如果能匹配到则返回True允许登录,如果无法匹配则返回False拒绝登录。

#/usr/bin/env python
import MySQLdb

#mysql配置文件 dbconfig = { 'user': 'root', 'host': 'localhost', 'passwd': '', 'db': 'zhonggu', 'charset': 'utf8', } conn = MySQLdb.connect(**dbconfig) #连接mysql cursor = conn.cursor() def user_login(name,password): sql = "select * from user where name=%s and password=%s" #查询的SQL语句 if cursor.execute(sql,(name, password)): return True else: return False name = raw_input("Input your name: ") password = raw_input("Input your password: ") print user_login(name, password) cursor.close() conn.close()

 

posted on 2015-04-26 20:38  漫漫运维路  阅读(187)  评论(0)    收藏  举报