[英]What is the type of table_name in select_from(table_name)?
I'm trying to get the number of rows from a SQL Server which consists of many tables by looping through it to see how much data they contain. 我试图通过遍历SQL Server以查看其中包含多少数据来从包含许多表的SQL Server中获取行数。 However, I'm not sure what will go into select_from() function.
但是,我不确定select_from()函数中将使用什么内容。 As I currently supply Unicode for table names and it raised
正如我目前为表名提供Unicode一样,它提出了
NoInspectionAvailable: No inspection system is available for object of type <type 'unicode'>
The code that I used was 我使用的代码是
from sqlalchemy import create_engine
import urllib
from sqlalchemy import inspect
import sqlalchemy
from sqlalchemy import select, func, Integer, Table, Column, MetaData
from sqlalchemy.orm import sessionmaker
connection_string = "DRIVER={SQL Server}; *hidden*"
connection_string = urllib.quote_plus(connection_string)
connection_string = "mssql+pyodbc:///?odbc_connect=%s" % connection_string
engine = sqlalchemy.create_engine(connection_string)
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
connection = engine.connect()
inspector = inspect(engine)
for table_name in inspector.get_table_names():
print session.query(func.count('*')).select_from(table_name).scalar()
Typically, it's a class name that refers to a class that describes the database table. 通常,它是一个类名称,它引用描述数据库表的类。
In the sqlalchemy docs, http://docs.sqlalchemy.org/en/latest/orm/tutorial.html , they have you create a base class using declarative base and then create child classes for each table you want to query. 在sqlalchemy文档http://docs.sqlalchemy.org/en/latest/orm/tutorial.html中 ,他们让您使用声明性基类创建基类,然后为要查询的每个表创建子类。 You would then pass that class name into the select_from function unquoted.
然后,您可以将该类名不加引用地传递给select_from函数。
The Flask framework provides a built-in base class that is ready for use called db.Model and Django has one called models.Model. Flask框架提供了一个内置的基类,可以立即使用,名为db.Model,而Django有一个名为models.Model的基类。
Alternatively, you can also pass queries. 或者,您也可以传递查询。 I use the Flask framework typically for python so I usually initiate queries like this:
我通常将Flask框架用于python,因此通常会这样发起查询:
my_qry = db.session.query(Cust).filter(Cust, Cust.cust == 'lolz')
results = my_qry.all()
On a side note, if you decide to look at .NET they also have nice ORMs. 附带一提,如果您决定查看.NET,则它们也有不错的ORM。 Personally, I favor Entity Framework, but Linq to SQL is out there, too.
就个人而言,我更喜欢实体框架,但是Linq to SQL也在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.