如何用Python做基本的CRUD应用程序 [英] How to do basic CRUD apps with Python

查看:86
本文介绍了如何用Python做基本的CRUD应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP,库,应用程序等来做基本的CRUD无处不在。 Ajax

和非Ajax解决方案比比皆是。


使用Python,找到这样的库或应用程序。似乎更多

很难找到。


我以为django可能是一个好方法,但我似乎无法得到一个

在该主板上回答。


我想用可编辑/删除/

可添加字段组合一个CRUD网格,点击标题排序。可能会有点像在线电子表格的东西。如果可以在线编辑

字段会很好,但这并不是完全必要的。


是否有任何Python库可以做诸如此类的事情?可以用django或cherrypy来完成

吗?


请不要宣传您的PHP / Ajax应用程序。

解决方案



2007年5月13日下午6:20,walterbyrd写道:


使用PHP,库,应用程序等来做基本的CRUD无处不在。 Ajax

和非Ajax解决方案比比皆是。


使用Python,找到这样的库或应用程序。似乎更多

很难找到。


我以为django可能是一个好方法,但我似乎无法得到一个

在该主板上回答。


我想用可编辑/删除/

可添加字段组合一个CRUD网格,点击标题排序。可能会有点像在线电子表格的东西。如果可以在线编辑

字段会很好,但这并不是完全必要的。


是否有任何Python库可以做诸如此类的事情?可以用django或者cherrypy来完成吗?


请不要宣传您的PHP / Ajax应用程序。



你有django-users的答案!最好的恕我直言是来自

dballanc:Django很适合提供后端,但你的大部分

功能可能都是由ajax / javascript提供的

django会愉快地与之沟通,但不会提供。


Django提供了一个ORM,但你不必使用它。如果你愿意,

就可以像使用

php一样直接连接到你的数据库。我实际上已经这样做了,因为对于使用CRM应用程序的ORM来说,感觉不对这是一件好事。




walterbyrd scris:


使用PHP,库,应用程序等做基本的CRUD无处不在。 Ajax

和非Ajax解决方案比比皆是。


使用Python,找到这样的库或应用程序。似乎更多

很难找到。


我以为django可能是一个好方法,但我似乎无法得到一个

在该主板上回答。


我想用可编辑/删除/

可添加字段组合一个CRUD网格,点击标题排序。可能会有点像在线电子表格的东西。如果可以在线编辑

字段会很好,但这并不是完全必要的。


是否有任何Python库可以做诸如此类的事情?可以用django或者cherrypy来完成吗?


请不要宣传您的PHP / Ajax应用程序。



从SqlAlchemy尝试SqlSoup。我可以在PyQt4中发送示例。




walterbyrd scris:


使用PHP,库,应用程序等来做基本的CRUD无处不在。 Ajax

和非Ajax解决方案比比皆是。


使用Python,找到这样的库或应用程序。似乎更多

很难找到。


我以为django可能是一个好方法,但我似乎无法得到一个

在该主板上回答。


我想用可编辑/删除/

可添加字段组合一个CRUD网格,点击标题排序。可能会有点像在线电子表格的东西。如果可以在线编辑

字段会很好,但这并不是完全必要的。


是否有任何Python库可以做诸如此类的事情?可以用django或者cherrypy来完成吗?


请不要宣传您的PHP / Ajax应用程序。



SqlAlchemy-SqlSoup示例:

#SqlSoup。带有一个表的CRUD
来自sqlalchemy.ext.sqlsoup的
导入SqlSoup

#connection:''postgres:// user:password @ address:port / db_name''

db = SqlSoup(''postgres:// postgres:postgres @ localhost:5432 / testdb'')


#读取数据

person = db.person.select()

打印人


#index与主键不一样!!!

print person [0] .firstname


#在第一个名字写下

person [0] .firstname =" George"


#有效写入

db.flush()


打印人[0]


print db.person.count()


for i in range(0,db.person.count()):

print person [i ]


db.person.insert(id = 1000,firstname =''Mitu'')

db.flush


#插入后,重新加载映射:

person = db.person.select()


#delete:

#记录选择

mk = db.person.selectone_by(id = 1000)

#删除

db.delete(mk)

db.flush()


person = db.person.select()


打印人

"""

来自文件:


=======

SqlSoup

===== ==


简介


SqlSoup提供了一种访问数据库表的便捷方式,而不需要

>
提前声明表或映射器类。


假设我们有一个包含用户,书籍和贷款表的数据库

(对应于

PyW​​ebOff数据集,如果你很好奇的话。

出于测试目的,我们将按如下方式创建这个数据库:


>>来自sqlalchemy import create_engine
e = create_engine('sqlite:///:memory: '')
对于_testsql中的sql:e.execute(sql)#do ctest:+ ELLIPSIS



< ...


创建SqlSoup网关是就像创建一个SqlAlchemy引擎一样:


>>来自sqlalchemy.ext.sqlsoup import SqlSoup
db = SqlSoup(''sqlite:///:memory:'')



或者,你可以重用现有的元数据:


>> db = SqlSoup(BoundMetaData(e ))



您可以选择在数据库中为您的指定指定

SqlSoup:


#>> db.schema = myschemaname


装货对象


装货对象一样容易如下:


>> users = db.users.select()
users.sort()
用户



[MappedUsers(name =''Joe Student'',em************ @ example.edu'',

password =''student'',classname = None,admin = 0),

MappedUsers(name =''Bhargan Basepair'',em *********** **@example.edu'',

密码=''basepair'',classname =无,admin = 1)]


当然,让数据库做的排序更好

(" .c"是.columns的缩写:


>> db.users。 select(order_by = [db.users.c.name])



[MappedUsers(name =''Bhargan Basepair'', em*************@example.edu'',

密码=''basepair'',classname =无,admin = 1),

MappedUsers(name =''Joe Student'',em************ @ example.edu'',

密码=''学生'' ,classname = None,admin = 0)]

字段访问直观:


>> users [0] .email



u''**** ***@example.edu''


当然,你不想经常加载所有用户。

让我们添加一个WHERE子句。

让我们也切换order_b当我们在它的时候到DESC。


>>来自sqlalchemy import or_,and_,desc
where = or_(db.users.c.name ==''Bhargan Basepair'',



db************************@example.edu'')


>> db.users.select(其中,order_by = [desc(db.users.c.name)])



[MappedUsers(name =''Joe Student'',em************ @ example.edu'',

密码=''学生'',classname =无,admin = 0),

MappedUsers(name =''Bhargan Basepair'',em ***** ********@example.edu'',

密码=''basepair'',classname =无,admin = 1)]


如果您要查询

,也可以使用select ... by方法单列。

这允许使用关键字参数作为列名:


>> db.users.selectone_by(name =''Bhargan Basepair'')



MappedUsers(name =' 'Bhargan Basepair'',em************ * @ example.edu'',

password =''basepair'',classname = None,admin = 1 )


选择变体


所有SqlAlchemy查询选择变体都可用。

这里是一个快速摘要这些方法:


* get(PK):加载由其主键标识的单个对象

(标量或元组)

* select(Clause,** kwargs):执行由

条款限制的选择

参数;返回一个对象列表。

最常见的子句参数采用

" db.tablename.c.columname == value。的形式。

最常见的可选参数是order_by。

* select_by(** params):选择以_by结尾的方法允许使用



列名称。 (columname = value)对于大多数Python

程序员来说,这感觉更自然。缺点是你不能指定order_by或

其他

选择选项。

* selectfirst,selectfirst_by:只返回第一个对象<找到
;

相当于select(...)[0]或select_by(...)[0],除了没有

是如果没有选择行,则返回



* selectone,selectone_by:喜欢selectfirst或selectfirst_by,但

加价

如果选择的对象少于或多于一个。

* count,count_by:返回所选行的整数计数。


请参阅SqlAlchemy文档详情:


* http ://www.sqlalchemy.org/docs/datam...amapping_query

了解一般信息和示例,

* http://www.sqlalchemy.org/docs/sqlconstruction.myt

有关构造WHERE子句的详细信息。


修改对象


修改对象非常直观:


>> user = _
user.email =''b ****** ********@example.edu''
db.flush()



(SqlSoup利用率复杂的SqlAlchemy工作单元代码,所以

多个

对单个对象的更新将变成单个UPDATE

语句<刷新时
。)


要完成基本工作,让我们插入一笔新贷款,然后删除

it :


>> book_id = db.books.selectfirst(db.books.c .title ==''苔藓中的区域变化''。id
db.loans.insert(book_id = book_id,user_name = user.name)



MappedLoans(book_id = 2,user_name =''Bhargan Basepair'',loan_date = None)


>> db.flush()


>> loan = db.loans.selectone_by(book_id = 2,user_name =''Bhargan



Basepair'')


>> db.delete(贷款)
db.flush()



你也可以删除尚未作为对象加载的行。

让我们再次执行插入/删除循环,这次使用贷款

table''s

删除方法。 (对于SQLAlchemy专家:请注意,没有flush()调用

需要

,因为此删除操作在SQL级别,而不是在Mapper级别。)

这里适用的where子句构造规则适用于选择

方法。


>> db.loans.insert(book_id = book_id,user_name = user.name)



MappedLoans(book_id = 2,user_name =''Bhargan Basepair'',loan_date =无)


>> db.flush()
db.loans.delete(db.loans.c.book_id == 2)



您可以同样更新多行。

这将在book_id为2的所有贷款中将book_id更改为1:


>> db.loans.update(db.loans.c.book_id == 2,book_id = 1)
db.loans.select_by(db.loans。 c.book_id == 1)



[MappedLoans(book_id = 1,user_name =''Joe

学生'',loan_date = datetime.datetime(2006,

7,

12,0,0))]


加入


偶尔,你会想要从相关的

表格中提取大量数据

。在这种情况下,使用
数据库

执行必要的连接效率要高得多。 (这里我们没有很多数据,但是

希望

这个概念仍然很清楚。)SQLAlchemy足够聪明地认识到

贷款

有一个外键供用户使用,并自动将其用作加入条件



>> join1 = db.join(db.users,db.loans,isouter = True)
join1.select_by( name =''Joe Student'')



[MappedJoin(name =''Joe Student'',em **** ********@example.edu'',

密码=''学生'',classname =无,admin = 0,book_id = 1,

user_name =''Joe Student'',loan_date = datetime.datetime(2006,7,

12,0,0))]


如果你'不幸的是,使用MySQL的默认MyISAM

存储

引擎,你必须指定连接条件manuall y,因为

MyISAM确实

不存储外键。

这里再次使用相同的连接,明确连接条件

指定:


>> db.join(db。 users,db.loans,db.users.c.name == db.loans.c.user_name,isouter = True)



< class''sqlalchemy.ext.sqlsoup.MappedJoin''>


您可以通过组合Join对象组合任意复杂的连接

与表

或其他联接。在这里,我们将第一次加入与书籍表结合起来:


>> join2 = db .join(join1,db.books)
join2.select()



[MappedJoin(name =''Joe学生'',em************ @ example.edu'',

密码=''学生'',classname =无,admin = 0,book_id = 1,

user_name =''Joe Student'',loan_date = datetime.datetime(2006,7,12,

0,0),

id = 1,title =''我有芥末

已知'',published_year ='''1989'',authors ='''Jones'')]


如果您连接具有相同列名的表,请使用

" with_labels"包装您的联接

,以消除带有表名的列的歧义:


>> db.with_labels(join1) .select()



[MappedUsersLoansJoin(users_name =''Joe Student'',

users_em * *********** @ example.edu'',users_password =''student'',

users_classname = None,users_admin = 0,loans_book_id = 1,

loans_user_name =''Joe Student'',

loans_loan_date = datetime.datetime(2006,7,12,0,0))]


高级使用

映射任意可选项


SqlSoup可以使用map方法映射任何SQLAlchemy Selectable。

让我们的地图a选择使用聚合函数的对象;我们将使用

SqlSoup作为基础的
SQLAlchemy表。

(因为我们没有映射到一个简单的表或加入,我们需要告诉

SQLAlchemy

如何找到主键,它只需要在$ b $中唯一b select,

并不一定对应数据库中的真实PK。)


>>来自sqlalchemy import select,func
b = db.books._table
s = select([bcpublished_year,func.count(''* '')。label(''n'')],



from_obj = [b],group_by = [bcpublished_year] )


>> s = s.alias(''years_with_count'')
years_with_count = db.map(s,primary_key = [scpublished_年])
years_with_count.select_by(published_year ='''1989'')



[MappedBooks(published_year ='' 1989'',n = 1)]


显然,如果我们只想获得与

预订年度相关的计数清单

一次,原始SQL将减少工作量。映射

Select的优势是

可重用性,无论是独立还是联接。 (如果你去完整的

SQLAlchemy,

你可以直接对你的对象模型执行这样的映射。)


原始SQL


您可以访问SqlSoup的引擎属性直接编写SQL



引擎'' execute方法对应于DBAPI游标之一,

并返回一个ResultProxy,它具有你也可以在

a游标上看到的获取方法。


>> rp = db.engine.execute(''选择姓名,来自用户的电子邮件,请按
排序



name'')


>>用于名称,电子邮件在rp.fetchall()中:打印名称,电子邮件



Bhargan Basepair ba*************@example.edu

Joe S. tudent st*****@example.edu


你也可以通过此引擎对象为其他SQLAlchemy构造。

""


With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it''s not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don''t advertise your PHP/Ajax apps.

解决方案


On May 13, 2007, at 6:20 PM, walterbyrd wrote:

With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it''s not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don''t advertise your PHP/Ajax apps.

You got answers on django-users! The best IMHO was the one from
dballanc: "Django is good for providing the backend, but most of your
functionality is probably going to be provided by ajax/javascript
which django will happily communicate with, but does not provide."

Django provides an ORM, but you don''t have to use it. If you want,
you can connect directly to your database just like you did with
php. I''ve actually done that because something just "feels wrong"
about using an ORM for CRUDy applications.



walterbyrd a scris:

With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it''s not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don''t advertise your PHP/Ajax apps.

Try SqlSoup from SqlAlchemy. I can send examples in PyQt4.



walterbyrd a scris:

With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax
and non-Ajax solutions abound.

With Python, finding such library, or apps. seems to be much more
difficult to find.

I thought django might be a good way, but I can not seem to get an
answer on that board.

I would like to put together a CRUD grid with editable/deletable/
addable fields, click on the headers to sort. Something that would
sort-of looks like an online spreadsheet. It would be nice if the
fields could be edited in-line, but it''s not entirely necessary.

Are there any Python libraries to do that sort of thing? Can it be
done with django or cherrypy?

Please, don''t advertise your PHP/Ajax apps.

SqlAlchemy-SqlSoup Example:
# SqlSoup. CRUD with one table
from sqlalchemy.ext.sqlsoup import SqlSoup
# connection: ''postgres://user:password@address:port/db_name''
db = SqlSoup(''postgres://postgres:postgres@localhost:5432/testdb'')

# read data
person = db.person.select()
print person

# index is not the same with primary key !!!
print person[0].firstname

# write in column firstname
person[0].firstname = "George"

# effective write
db.flush()

print person[0]

print db.person.count()

for i in range(0, db.person.count()):
print person[i]

db.person.insert(id=1000, firstname=''Mitu'')
db.flush

# after insert, reload mapping:
person = db.person.select()

# delete:
# record select
mk = db.person.selectone_by(id=1000)
# delete
db.delete(mk)
db.flush()

person = db.person.select()

print person
"""
FROM DOCUMENTATION:

=======
SqlSoup
=======

Introduction

SqlSoup provides a convenient way to access database tables without
having to
declare table or mapper classes ahead of time.

Suppose we have a database with users, books, and loans tables
(corresponding to
the PyWebOff dataset, if you''re curious).
For testing purposes, we''ll create this db as follows:

>>from sqlalchemy import create_engine
e = create_engine(''sqlite:///:memory:'')
for sql in _testsql: e.execute(sql) #doctest: +ELLIPSIS

<...

Creating a SqlSoup gateway is just like creating an SqlAlchemy engine:

>>from sqlalchemy.ext.sqlsoup import SqlSoup
db = SqlSoup(''sqlite:///:memory:'')

or, you can re-use an existing metadata:

>>db = SqlSoup(BoundMetaData(e))

You can optionally specify a schema within the database for your
SqlSoup:

# >>db.schema = myschemaname

Loading objects

Loading objects is as easy as this:

>>users = db.users.select()
users.sort()
users

[MappedUsers(name=''Joe Student'',em************@example.edu'',
password=''student'',classname=None,admin=0),
MappedUsers(name=''Bhargan Basepair'',em*************@example.edu'',
password=''basepair'',classname=None,admin=1)]

Of course, letting the database do the sort is better
(".c" is short for ".columns"):

>>db.users.select(order_by=[db.users.c.name])

[MappedUsers(name=''Bhargan Basepair'',em*************@example.edu'',
password=''basepair'',classname=None,admin=1),
MappedUsers(name=''Joe Student'',em************@example.edu'',
password=''student'',classname=None,admin=0)]

Field access is intuitive:

>>users[0].email

u''*******@example.edu''

Of course, you don''t want to load all users very often.
Let''s add a WHERE clause.
Let''s also switch the order_by to DESC while we''re at it.

>>from sqlalchemy import or_, and_, desc
where = or_(db.users.c.name==''Bhargan Basepair'',

db************************@example.edu'')

>>db.users.select(where, order_by=[desc(db.users.c.name)])

[MappedUsers(name=''Joe Student'',em************@example.edu'',
password=''student'',classname=None,admin=0),
MappedUsers(name=''Bhargan Basepair'',em*************@example.edu'',
password=''basepair'',classname=None,admin=1)]

You can also use the select...by methods if you''re querying on a
single column.
This allows using keyword arguments as column names:

>>db.users.selectone_by(name=''Bhargan Basepair'')

MappedUsers(name=''Bhargan Basepair'',em*************@example.edu'',
password=''basepair'',classname=None,admin=1)

Select variants

All the SqlAlchemy Query select variants are available.
Here''s a quick summary of these methods:

* get(PK): load a single object identified by its primary key
(either a scalar, or a tuple)
* select(Clause, **kwargs): perform a select restricted by the
Clause
argument; returns a list of objects.
The most common clause argument takes the form
"db.tablename.c.columname == value."
The most common optional argument is order_by.
* select_by(**params): select methods ending with _by allow using
bare
column names. (columname=value) This feels more natural to
most Python
programmers; the downside is you can''t specify order_by or
other
select options.
* selectfirst, selectfirst_by: returns only the first object
found;
equivalent to select(...)[0] or select_by(...)[0], except None
is returned
if no rows are selected.
* selectone, selectone_by: like selectfirst or selectfirst_by, but
raises
if less or more than one object is selected.
* count, count_by: returns an integer count of the rows selected.

See the SqlAlchemy documentation for details:

* http://www.sqlalchemy.org/docs/datam...amapping_query
for general info and examples,
* http://www.sqlalchemy.org/docs/sqlconstruction.myt
for details on constructing WHERE clauses.

Modifying objects

Modifying objects is intuitive:

>>user = _
user.email = ''b**************@example.edu''
db.flush()

(SqlSoup leverages the sophisticated SqlAlchemy unit-of-work code, so
multiple
updates to a single object will be turned into a single UPDATE
statement
when you flush.)

To finish covering the basics, let''s insert a new loan, then delete
it:

>>book_id = db.books.selectfirst(db.books.c.title==''Regional Variation in Moss'').id
db.loans.insert(book_id=book_id, user_name=user.name)

MappedLoans(book_id=2,user_name=''Bhargan Basepair'',loan_date=None)

>>db.flush()

>>loan = db.loans.selectone_by(book_id=2, user_name=''Bhargan

Basepair'')

>>db.delete(loan)
db.flush()

You can also delete rows that have not been loaded as objects.
Let''s do our insert/delete cycle once more, this time using the loans
table''s
delete method. (For SQLAlchemy experts: note that no flush() call is
required
since this delete acts at the SQL level, not at the Mapper level.)
The same where-clause construction rules apply here as to the select
methods.

>>db.loans.insert(book_id=book_id, user_name=user.name)

MappedLoans(book_id=2,user_name=''Bhargan Basepair'',loan_date=None)

>>db.flush()
db.loans.delete(db.loans.c.book_id==2)

You can similarly update multiple rows at once.
This will change the book_id to 1 in all loans whose book_id is 2:

>>db.loans.update(db.loans.c.book_id==2, book_id=1)
db.loans.select_by(db.loans.c.book_id==1)

[MappedLoans(book_id=1,user_name=''Joe
Student'',loan_date=datetime.datetime(2006,
7,
12, 0, 0))]

Joins

Occasionally, you will want to pull out a lot of data from related
tables all
at once. In this situation, it is far more efficient to have the
database
perform the necessary join. (Here we do not have "a lot of data," but
hopefully
the concept is still clear.) SQLAlchemy is smart enough to recognize
that loans
has a foreign key to users, and uses that as the join condition
automatically.

>>join1 = db.join(db.users, db.loans, isouter=True)
join1.select_by(name=''Joe Student'')

[MappedJoin(name=''Joe Student'',em************@example.edu'',
password=''student'',classname=None,admin=0,book_id= 1,
user_name=''Joe Student'',loan_date=datetime.datetime(2006, 7,
12, 0, 0))]

If you''re unfortunate enough to be using MySQL with the default MyISAM
storage
engine, you''ll have to specify the join condition manually, since
MyISAM does
not store foreign keys.
Here''s the same join again, with the join condition explicitly
specified:

>>db.join(db.users, db.loans, db.users.c.name==db.loans.c.user_name, isouter=True)

<class ''sqlalchemy.ext.sqlsoup.MappedJoin''>

You can compose arbitrarily complex joins by combining Join objects
with tables
or other joins. Here we combine our first join with the books table:

>>join2 = db.join(join1, db.books)
join2.select()

[MappedJoin(name=''Joe Student'',em************@example.edu'',
password=''student'',classname=None,admin=0,book_id= 1,
user_name=''Joe Student'',loan_date=datetime.datetime(2006, 7, 12,
0, 0),
id=1,title=''Mustards I Have
Known'',published_year=''1989'',authors=''Jones'')]

If you join tables that have an identical column name, wrap your join
with
"with_labels", to disambiguate columns with their table name:

>>db.with_labels(join1).select()

[MappedUsersLoansJoin(users_name=''Joe Student'',
users_em************@example.edu'',users_password='' student'',
users_classname=None,users_admin=0,loans_book_id=1 ,
loans_user_name=''Joe Student'',
loans_loan_date=datetime.datetime(2006, 7, 12, 0, 0))]

Advanced Use
Mapping arbitrary Selectables

SqlSoup can map any SQLAlchemy Selectable with the map method.
Let''s map a Select object that uses an aggregate function; we''ll use
the
SQLAlchemy Table that SqlSoup introspected as the basis.
(Since we''re not mapping to a simple table or join, we need to tell
SQLAlchemy
how to find the "primary key," which just needs to be unique within
the select,
and not necessarily correspond to a "real" PK in the database.)

>>from sqlalchemy import select, func
b = db.books._table
s = select([b.c.published_year, func.count(''*'').label(''n'')],

from_obj=[b], group_by=[b.c.published_year])

>>s = s.alias(''years_with_count'')
years_with_count = db.map(s, primary_key=[s.c.published_year])
years_with_count.select_by(published_year=''1989'' )

[MappedBooks(published_year=''1989'',n=1)]

Obviously if we just wanted to get a list of counts associated with
book years
once, raw SQL is going to be less work. The advantage of mapping a
Select is
reusability, both standalone and in Joins. (And if you go to full
SQLAlchemy,
you can perform mappings like this directly to your object models.)

Raw SQL

You can access the SqlSoup''s engine attribute to compose SQL
directly.
The engine''s execute method corresponds to the one of a DBAPI cursor,
and returns a ResultProxy that has fetch methods you would also see on
a cursor.

>>rp = db.engine.execute(''select name, email from users order by

name'')

>>for name, email in rp.fetchall(): print name, email

Bhargan Basepair ba*************@example.edu
Joe Student st*****@example.edu

You can also pass this engine object to other SQLAlchemy constructs.
"""


这篇关于如何用Python做基本的CRUD应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆