如果您使用的是烧瓶,更新多个字段所需要做的就是实例化数据库类并提交会话(会话将为您节省大量时间,请在此处查看)
例如,您的数据库类名称是事件,字段event_name, start_date, end_date and venue
与会话:
def updateEventData():
ID = input('Enter ID of row you\'d like to update: ')
eventname = input('\nPlease enter event name: ')
startdate = input('\nPlease enter event start date: (DD/MM/YYYY): ')
enddate = input('\nPlease enter event end date: (DD/MM/YYYY): ')
venue = input('\nPlease enter event venue: ')
events = Events(event_name=eventname, start_date=startdate, end_date=enddate, venue=venue)
events.session.commit()
要实例化会话,您需要 sqlalchemy,只需导入sessionmaker
from即可。完整代码:sqlalchemy.orm
create_engine
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
class Foo(object):
engine = create_engine('sqlite:///foo.sqlite', echo = False)
# create new session object for queries
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
def updateEventData():
ID = input('Enter ID of row you\'d like to update: ')
eventname = input('\nPlease enter event name: ')
startdate = input('\nPlease enter event start date: (DD/MM/YYYY): ')
enddate = input('\nPlease enter event end date: (DD/MM/YYYY): ')
venue = input('\nPlease enter event venue: ')
events = Events(event_name=eventname, start_date=startdate, end_date=enddate, venue=venue)
events.session.add(events)
events.session.commit()