python学习之mysql数据库编程基础知识介绍

在python网络爬虫中,通常是通过txt纯文本方式存储,其实也是可以存储在数据库中的;同时在wamp(windows、apache、mysql、php或python)开发网站中,也可以通过python构建网页的,所以这篇文章主要讲述python调用mysql数据库相关编程知识

在python网络爬虫中,通常是通过txt纯文本方式存储,其实也是可以存储在数据库中的;同时在wamp(windows、apache、mysql、php或python)开发网站中,也可以通过python构建网页的,所以这篇文章主要讲述python调用mysql数据库相关编程知识。从以下几个方面进行讲解:

1.配置myslq2.sql语句基础知识3.python操作mysql基础知识4.python调用mysql示例

一. 配置mysql

首先下载mysql-5.0.96-winx64,安装过程如下图所示。 1.安装mysql 5.0python学习之mysql数据库编程基础知识介绍 python学习之mysql数据库编程基础知识介绍python学习之mysql数据库编程基础知识介绍 python学习之mysql数据库编程基础知识介绍 2.选择手动配置、服务类型、通用多功能型和安装路径python学习之mysql数据库编程基础知识介绍 python学习之mysql数据库编程基础知识介绍python学习之mysql数据库编程基础知识介绍 python学习之mysql数据库编程基础知识介绍 3.设置数据库访问量连接数为15、端口为3306(代码中设置url用到)、编码方式为utf-8python学习之mysql数据库编程基础知识介绍 python学习之mysql数据库编程基础知识介绍python学习之mysql数据库编程基础知识介绍 python学习之mysql数据库编程基础知识介绍 4.设置默认超级root用户的用户名和密码,最后安装成功python学习之mysql数据库编程基础知识介绍 python学习之mysql数据库编程基础知识介绍

二. sql语句基础知识

安装mysql 5.0成功后,进行数据库的简单操作。 1.运行mysql输入默认用户密码123456

python学习之mysql数据库编程基础知识介绍

2.创建数据库test01和使用数据库(第二次调用直接use database) create database test01;

python学习之mysql数据库编程基础知识介绍

显示数据库中包含的数据库:show databases;

python学习之mysql数据库编程基础知识介绍

3.创建表student,其中学号为主键 create table student(username varchar(20),password varchar(20),stuid int primary key);

python学习之mysql数据库编程基础知识介绍

4.显示表结构,使用语句desc student

python学习之mysql数据库编程基础知识介绍

5.向学生表中插入数据并显示查询的数据

python学习之mysql数据库编程基础知识介绍

6.删除表:drop table student;

python学习之mysql数据库编程基础知识介绍

7.更新数据 update student set password=’000000′ where stu;

python学习之mysql数据库编程基础知识介绍

8.删除数据 delete from student where username=’eastmount;

python学习之mysql数据库编程基础知识介绍

此时mysql操作数据库基本讲解结束,你同样可以实现数据库的增删改查、事务、存储过程等操作,建议安装可视化的软件来替代黑框,或使用navicat for mysql软件即可。代码如下:

enter password: ******
mysql> show databases;
+——————–+
| database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
| test01 |
+——————–+
5 rows in set (0.00 sec)
mysql> use test01;
database changed
mysql> show tables;
empty set (0.00 sec)
mysql> create table student(username varchar(20),
-> password varchar(20),
-> stuid int primary key);
query ok, 0 rows affected (0.33 sec)
mysql> show tables;
+——————+
| tables_in_test01 |
+——————+
| student |
+——————+
1 row in set (0.00 sec)
mysql> desc student;
+———-+————-+——+—–+———+——-+
| field | type | null | key | default | extra |
+———-+————-+——+—–+———+——-+
| username | varchar(20) | yes | | null | |
| password | varchar(20) | yes | | null | |
| stuid | int(11) | no | pri | null | |
+———-+————-+——+—–+———+——-+
3 rows in set (0.03 sec)
mysql> insert student(username, password, stuid)
-> values(‘eastmount’,’123456′,1)
-> ;
query ok, 1 row affected (0.05 sec)
mysql> select * from student;
+———–+———-+——-+
| username | password | stuid |
+———–+———-+——-+
| eastmount | 123456 | 1 |
+———–+———-+——-+
1 row in set (0.00 sec)
mysql> update student set password=’000000′ where stu;
query ok, 1 row affected (0.10 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> select * from student;
+———–+———-+——-+
| username | password | stuid |
+———–+———-+——-+
| eastmount | 000000 | 1 |
+———–+———-+——-+
1 row in set (0.00 sec)
mysql> delete from student where username=’eastmount’;
query ok, 1 row affected (0.08 sec)
mysql> select * from student;
empty set (0.00 sec)
mysql>

三. python调用mysql基础知识

通常的安装方法是使用:pip install mysql 安装python的mysql库,但是总会报错。常见错误如:microsoft visual c++ 9.0 is required (unable to find vcvarsall.bat)mysql.c(42) : fatal error c1083: cannot open include file: ‘config-win.h’: no such file or directory这些可能是驱动等问题。

python学习之mysql数据库编程基础知识介绍

正确安装方法:建议下载一个mysql-python-1.2.3.win-amd64-py2.7.exe文件进行安装。官网地址:https://pypi.python.org/pypi/mysql-python/

python学习之mysql数据库编程基础知识介绍

下面我们要详细了解python数据库api。从python中访问数据库需要接口程序,接口程序是一个python模块,它提供数据库客户端库(通常是c语言写成的)的接口供你访问。注意:python接口程序都一定要遵守python db-api规范。db-api是一个规范。它定义了一系列必须的对象和数据库存取方式,以便为各种各样的底层数据库系统和多种多样的数据库接口程序提供一致的访问接口。db-api为不同的数据库提供了一致的访问接口,在不同的数据库之间移植代码成为一件轻松的事情。

下面简单介绍db-api的使用方法。

1.模块属性db-api规范里的以下特性和属性必须提供。一个db-api兼容模块定义如下所示:

apilevel:模块兼容的db-api版本号
threadsafety:线程安全级别
paramstyle:支持sql语句参数风格
connect():连接数据库

python调用mssql需要导入mysqldb库,如下:

import mysqldb

2.connect()函数

其中主要使用的方法是connect对象。connect()方法生成一个connect对象,用于访问数据库,其参数如下:

user:username
password:password
host:hostname
database:databasename
dsn:data source name

注意并非所有的接口程序都严格按照这种格式,如mysqldb。

import mysqldb
conn = mysqldb.connect(host=’localhost’, db=’test01′, user=’root’, passwd=’123456′, port=3306, charset=’utf8′)

connect()对象方法如下:

close():关闭数据库连接,或者关闭游标对象
commit():提交当前事务
rollback():取消当前事务
cursor():创建游标或类游标对象
errorhandler(cxn,errcls,errval):作为已给游标的句柄

注意,执行close()方法则上述的连接对象方法不能再使用,否则发生异常。commit()、rollback()、cursor()或许更对于支持事务的数据库更有意义。数据库事务(database transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完整地执行,要么完全地不执行。 一旦你完成了数据库连接,关闭了游标对象,然后在执行commit()提交你的操作,然后关闭连接。

3.游标对象上面说了connect()方法用于提供连接数据库的接口,如果要对数据库操作那么还需要使用游标对象。游标对象的属性和方法:

fetchone():可以看作fetch(取出) one(一个),也就是得到结果集的下一行(一行)。
fetchmany(size):可以看作fetch(取出)many(多个),这里的参数是界限,得到结果集的下几行(几行)
fetchall():顾名思义,取得所有。
execute(sql):执行数据库操作,参数为sql语句。
close():不需要游标时尽可能的关闭

下面通过简单的示例进行讲解。

四. python调用mysql示例

在前面数据库中我们创建了数据库“test01”和表“student”,同时插入了数据。那么,怎样通过python来显示呢?1.查询所有数据库首先,我们查看本地数据库中所包含的数据库名称,通过“show databases”语句。

import mysqldb
try:
conn=mysqldb.connect(host=’localhost’,user=’root’,passwd=’123456′,port=3306)
cur=conn.cursor()
res = cur.execute(‘show databases’)
print res
for data in cur.fetchall():
print ‘%s’ % data
cur.close()
conn.close()
except mysqldb.error,e:
print “mysql error %d: %s” % (e.args[0], e.args[1])

其中通过链接数据库代码为:conn=mysqldb.connect(host=’localhost’,user=’root’,passwd=’123456′,port=3306)访问root超级用户,其密码为“123456”,端口为“3306”,其结果如下:

python学习之mysql数据库编程基础知识介绍

如果不知道本地数据库的名称,可以通过该方法,先查询数据库中包含哪些数据库,然后再连接该数据库进行相关的操作。

2.查询表下面介绍查询表student中数据,代码如下,代码的具体含义是通过connect()连接数据库,通过conn.cursor()定义游标,然后调用游标的excute(sql)执行数据库操作,此处为查询操作,再通过fetchall()函数获取所有数据。

# coding:utf-8
import mysqldb
try:
conn=mysqldb.connect(host=’localhost’,user=’root’,passwd=’123456′,port=3306, db=’test01′, charset=’utf8′)
cur=conn.cursor()
res = cur.execute(‘select * from student’)
print u’表中包含’,res,u’条数据\n’
print u’数据如下:(姓名 密码 序号)’
for data in cur.fetchall():
print ‘%s %s %s’ % data
cur.close()
conn.close()
except mysqldb.error,e:
print “mysql error %d: %s” % (e.args[0], e.args[1])

输出结果如图所示:

python学习之mysql数据库编程基础知识介绍

对应的mysql中的结果是一致的,下图是对应的结果。

python学习之mysql数据库编程基础知识介绍

3.创建表下面这段代码是创建一张教师表,主要是通过commit()提交数据。

# coding:utf-8
import mysqldb
try:
conn=mysqldb.connect(host=’localhost’,user=’root’,passwd=’123456′,port=3306, db=’test01′, charset=’utf8′)
cur=conn.cursor()
#查看表
print u’插入前包含表:’
cur.execute(‘show tables’)
for data in cur.fetchall():
print ‘%s’ % data
#插入数据
sql = ”’create table teacher(id int not null primary key auto_increment,
name char(30) not null,
sex char(20) not null
)”’
cur.execute(sql)
#查看表
print u’\n插入后包含表:’
cur.execute(‘show tables’)
for data in cur.fetchall():
print ‘%s’ % data
cur.close()
conn.commit()
conn.close()
except mysqldb.error,e:
print “mysql error %d: %s” % (e.args[0], e.args[1])

输出结果如下所示,插入教师表,包含字段:教师序号(id)、教师名称(name)、教师性别(sex)。

python学习之mysql数据库编程基础知识介绍

插入数据也可以通过execute(sql)方法实现,如: cur.execute(“insert into student values( ‘yxz’, ‘111111’, ’10’)”)但插入的新数据通常是通过变量进行赋值,而不是固定的,所以要对这条语句中的值做修改。我们可以做如下修改:

# coding:utf-8
import mysqldb
try:
conn=mysqldb.connect(host=’localhost’,user=’root’,passwd=’123456′,port=3306, db=’test01′)
cur=conn.cursor()
#插入数据
sql = ”’insert into student values(%s, %s, %s)”’
cur.execute(sql, (‘yxz’,’111111′, ’10’))
#查看数据
print u’\n插入数据:’
cur.execute(‘select * from student’)
for data in cur.fetchall():
print ‘%s %s %s’ % data
cur.close()
conn.commit()
conn.close()
except mysqldb.error,e:
print “mysql error %d: %s” % (e.args[0], e.args[1])

输出结果如下所示:

>>>
插入数据:
esatmount 123456 1
yangxiuzhang 123456 2
xiaoy 123456 3
yxz 111111 10
>>>

同样,对数据库的增删改插都可以进行,请读者自行阅读。推荐资料:python使用mysql数据库 – 虫师后面我会结合python爬虫讲述,如何将爬取的内容存储在数据库中,如我csdn的博客,爬取博客标题、发布时间、阅读量和评论数。

python学习之mysql数据库编程基础知识介绍

mysql数据库中结果如下图所示:

python学习之mysql数据库编程基础知识介绍

最后希望文章对你有所帮助,如果文章中存在不足或错误的地方,还请海涵~还是那句话,挺享受现在的老师生活,不论科研、项目,还是教学,很充实,加油!

以上就是python学习之mysql数据库编程基础知识介绍的详细内容,更多请关注 第一php社区 其它相关文章!

Posted in 未分类

发表评论