简体   繁体   English

select_from(table_name)中的table_name是什么类型?

[英]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.

相关问题 如何避免使用“SELECT * FROM {table_name}”进行 SQL 注入? - How to avoid SQL injection with “SELECT * FROM {table_name}”? MySql SELECT * FROM table_name WHERE col_name IN(任何值) - MySql SELECT * FROM table_name WHERE col_name IN (any value) SELECT * FROM table_name仅给我一列作为结果 - SELECT * FROM table_name give me only one column as result SqlAlchemy自动映射不会生成Base.classes。[table_name] - SqlAlchemy automap not generating Base.classes.[table_name] Django Admin隐藏名称并显示(table_name对象) - Django Admin hides names and shows (table_name object) Peewee:在模型实例化时动态设置 table_name - Peewee: dynamically set table_name when model is instanced 从redshift和从python提取DB表蓝图,例如“ describe table_name” commande - Fetch DB tables blueprint like “describe table_name” commande from redshift and DB2 from python 我可以像这样将python的`for`语句与SQL结合:`db.select(&#39;table_name&#39;,where =&#39;...&#39;)中的for id,name,ctime。 - Can I combine python's `for` statement with SQL like this: `for id, name, ctime in db.select('table_name', where='…')` Flask-SQLAlchemy单元测试失败InvalidRequestError:已为此MetaData实例定义表&#39;{table_name}&#39; - Flask-SQLAlchemy unittests failing InvalidRequestError: Table '{table_name}' is already defined for this MetaData instance SQLAlchemy select_from一个表 - SQLAlchemy select_from a single table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM