MysQL使用一与Python交互
与python交互
- 在熟练使用sql语句的基础上,开始使用python语言提供的模块与mysql进行交互
- 这是我们在工作中大事要做的事
- 先学会sql是基础,一定要熟练编写sql语句
安装引入模块
- 安装mysql模块
sudo apt-get install python-mysql
- 在文件中引入模块
import Mysqldb
Connection对象
- 用于建立与数据库的连接
- 创建对象:调用connect()方法
conn=connect(参数列表)
- 参数host:连接的mysql主机,如果本机是'localhost'
- 参数port:连接的mysql主机的端口,默认是3306
- 参数db:数据库的名称
- 参数user:连接的用户名
- 参数password:连接的密码
- 参数charset:通信采用的编码方式,默认是'gb2312',要求与数据库创建时指定的编码一致,否则中文会乱码
对象的方法
- close()关闭连接
- commit()事务,所以需要提交才会生效
- rollback()事务,放弃之前的操作
- cursor()返回Cursor对象,用于执行sql语句并获得结果
Cursor对象
- 执行sql语句
- 创建对象:调用Connection对象的cursor()方法
cursor1=conn.cursor()
对象的方法
- close()关闭
- execute(operation [, parameters ])执行语句,返回受影响的行数
- fetchone()执行查询语句时,获取查询结果集的第一个行数据,返回一个元组
- next()执行查询语句时,获取当前行的下一行
- fetchall()执行查询时,获取结果集的所有行,一行构成一个元组,再将这些元组装入一个元组返回
- scroll(value[,mode])将行指针移动到某个位置
- mode表示移动的方式
- mode的默认值为relative,表示基于当前行移动到value,value为正则向下移动,value为负则向上移动
- mode的值为absolute,表示基于第一条数据的位置,第一条数据的位置为0
对象的属性
- rowcount只读属性,表示最近一次execute()执行后受影响的行数
- connection获得当前连接对象
增加
- 创建testInsert.py文件,向学生表中插入一条数据
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
cs1=conn.cursor()
count=cs1.execute("insert into students(sname) values('张良')")
print count
conn.commit()
cs1.close()
conn.close()
except Exception,e:
print e.message
修改
- 创建testUpdate.py文件,修改学生表的一条数据
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
cs1=conn.cursor()
count=cs1.execute("update students set sname='刘邦' where id=6")
print count
conn.commit()
cs1.close()
conn.close()
except Exception,e:
print e.message
删除
- 创建testDelete.py文件,删除学生表的一条数据
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
cs1=conn.cursor()
count=cs1.execute("delete from students where id=6")
print count
conn.commit()
cs1.close()
conn.close()
except Exception,e:
print e.message
sql语句参数化
- 创建testInsertParam.py文件,向学生表中插入一条数据
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
cs1=conn.cursor()
sname=raw_input("请输入学生姓名:")
params=[sname]
count=cs1.execute('insert into students(sname) values(%s)',params)
print count
conn.commit()
cs1.close()
conn.close()
except Exception,e:
print e.message
其它语句
- cursor对象的execute()方法,也可以用于执行create table等语句
- 建议在开发之初,就创建好数据库表结构,不要在这里执行
查询一行数据
- 创建testSelectOne.py文件,查询一条学生信息
#encoding=utf8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
cur=conn.cursor()
cur.execute('select * from students where id=7')
result=cur.fetchone()
print result
cur.close()
conn.close()
except Exception,e:
print e.message
查询多行数据
- 创建testSelectMany.py文件,查询一条学生信息
#encoding=utf8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
cur=conn.cursor()
cur.execute('select * from students')
result=cur.fetchall()
print result
cur.close()
conn.close()
except Exception,e:
print e.message
封装
- 观察前面的文件发现,除了sql语句及参数不同,其它语句都是一样的
- 创建MysqlHelper.py文件,定义类
#encoding=utf8
import MySQLdb
class MysqlHelper():
def __init__(self,host,port,db,user,passwd,charset='utf8'):
self.host=host
self.port=port
self.db=db
self.user=user
self.passwd=passwd
self.charset=charset
def connect(self):
self.conn=MySQLdb.connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)
self.cursor=self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def get_one(self,sql,params=()):
result=None
try:
self.connect()
self.cursor.execute(sql, params)
result = self.cursor.fetchone()
self.close()
except Exception, e:
print e.message
return result
def get_all(self,sql,params=()):
list=()
try:
self.connect()
self.cursor.execute(sql,params)
list=self.cursor.fetchall()
self.close()
except Exception,e:
print e.message
return list
def insert(self,sql,params=()):
return self.__edit(sql,params)
def update(self, sql, params=()):
return self.__edit(sql, params)
def delete(self, sql, params=()):
return self.__edit(sql, params)
def __edit(self,sql,params):
count=0
try:
self.connect()
count=self.cursor.execute(sql,params)
self.conn.commit()
self.close()
except Exception,e:
print e.message
return count
添加
- 创建testInsertWrap.py文件,使用封装好的帮助类完成插入操作
#encoding=utf8
from MysqlHelper import *
sql='insert into students(sname,gender) values(%s,%s)'
sname=raw_input("请输入用户名:")
gender=raw_input("请输入性别,1为男,0为女")
params=[sname,bool(gender)]
mysqlHelper=MysqlHelper('localhost',3306,'test1','root','mysql')
count=mysqlHelper.insert(sql,params)
if count==1:
print 'ok'
else:
print 'error'
查询一个
- 创建testGetOneWrap.py文件,使用封装好的帮助类完成查询最新一行数据操作
#encoding=utf8
from MysqlHelper import *
sql='select sname,gender from students order by id desc'
helper=MysqlHelper('localhost',3306,'test1','root','mysql')
one=helper.get_one(sql)
print one
实例:用户登录
创建用户表userinfos
- 表结构如下
- id
- uname
- upwd
- isdelete
- 注意:需要对密码进行加密
- 如果使用md5加密,则密码包含32个字符
- 如果使用sha1加密,则密码包含40个字符,推荐使用这种方式
create table userinfos(
id int primary key auto_increment,
uname varchar(20),
upwd char(40),
isdelete bit default 0
);
加入测试数据
- 插入如下数据,用户名为123,密码为123,这是sha1加密后的值
insert into userinfos values(0,'123','40bd001563085fc35165329ea1ff5c5ecbdbbeef',0);
接收输入并验证
- 创建testLogin.py文件,引入hashlib模块、MysqlHelper模块
- 接收输入
- 根据用户名查询,如果未查到则提示用户名不存在
- 如果查到则匹配密码是否相等,如果相等则提示登录成功
- 如果不相等则提示密码错误
#encoding=utf-8
from MysqlHelper import MysqlHelper
from hashlib import sha1
sname=raw_input("请输入用户名:")
spwd=raw_input("请输入密码:")
s1=sha1()
s1.update(spwd)
spwdSha1=s1.hexdigest()
sql="select upwd from userinfos where uname=%s"
params=[sname]
sqlhelper=MysqlHelper('localhost',3306,'test1','root','mysql')
userinfo=sqlhelper.get_one(sql,params)
if userinfo==None:
print '用户名错误'
elif userinfo[0]==spwdSha1:
print '登录成功'
else:
print '密码错误'
MysQL使用一与Python交互的更多相关文章
- mysql及python交互
mysql在之前写过一次,那时是我刚刚进入博客,今天介绍一下mysql的python交互,当然前面会把mysql基本概述一下. 目录: 一.命令脚本(mysql) 1.基本命令 2.数据库操作命令 3 ...
- MySQL 存储引擎、锁、调优、失误与事务回滚、与python交互、orm
1.存储引擎(处理表的处理器) 1.基本操作 1.查看所有存储引擎 mysql> show engines; 2.查看已有表的存储引擎 mysql> show create table 表 ...
- 开发使用mysql的一些必备知识点整理(四)与python交互
与python交互 在熟练使用sql语句的基础上,开始使用python语言提供的模块与mysql进行交互 这是我们在工作中大事要做的事 先学会sql是基础,一定要熟练编写sql语句 安装引入模块 安装 ...
- MySQL和Python交互
与Python交互 python3模块名:pymysql conda install pymysql conda install sqlalchemy python2模块名:MySQLdb impor ...
- MySQL——python交互
与python交互之前我们需要安装一个MySQL的驱动模块Connector,这个驱动模块直接在cmd命令行输入 pip install mysql.connector 安装是否成功可以接着输入 py ...
- Nginx+uWSGI+Django+Python+ MySQL 搭建可靠的Python Web服务器
一.安装所需工具 yum -y install gcc gcc-c++ rpm-build mysql* libtool-ltdl* libtool automake autoconf libtool ...
- 7.与python交互
与python交互 在熟练使用sql语句的基础上,开始使用python语言提供的模块与mysql进行交互 这是我们在工作中大事要做的事 先学会sql是基础,一定要熟练编写sql语句 安装引入模块 安装 ...
- 工大助手(C#与python交互)
工大助手(爬虫--C#与python交互) 基本内容 工大助手(桌面版) 实现登陆.查成绩.计算加权平均分等功能 团队人员 13070046 孙宇辰 13070003 张帆 13070004 崔巍 1 ...
- Python教程(1.2)——Python交互模式
上一节已经说过,安装完Python,在命令行输入"python"之后,如果成功,会得到类似于下面的窗口: 可以看到,结尾有3个>符号(>>>).>&g ...
随机推荐
- Node REPL
Node 自带了交互式解释器,可以执行以下任务: 读取 - 读取用户输入,解析输入了Javascript 数据结构并存储在内存中. 执行 - 执行输入的数据结构 打印 - 输出结果 循环 - 循环操作 ...
- RabbitMq入门与基本使用
这两天工作项目中用到了rabbitmq,顺便学习了一下. RabbitMq主要的使用模式有三种:工作队列,发布订阅和RPC远程调用. 1.工作队列 生产者: using System; using R ...
- 常用移动web开发框架--转载
阅读目录 1.1 jQuery mobile flat-ui 主题 1.2jQuery mobile Bootstrap 主题 4.1 GMU 4.2 Clouda+ 4.3 efe 5.1 Sp ...
- Win32 配置文件用法
#include "stdafx.h"#include <Shlobj.h>#include <Shlwapi.h> #pragma comment(lib ...
- 我不想用for循环
为什么要挑战自己在代码里不写for loop?因为这样可以迫使你去使用比较高级.地道的语法或库.文中以python为例子,讲了不少大家其实在别人的代码里都见过.但自己很少用的语法. 这是一个挑战.我要 ...
- Newman语法参数
npm install -g newman npm install -g newman-reporter-html newman run XXX.json -k -r html --reporter- ...
- (0.2.7)Mysql安装——多实例安装
(0.2.6)Mysql安装——多实例安装 待完善
- CCF 201312-3 最大的矩形[比较简单]
问题描述 试题编号: 201312-3 试题名称: 最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ ...
- 【多线程基础】- 多个线程顺序打印ABC
题目:3个线程名字分别是A,B,C 现在在console上连续打印10次 ABC . public class Test { public static void main(String[] args ...
- Delphi APP 開發入門(四)簡易手電筒
Delphi APP 開發入門(四)簡易手電筒 分享: Share on facebookShare on twitterShare on google_plusone_share 閲讀次數:32 ...