1.Python创建数据库

import MySQLdb
try:
conn = MySQLdb.connect(
host="127.0.0.1",
port=3306,
user="root",
passwd="",
# db="juntest",#也可以在这一步显示数据库名
charset="utf8")
cur = conn.cursor()
cur.execute('CREATE DATABASE IF NOT EXISTS juntestDBnew DEFAULT CHARSET utf8 COLLATE utf8_general_ci;') #没有这个表,就创建
conn.close()
print u"创建数据库juntestDBnew成功! "
except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])

创建成功,查看

2.创建数据表

#coding=utf-8
import MySQLdb
try:
conn = MySQLdb.connect(
host="127.0.0.1",
port=3306,
user="root",
passwd="",
# db="juntest",另外写一种建表的方法
charset="utf8")
conn.select_db('juntestDBnew')#选择一个数据库
cur = conn.cursor()
cur.execute("drop table if exists emp_info2;")
cur.execute("drop table if exists salary2;")
cur.execute('''create table emp_info2(
id int not null auto_increment,
name varchar(30) not null,
sex char(4) default null,
dept varchar(10),
mobile varchar(11) not null unique,
birthday date default "0000-00-00",
primary key(id)
)engine=innodb character set utf8 comment 'employer info';
''')
cur.execute('''create table salary2(
id int not null auto_increment,
emp_id int not null,
salary int not null,
primary key(id)
)engine=innodb character set utf8 comment 'employer salary info';
''')
cur.close()
conn.close()
print u"创建数据表成功! " except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])

查看是否成功

3.进行数据的插入

#coding=utf-8
import MySQLdb
import random try:
conn = MySQLdb.connect(
host="127.0.0.1",
port=3306,
user="root",
passwd="",
db="juntestDBnew",#另外写一种建表的方法
charset="utf8"
)
cur=conn.cursor()
num=random.randint(10,300)
sql="insert into emp_info2 values(%s,%s,%s,%s,%s,%s)"#这种写法自增字段和隐藏字段都要写出来
#cur.execute(sql, (4, 'jun1', 'm', 'jun', '12345', '2017-4-30'));
for i in range(1,10):
cur.execute('delete from emp_info2 where id='+str(i));
#第一种插入
cur.execute(sql, (i, 'jun'+str(i), 'm', 'jun', ''+str(random.randint(1,1000)), '2017-4-'+str(random.randint(1,30))));
print u"插入第(%s)条数据成功"%i
cur.close()
conn.commit()
conn.close() except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])

最新文章

  1. js框架模版
  2. 机器学习&数据挖掘笔记_19(PGM练习三:马尔科夫网络在OCR上的简单应用)
  3. 对编写html代码的几点儿小建议
  4. nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 56; cvc-complex-type.2.4.c通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明
  5. ListView配合CheckBox出现position位置错误的解决
  6. How to Configure the Gradient Boosting Algorithm
  7. 分段播放的flash播放器
  8. 编译android版libmpg
  9. Foundation Data Structure
  10. 一个关于css3背景透明的例子
  11. Netflix
  12. 演练5-6:Contoso大学校园管理系统6
  13. Sudoku 小项目
  14. [apr] Apache Portable Runtime
  15. 20155206 Exp8 WEB基础实践
  16. SpringCloud负载均衡笔记
  17. C++的空指针、野指针和指针赋值NULL.md
  18. python(五):面向对象--类和实例
  19. Ubuntu : 解决更新时出现 Unable to locate package update
  20. 用python实现0到9之间10个数字排列不重复的个数

热门文章

  1. POJ 3710:Matrix Power Series
  2. IOS Core Image之一
  3. C#生成Excel
  4. 秒懂String,StringBuilder与StringBuffer
  5. 常见IT英语短语一
  6. composer gitlab 搭建私包
  7. BZOJ1968 [Ahoi2005] 约数研究
  8. PDO drivers no value in Windows 或 ndefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'
  9. spring 与mybatis 整合总结
  10. Week1——JavaEE