码迷,mamicode.com
首页 > 数据库 > 详细

Python连接MySQL数据库

时间:2015-04-26 21:09:53      阅读:132      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!