Chinaunix首页 | 论坛 | 博客
  • 博客访问: 427592
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1101
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-20 19:29
个人简介

http://meetbill.github.io/

文章分类

全部博文(121)

我的朋友

分类: LINUX

2016-03-13 23:21:46

python自动创建数据库以及数据库中的表

import MySQLdb
conn = MySQLdb.connect(host = 'localhost', port = 3306, user = 'root', passwd = '*****')
curs = conn.cursor()   
 try:  curs.execute('create database addtest') except:  print 'Database addtest exists!' conn.select_db('addtest') # create a table named addfields 
try:  curs.execute('create table addfields(id int PRIMARY KEY NOT NULL,name text)') except:  print('The table addfields exists!') # add the fileds  
curs.close()
conn.close()
阅读(1284) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~