Python对mysql的操作三
来源:互联网 发布:vb.net 入门 编辑:程序博客网 时间:2024/05/17 22:43
1)创建表
import MySQLdbdef connect_mysql(): db_config = { "host":"localhost", "user":"root", "passwd":"root", "db":"python_db", "charset":"utf8" } try: cnx = MySQLdb.connect(**db_config) except Exception as e: raise e return cnxstudent = ''' create table student( stdid int primary key not null, stdname varchar(100) not null, gender enum('F','M'), age int )'''course = ''' create table course( couid int primary key not null, cname varchar(100) not null, tid int not null )'''sorce = ''' create table sorce( sid int primary key not null, std int not null, couid int not null, grade int not null )'''teacher = ''' create table teacher( tid int primary key not null, tname varchar(100) not null )'''tmp = ''' set @a := 0; # mysql中变量不用事前申明,直接用“set @变量名”就可以了 create table tmp as select (@a := @a + 1)as id from information_schema.tables limit 10; # 当@a需要在select中使用的时候,必须加:'''if __name__ == '__main__': cnx = connect_mysql() cus = cnx.cursor() try: cus.execute(student) cus.execute(course) cus.execute(sorce) cus.execute(teacher) cus.execute(tmp) cus.close() cnx.commit() except Exception as e: cnx.rollback() raise e finally: cnx.close()
2)增加数据
# select * from tmp; # 10条数据# select * from tmp a,tmp b,tmp c; # 1000条数据,相当于10*10*10# 获得随机字符串的设计# 增加的数据是随机数据, rand()函数随机生成一个0-1的随机数# sha1()函数对数字进行加密,然后生成一堆字符串# concat()是拼接多个字符串的函数# substr()截取多少个字符# 获得随机整数的设计# rand()*50 获得0-50的随机小数# floor() 这个函数代表的是去尾法取整数# 男女的设计# rand()*10 /2 最后取余数# 如果余数为1,就设置为M# 如果余数为0,就设置为Fstudent1 = ''' set @b := 10000; insert into student select @b:= @b+1,substr(concat(sha1(rand()),sha1(rand())),1,5+floor(rand()*50)),case floor(rand()*10) mod 2 when 1 then 'M' else 'F' end,20+floor(rand()*8) from tmp a,tmp b,tmp c,tmp d;'''course1 = ''' set @c := 10; insert into course select @c:= @c+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 45)), 100 + floor(rand() * 100) from tmp a;'''sorce1 = ''' set @d := 10000; insert into sorce select @d:= @d+1, floor(10001 + rand()*10000), floor(11 + rand()*10), floor(1+rand()*100) from tmp a, tmp b, tmp c, tmp d;'''theacher1 = ''' set @e := 100; insert into teacher select @e:= @e+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 80)) from tmp a, tmp b;'''
3)查询数据
import codecsimport MySQLdbdef connect_mysql(): db_config = { 'host': 'localhost', 'port': 3306, 'user': 'root', 'passwd': 'root', 'db': 'python_db', 'charset': 'utf8' } cnx = MySQLdb.connect(**db_config) return cnxif __name__ == '__main__': cnx = connect_mysql() sql = '''select * from course;''' #sql = '''select * from student where stdname in (select stdname from student group by stdname having count(1)>1 ) order by stdname;''' try: cus = cnx.cursor() cus.execute(sql) result = cus.fetchall() with codecs.open('select.txt', 'w+') as f: for line in result: f.write(str(line)) f.write('\n') cus.close() cnx.commit() except Exception as e: cnx.rollback() print('error') raise e finally: cnx.close()
阅读全文
0 0
- Python对mysql的操作三
- python对MySQL的操作
- python对mysql的操作
- python对mysql的操作
- python对mysql的操作
- Python对mysql的操作
- Python对nosql的操作三
- 使用pymysql在python中对mysql的增删改查操作(三)
- Python对MySQL数据库的操作
- Python中对mysql数据库的操作
- python对mysql的操作二
- Python对mysql的操作四
- python对mysql的一些操作(drop,create,insert)
- Python基础知识——对mysql数据库的操作
- Python接口测试之对MySQL的操作(六)
- Python操作MySql-pybatis的准备阶段(三)
- Python操作MySQL数据库的三种方法
- Python连接MySQL及对其操作
- 【01-Redis 介绍】
- 003 全概率公式与贝叶斯公式
- C++学习日记(1)
- [工作效率提升]焦虑性失眠的应对方法
- JavaCore-OOP-1-最最基础的商品管理
- Python对mysql的操作三
- day 28 awk
- Evnetbus使用
- RxJava+MVP+轮播+retrofit+条目点击+recyclerView
- 《七点三刻》北方降温/腾讯成功吃鸡/IS灭亡/小蓝车可电话退款…有价值的新闻早餐 2017年11月23日星期四,第014期
- poj2653 Pick-up sticks
- [Unity]Unity组件崩溃的通用的几种解决办法
- 登陆注册
- 人工智能让人类尴尬:伦理道德法律框架设计滞后