这是一个创建于 2595 天前的主题,其中的信息可能已经有所发展或是发生改变。
批量添加数据报错:RuntimeError: application not registered on db instance and no application bound to current context
from app import create_app,db
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
from script.hosts import hostinfo
from app.models import Hosts
app = create_app('default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, Hosts=Hosts,)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
hostinfo = hostinfo()
for i in hostinfo.get_host():
for info in i:
info = Hosts(netip=info['PublicIpAddress']['IpAddress'][0],
name=info['InstanceName'],
area=info['RegionId'],
osname=info['OSName'],
host_config='CPU:%s Mem:%s intout:%s' % (info['Cpu'],info['Memory'],info['InternetMaxBandwidthOut']))
db.session.add(info)
db.session.commit()
if __name__ == '__main__':
manager.run()
2 条回复 • 2017-10-27 07:45:22 +08:00
|
|
1
linuxs 2017-10-26 11:09:52 +08:00
hostinfo = hostinfo() for i in hostinfo.get_host(): for info in i: info = Hosts(netip=info['PublicIpAddress']['IpAddress'][0], name=info['InstanceName'], area=info['RegionId'], osname=info['OSName'], host_config='CPU:%s Mem:%s intout:%s' % (info['Cpu'],info['Memory'],info['InternetMaxBandwidthOut'])) db.session.add(info) db.session.commit() 这段是添加数据的代码
|
|
|
2
xlui 2017-10-27 07:45:22 +08:00 via Android
按你这样的写法,在 manager.run() 之前就尝试向数据库添加数据。报 no application bound to current context 大概也是这个原因。 试试看把批量添加数据的操作写成一个命令。
|