首页 > 数据库技术 > 详细

Python连接MySQL数据库

时间:2015-04-26 21:07:36      阅读:247      评论:0      收藏:0      [点我收藏+]

一、配置好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()

 

Python连接MySQL数据库

原文:http://www.cnblogs.com/shine009/p/4458307.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!