mysql 导入现有数据库_如何将现有的MySQL数据库导入web2py?

我遇到了同样的问题,下面的函数是在中创建的appadmin.py自动创建表定义的步骤get_db():

import re

datyp = dict(

bit='boolean', tinyint='boolean',

smallint='integer', mediumint='integer', int='integer', bigint='bigint',

decimal='decimal', double='double', float='double',

char='password', varchar='string',

longtext='list:string', mediumtext='text', text='text', tinytext='text',

enum='json',

timestamp='datetime', datetime='datetime', date='date', time='time',

year='integer',

tinyblob='blob', blob='blob', mediumblob='blob', longblob='blob',

binary='blob', varbinary='blob'

# upload reference list:string list:integer list:reference

# json big-id big-reference

)

tdat = []

tbls = [t[0] for t in db.executesql('SHOW TABLES;')]

for tbl in tbls:

cols = [k.strip() for k in db.executesql(

'SHOW CREATE TABLE `' + tbl + '`;')[0][1].split('\n')]

fname = OrderedDict()

for col in cols:

if col.startswith('KEY'):

continue

coln = re.findall('`([^`]*)`', col)

if (col.startswith('`')):

st = col.index(' ') + 1

typ = col[st:col.index('(' if '(' in col else

(' ' if ' ' in col[st:] else ','), st)]

ft = datyp[typ]

if (ft == 'string'):

st = col.index('(', st) + 1

ft = "'" + ft + "', length=" + col[st:col.index(')', st)]

elif (ft == 'decimal'):

st = col.index('(', st)

ft = "'" + ft + col[st:col.index(')', st) + 1] + "'"

else:

ft = "'" + ft + "'"

if (not "DEFAULT NULL" in col) and ("'" in col):

st = col.index("DEFAULT '") + len("DEFAULT '")

ft += ', default=' + col[st:col.index("'", st)]

if 'NOT NULL' in col:

ft += ', required=True'

fname.update({coln[0]: ft})

continue

if (len(coln) == 2 and col.startswith('UNIQUE KEY')):

fname[coln[1]] += ', unique=True'

continue

if (len(coln) == 4):

sval = fname[coln[1]]

if 'integer' in sval:

sval = sval.replace('integer', 'reference db.' + coln[2])

elif 'bigint' in sval:

sval = sval.replace('bigint',

'big-reference db.' + coln[2])

fname[coln[1]] = sval

if ('ON DELETE ' in col) and (not 'CASCADE' in col):

st = col.index("ON DELETE ") + len("ON DELETE ")

sval = ', ondelete="' + col[st:col.index(" ON", st)] + '"'

fname[coln[1]] += sval

colstr = ["db.define_table('" + tbl + "',"]

for key, val in fname.items():

if (key == 'id'):

continue

colstr.append("Field('" + key + "', " + val + "),")

colstr.append('migrate=False)')

tdat.append(colstr)

response.view = 'default/index.html'

return dict(msg=tdat)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值