英文:
Advantages of using sqlalchemy.types over sqlalchemy.dialects.mssql
问题
在使用SQLAlchemy保存到SQL Server数据库时,除了可移植性之外,使用sqlalchemy.types
比使用sqlalchemy.dialects.mssql
设置数据类型是否有优势?
我正在使用以下代码将数据保存到数据库中(利用了pandas的to_sql()
函数):
df_members.to_sql(
'parliament.member_' + extract_date + '',
schema='source',
con=engine,
index=False,
dtype={
'id': sqlalchemy.dialects.mssql.SMALLINT(),
'gender': sqlalchemy.dialects.mssql.NVARCHAR(1),
'nameDisplayAs': sqlalchemy.dialects.mssql.NVARCHAR(length=256),
'nameClean': sqlalchemy.dialects.mssql.NVARCHAR(length=256),
'count': sqlalchemy.dialects.mssql.TINYINT()
},
)
我注意到以下代码也会产生相同的结果:
df_members.to_sql(
'parliament.member_' + extract_date + '',
schema='source',
con=engine,
index=False,
dtype={
'id': sqlalchemy.types.SMALLINT(),
'gender': sqlalchemy.types.NVARCHAR(1),
'nameDisplayAs': sqlalchemy.types.NVARCHAR(length=256),
'nameClean': sqlalchemy.types.NVARCHAR(length=256),
'count': sqlalchemy.dialects.mssql.TINYINT()
},
)
我已经养成了使用前者的习惯,因为这样我就不必记住哪些数据类型是特定于供应商的(例如,TINYINT()
),哪些不是。
但是,假设可移植性不是一个问题(如果将来更改供应商,代码仍然需要更新以删除TINYINT()
引用),这种方法是否存在缺点,比如速度?
英文:
When saving to a SQL Server database using SQLAlchemy are there advantages to setting datatypes using sqlalchemy.types
over sqlalchemy.dialects.mssql
besides portability?
I'm saving data to a database using the following (making use of pandas' to_sql()
):
df_members.to_sql(
'parliament.member_' + extract_date + '',
schema='source',
con=engine,
index=False,
dtype={
'id': sqlalchemy.dialects.mssql.SMALLINT(),
'gender': sqlalchemy.dialects.mssql.NVARCHAR(1),
'nameDisplayAs': sqlalchemy.dialects.mssql.NVARCHAR(length=256),
'nameClean': sqlalchemy.dialects.mssql.NVARCHAR(length=256),
'count': sqlalchemy.dialects.mssql.TINYINT()
},
)
I note that the following yields the same results:
df_members.to_sql(
'parliament.member_' + extract_date + '',
schema='source',
con=engine,
index=False,
dtype={
'id': sqlalchemy.types.SMALLINT(),
'gender': sqlalchemy.types.NVARCHAR(1),
'nameDisplayAs': sqlalchemy.types.NVARCHAR(length=256),
'nameClean': sqlalchemy.types.NVARCHAR(length=256),
'count': sqlalchemy.dialects.mssql.TINYINT()
},
)
I've got in the habit of using the former because it saves me having to remember which datatypes are vendor-specific (e.g. TINYINT()
) and which aren't.
But, assuming portability isn't a concern (if we ever changed vendor the code would need updating anyway to remove the TINYINT()
references) are there disadvantages to this approach - for example, speed?
答案1
得分: 1
以下是代码部分的翻译:
在 sqlalchemy/lib/sqlalchemy/dialects/mssql/init.py 中:
from .base import SMALLINT
在 sqlalchemy/lib/sqlalchemy/dialects/mssql/base.py 中:
from ...types import SMALLINT
在 sqlalchemy/lib/sqlalchemy/types.py 中:
from .sql.sqltypes import SMALLINT as SMALLINT
继续查看:
在 sqlalchemy/lib/sqlalchemy/sql/sqltypes.py 中:
class SMALLINT(SmallInteger):
"""SQL SMALLINT类型。
.. 参见::
:class:`_types.SmallInteger` - 基本类型的文档。
"""
__visit_name__ = "SMALLINT"
因此,对于许多(甚至所有?)大写类型,使用 sqlalchemy.types.XXX
或 sqlalchemy.dialects.YYY.XXX
看起来是相同的。所有大写数据类型都继承自驼峰大小写的数据类型(参考这里)。
英文:
I don't know if this answer to your question but using the source code:
In sqlalchemy/lib/sqlalchemy/dialects/mssql/init.py:
from .base import SMALLINT
In sqlalchemy/lib/sqlalchemy/dialects/mssql/base.py
from ...types import SMALLINT
In sqlalchemy/lib/sqlalchemy/types.py <<< sqlalchemy.types.SMALLINT
from .sql.sqltypes import SMALLINT as SMALLINT
Go further
In sqlalchemy/lib/sqlalchemy/sql/sqltypes.py
class SMALLINT(SmallInteger):
"""The SQL SMALLINT type.
.. seealso::
:class:`_types.SmallInteger` - documentation for the base type.
"""
__visit_name__ = "SMALLINT"
So for many (all?) UPPERCASE types, using sqlalchemy.types.XXX
or sqlalchemy.dialects.YYY.XXX
seems to be the same thing. All UPPERCASE datatypes are inherited from CamelCase datatypes (ref)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论