-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetting.py
77 lines (63 loc) · 2.01 KB
/
setting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: UTF-8 -*-
from admin import blog_list
import MySQLdb
class _db(object):
def __init__(self):
self.host = 'localhost'
self.port = 3306
self.user = 'root'
self.passwd = 'wuwangjie'
self.db_name = 'blog'
self.table_name = ''
def create_db(self):
try:
db = MySQLdb.connect(self.host, self.user, self.passwd)
cursor = db.cursor()
cursor.execute('show databases')
cursor.execute('CREATE DATABASE '+ self.db_name +' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci')
db.commit()
except MySQLdb.Error, e:
#db.rollback()
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
finally:
# 关闭数据库连接
db.close()
def create_Table(self,app,*args):
try:
try:
db = MySQLdb.connect(self.host, self.user, self.passwd)
cursor = db.cursor()
cursor.execute('use '+self.db_name)
#print self.db_name
except MySQLdb.Error, e:
#cursor.rollback()
db.rollback()
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
try:
for t in range(len(args)): #不定参数个数
self.table_name = blog_list.Table[t] #获取表名
sql_table="""
CREATE TABLE `""" + self.table_name + """`
(
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
"""
cursor.execute(sql_table)
for t_value in range(len(args[t])): #args[t]为不定参数名
#print len(args[0])
vals=getattr(app,args[t][t_value]) #获取表属性数据类型
sql_add="alter table "+self.table_name+" add column "+args[t][t_value]+" "+vals+" NOT NULL" #为表增加一列
cursor.execute(sql_add)
except MySQLdb.Error, e:
#cursor.rollback()
db.rollback()
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
except MySQLdb.Error,e:
#cursor.rollback()
db.rollback()
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
db.commit()
cursor.close()
db.close()
db = _db()