Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy:更新&;从数据库中删除值_Python_Python 2.7_Sqlite_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy:更新&;从数据库中删除值

Python SQLAlchemy:更新&;从数据库中删除值,python,python-2.7,sqlite,sqlalchemy,Python,Python 2.7,Sqlite,Sqlalchemy,我是SQLAlchemy的新手,我使用一些示例来创建表并向其中插入信息,它100%工作 但是我没有找到一个例子来说明如何从数据库中更新和删除一些信息 我正在做的是: from sqlalchemy import Column, ForeignKey, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from sqlalch

我是SQLAlchemy的新手,我使用一些示例来创建表并向其中插入信息,它100%工作

但是我没有找到一个例子来说明如何从数据库中更新和删除一些信息

我正在做的是:

from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

Base = declarative_base()

## create
class Person(Base):
    __tablename__ = 'person'
    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)

engine = create_engine('sqlite:///database.db')
Base.metadata.create_all(engine)

## insert

Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)

session = DBSession()
new_person = Person(name='new person')
session.add(new_person)
session.commit()

## fetch
getperson = session.query(Person).first()
print getperson.name

# this will print : new person

# I need some example to how can I update and delete this : new person

因此,在这段代码中,它将打印“new person”,我的问题是如何更新或删除它?

这里有一些关于sqlalchemy中每个CRUD操作的示例(ommiting Create,阅读时您已经知道如何执行这些操作): 首先,为任何操作进行必要的导入和配置:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# Category, Item, User are my tables
from database_setup import Base, Category, Item, User

# Generating session to connect to the db's ORM
engine = create_engine('sqlite:///catalogwithusers.db') # my db
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
session = DBSession()
然后进行更新:

# Get the item filtering by it's id using a one() query on Item table
# If query is not empty, update the attributes, add query to session and commit

q = session.query(Item).filter_by(id=item_id).one()
if q != []:
    q.name = edited_name
    q.description = edited_description
    session.add(q)
    session.commit()
最后,执行删除:

# Again get the item similarly to the example above
# Then if query returned results, use the delete method and commit
q = session.query(Item).filter_by(id=item_id).one()
if q != []:
    session.delete(q)
    session.commit()

这些例子摘自。我建议你看看。ORM创建在database_setup.py内,CRUD操作在project.py和populatecatalog.py内执行。

以下是有关sqlalchemy中每个CRUD操作的示例(ommiting Create,读起来您已经知道如何执行这些操作): 首先,为任何操作进行必要的导入和配置:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# Category, Item, User are my tables
from database_setup import Base, Category, Item, User

# Generating session to connect to the db's ORM
engine = create_engine('sqlite:///catalogwithusers.db') # my db
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
session = DBSession()
然后进行更新:

# Get the item filtering by it's id using a one() query on Item table
# If query is not empty, update the attributes, add query to session and commit

q = session.query(Item).filter_by(id=item_id).one()
if q != []:
    q.name = edited_name
    q.description = edited_description
    session.add(q)
    session.commit()
最后,执行删除:

# Again get the item similarly to the example above
# Then if query returned results, use the delete method and commit
q = session.query(Item).filter_by(id=item_id).one()
if q != []:
    session.delete(q)
    session.commit()

这些例子摘自。我建议你看看。ORM创建在database_setup.py中,CRUD操作在project.py和populatecatalog.py中执行。

您能澄清一下“更新和删除”是什么意思吗?这似乎相当于“删除”;数据以任何一种方式消失了。请看这里(他们讨论了使用原始sql和execute-似乎这对您很有用):您能澄清一下“更新和删除”是什么意思吗?这似乎相当于“删除”;数据以任何一种方式消失了。请参见此处(他们讨论了使用原始sql和execute-似乎这对您很有用):