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

python2.0_s12_day9_mysql操作

时间:2016-06-13 18:56:27      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

mysql的基本语法:
1.数据库操作
show databases;
create database 数据库名;如果想允许数据库可以写中文create database 数据库名 charset utf8
use 数据库名;
show tables;

2.数据表操作
create table 表名
(
id int not null auto_increment primary key, # 主键 指每一行的唯一标示符
name char(9) not null,
sex char(4) not null,
age tinyint unsigned not null, # unsigned
tel char(13) null default "_"
);
desc 表名;查看表结构
show create table 表名; 查看这个表是通过什么语句创建的
alter table students add column 字段名 char(30); 给表插入一个字段
InnoDB 数据引擎,是支持事务性操作,比如ATM银行转帐,拿现金转账,当你现金存入,开始转账的时候断电,那么转账失败,同时数据库会把存入成功的记录也会回滚,变得不成功,随后把钱给你退出来.

3.数据操作
insert into 表名(字段1,字段2,字段3) values(‘值1‘,‘值2‘,‘值3‘) ; 数据插入
delete from 表名 where 字段1 = ‘值‘; 删除行记录
update 表名 set 字段2 = ‘sb‘ where 字段1 = ‘值‘; 更新表中某条记录的某个字段值
select * from 表名 ; 查寻表所有记录

4.其他
主键
外键
左右连接

python连接mysql的模块

  python连接mysql的模块很多,我们使用MySQLdb模块,需要下载。
一、插入数据
 1         import MySQLdb
 2 
 3         conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
 4 
 5         cur = conn.cursor()
 6 
 7         reCount = cur.execute(insert into UserInfo(Name,Address) values(%s,%s),(alex,usa))
 8         # reCount = cur.execute(insert into UserInfo(Name,Address) values(%(id)s, %(name)s),{id:12345,name:wupeiqi})
 9 
10         conn.commit()
11 
12         cur.close()
13         conn.close()
14 
15         print reCount
    上面使用cur.execute()方法插入一条记录,那么怎样批量插入数据记录呢.可以使用cur.executemany()
 1      import MySQLdb
 2 
 3         conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
 4 
 5         cur = conn.cursor()
 6 
 7         li =[
 8              (alex,usa),
 9              (sb,usa),
10         ]
11         reCount = cur.executemany(insert into UserInfo(Name,Address) values(%s,%s),li)
12 
13         conn.commit()
14         cur.close()
15         conn.close()
16 
17         print reCount

二、删除数据
 1         import MySQLdb
 2 
 3         conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
 4 
 5         cur = conn.cursor()
 6 
 7         reCount = cur.execute(delete from UserInfo)
 8 
 9         conn.commit()
10 
11         cur.close()
12         conn.close()
13 
14         print reCount
    三、修改数据
 1 import MySQLdb
 2 
 3         conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
 4 
 5         cur = conn.cursor()
 6 
 7         reCount = cur.execute(update UserInfo set Name = %s,(alin,))
 8 
 9         conn.commit()
10         cur.close()
11         conn.close()
12 
13         print reCount

 


四、查数据
 1         # ############################## fetchone/fetchmany(num)  ##############################
 2 
 3         import MySQLdb
 4 
 5         conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
 6         cur = conn.cursor()
 7 
 8         reCount = cur.execute(select * from UserInfo)
 9 
10         print cur.fetchone()
11         print cur.fetchone()
12         cur.scroll(-1,mode=relative)
13         print cur.fetchone()
14         print cur.fetchone()
15         cur.scroll(0,mode=absolute)
16         print cur.fetchone()
17         print cur.fetchone()
18 
19         cur.close()
20         conn.close()
21 
22         print reCount
 1         # ############################## fetchall  ##############################
 2 
 3         import MySQLdb
 4 
 5         conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
 6         #cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
 7         cur = conn.cursor()
 8 
 9         reCount = cur.execute(select Name,Address from UserInfo)
10 
11         nRet = cur.fetchall()
12 
13         cur.close()
14         conn.close()
15 
16         print reCount
17         print nRet
18         for i in nRet:
19             print i[0],i[1]

 





python2.0_s12_day9_mysql操作

标签:

原文地址:http://www.cnblogs.com/zhming26/p/5581376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!