Edit alterUserTable.py

This commit is contained in:
zhuyj17 2016-08-10 15:39:32 +08:00
parent 1230abdd4a
commit e3bb1e1974
2 changed files with 12 additions and 17 deletions

View File

@ -2,16 +2,23 @@ import sys
sys.path.append("../src/")
from model import db,User
print("Query all users:")
users = User.query.all()
db.drop_all(bind='__all__')
print(users)
setattr(User,'beans',db.Column(db.Integer))
db.create_all(bind='__all__')
newusers = []
print("Copy data to new users and set their beans to 10000...")
for user in users:
newuser = User(user.username,user.password,user.avatar,user.nickname,user.description,user.status,
user.e_mail,user.student_number,user.department,user.truename,user.tel,user.register_date,
user.user_group,user.auth_method)
newuser.beans = 1000
newuser.beans = 10000
newusers.append(newuser)
print("Drop all table...")
db.drop_all(bind='__all__')
print("Create all tables with beans...")
setattr(User,'beans',db.Column(db.Integer))
db.create_all(bind='__all__')
for newuser in newusers:
db.session.add(newuser)
db.session.commit()
print("Update users table successfully!")

View File

@ -1,12 +0,0 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
DB_CONNECT_STRING = "sqlite:////opt/docklet/global/sys/UserTable.db"
engine = create_engine(DB_CONNECT_STRING,echo=True)
DB_Session = sessionmaker(bind=engine)
session = DB_Session()
print(session.execute('Select * from User').fetchall())
#print(session.execute('Alter table User add beans integer'))
#print(session.execute('update User set beans=1000'))
print(session.execute('Select * from User').fetchall())