ubuntu 系统,安装方法为:
- apt-get install python-MySQLdb,
-
这样当在 python 环境执行 import MySQLdb 不报错就是安装好了。
- root@ubuntu:~# python
- Python 2.7.4 (default, Apr 19 2013, 18:32:33)
- [GCC 4.7.3] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import MySQLdb
- >>>
-
MySQLdb 提供的 connect 方法用来和数据库建立连接,接收数个参数,返回连接对象,如:
- conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable",port=3306)
-
特别注意,因为数据库常常是 utf8 编码,所以连接数据库的时候,推荐使用下面的方法
- conn = MySQLdb.connect(host='localhost', user='root', passwd='root', db='python',charset='utf8')
-
charset是要跟你数据库的编码一样,如果是数据库是 gb2312 ,则写 charset='gb2312'。
比较常用的参数包括:
注:connect 中的 host、user、passwd 等可以不写,只有在写的时候按照 host、user、passwd、db (可以不写)、port顺序写就可以,注意端口号port=3306 还是不要省略的为好,如果没有db在port前面,直接写3306会报错.
连接成功后,如需切换该用户的其他数据库,使用以下语句:conn.select_db('mysql')形式切换数据库
- >>> con=MySQLdb.connect('localhost','root','123456',port=3306)
- >>> con.select_db('mysql')
- >>> cur=con.cursor()
- >>> cur.execute('show tables')
- 24L
- >>> cur.fetchall()
- (('columns_priv',), ('db',), ('event',), ('func',), ('general_log',), ('help_category',),
- ('help_keyword',), ('help_relation',), ('help_topic',), ('host',), ('ndb_binlog_index',),
- ('plugin',), ('proc',), ('procs_priv',), ('proxies_priv',), ('servers',), ('slow_log',),
- ('tables_priv',), ('time_zone',), ('time_zone_leap_second',), ('time_zone_name',),
- ('time_zone_transition',), ('time_zone_transition_type',), ('user',))
-
第1行:连接数据库
第2行:选择连接mysql这个数据库
第3行以下是获取数据库表,语法后面会讲
MySQLdb用游标(指针)cursor 的方式操作数据库
因该模块底层其实是调用 CAPI 的,所以,需要先得到当前指向数据库的指针
- >>> cur=con.cursor()
-
数据库的操作和结果显示编辑本段回目录
我们利用 cursor 提供的方法来进行操作,方法主要是:
创建数据库51ctotest
- >>> cur.execute('create database 51ctotest')
-
选择数据库51ctotest
- >>>con.select_db('51ctotest')
-
创建表51cto,id自增
- >>>cur.execute('
- create table if not exists 51cto(
- id int(11) PRIMARY KEY AUTO_INCREMENT,
- name varchar(20),age int(3)
- )
- ')
-
插入一行数据,只给name、age赋值,让id自增
使用sql语句,这里要接收的参数都用%s占位符.要注意的是,无论你要插入的数据是什么类型,占位符永远都要用%s,后面的数值为元组或者列表
- >>>cur.execute("insert into 51cto(name,age) values(%s,%s)",('fan',25))
-
插入多行数据,用executemany,它会循环插入后面元组中的所有值
- >>> cur.executemany("insert into 51cto(name,age) values(%s,%s)",
- (('te',25),('fei',26),('musha',25)))
- 3L
-
查询
- >>> cur.execute('select * from 51cto')
- 5L
-
我们使用了fetchall这个方法.这样,cds里保存的将会是查询返回的全部结果.每条结果都是一个tuple类型的数据,这些tuple组成了一个tuple
- >>> cur.fetchall() ((1L, 'fan', 25L), (2L, 'fan', 25L), (3L, 'te', 25L),
- (4L, 'fei', 26L), (5L, 'musha', 25L))
-
再次查询,会看到查询不到结果,因为无论 fetchone、fetchall、fetchmany 指针是会发生移动的。所以,若不重置指针,那么使用fetchall的信息将只会包含指针后面的行内容。使用fetchall把指针挪到了最后,可以用scroll手动把指针挪到一个位置
- >>> cur.fetchall() ()
- >>> cur.scroll(1,'absolute')
- >>> for i in cur.fetchall():
- print i
- (2L, 'fan', 25L)
- (3L, 'te', 25L)
- (4L, 'fei', 26L)
- (5L, 'musha', 25L)
-
这里有必要说一下scroll:
- cur.scroll(int,parm) 这里参数含义为:
-
fetchone 一次只取一行,指针下移 fetchmany(size)一次去 size 行
- >>> cur.scroll(1,'absolute')
- >>> cur.fetchone()
- (2L, 'fan', 25L)
- >>> cur.fetchmany(2)
- ((3L, 'te', 25L), (4L, 'fei', 26L))
-
普通取出是元组的形式,再从里面取值不好取,那怎么取成字典的格式呢,MySQLdb中有DictCursor,要做到这点也很简单,那就是建立数据库连接是传递cusorclass参数,或者在获取Cursor对象时传递cusorclass参数即可
- >>> cur = con.cursor(cursorclass=MySQLdb.cursors.DictCursor)
- >>> cur.execute('select * from 51cto')
- 5L
- >>> for i in cur.fetchall():
- ... print i
- ...
- {'age': 25L, 'id': 2L, 'name': 'fan'}
- {'age': 25L, 'id': 3L, 'name': 'te'}
- {'age': 26L, 'id': 4L, 'name': 'fei'}
- {'age': 25L, 'id': 5L, 'name': 'musha'}
-
更新,习惯 %s 的用法
- >>> cur.execute('update 51cto set name=%s where id=3',('Mus'))
- >>> cur.scroll(2,'absolute')
- >>> cur.fetchone()
- {'age': 25L, 'id': 3L, 'name': 'Mus'}
-
在执行完插入或删除或修改操作后,需要调用一下 conn.commit() 方法进行提交。这样数据才会真正保,存在数据库中。
- >>> con.commit()
-
最后关闭游标,关闭连接
- >>> cur.close()
- >>> con.close()
-
- #encoding=utf-8
- import sys
- import MySQLdb
- reload(sys)
- sys.setdefaultencoding('utf-8')
- db=MySQLdb.connect(user='root',charset='utf8')
-
注:MySQL 的配置文件设置也必须配置成 utf8 设置 MySQL 的 my.cnf 文件,在 [client]/[mysqld] 部分都设置默认的字符集(通常在 /etc/mysql/my.cnf ):
- [client] default-character-set = utf8
- [mysqld] default-character-set = utf8