📌  相关文章
📜  如何在 SQLAlchemy 查询中将日期时间更改为字符串?

📅  最后修改于: 2022-05-13 01:55:26.336000             🧑  作者: Mango

如何在 SQLAlchemy 查询中将日期时间更改为字符串?

在本文中,我们将在Python编程语言的 sqlalchemy 查询中将 DateTime 更改为字符串。

使用的数据库

安装

安装 sqlalchemy 和 pymysql 的语法:

pip install sqlalchmey pymysql

注意: pymysql 是 sqlalchemy 的依赖项,我们需要为这篇文章安装它

首先,我们需要导入模块并将其连接到数据库。

Python3
from sqlalchemy import create_engine
  
user, password, host, database = 'root', '123', 'localhost', 'geeksforgeeks'
engine = create_engine(
    url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8')
  
connection = engine.connect()
  
table_name = 'to_do_list'


Python3
from sqlalchemy import create_engine
  
user, password, host, database = 'root', '123', 'localhost', 'geeksforgeeks'
engine = create_engine(
    url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8')
  
connection = engine.connect()
  
table_name = 'to_do_list'
  
query = f'SELECT CONVERT(start_datetime,char) FROM {table_name}'
result = connection.execute(query)
  
for elem in result:
    value = elem[0]
    print(value, type(value))


Python3
from sqlalchemy import create_engine
  
user, password, host, database = 'root', '123', 'localhost', 'geeksforgeeks'
engine = create_engine(
    url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8')
  
connection = engine.connect()
  
table_name = 'to_do_list'
  
query = f'SELECT start_datetime FROM {table_name}'
result = connection.execute(query)
  
for elem in result:
    value = elem[0]
    print(value, type(value))
    converted_value = str(value)
    print(converted_value, type(converted_value))
    # just a line for much more readable output
    print("____________________________\n")


方法一:使用SQL的convert函数

因此,SQL 有一个转换函数,可用于更改列的数据类型。

该函数的语法是:

CONVERT(column_name , datatype)

这会将列转换为给定的数据类型并相应地返回值。

SQL 查询将如下所示:

SELECT CONVERT(column_name , CHAR) FROM table_name;

我们正在转换为 CHAR,因为它是 SQL 中字符串的数据类型之一。

例子:

Python3

from sqlalchemy import create_engine
  
user, password, host, database = 'root', '123', 'localhost', 'geeksforgeeks'
engine = create_engine(
    url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8')
  
connection = engine.connect()
  
table_name = 'to_do_list'
  
query = f'SELECT CONVERT(start_datetime,char) FROM {table_name}'
result = connection.execute(query)
  
for elem in result:
    value = elem[0]
    print(value, type(value))

输出:

方法2:使用Python的str函数:

在这个方法中,我们将编写一个普通的 SQL SELECT 查询,执行它,获取结果,然后应用 str函数来改变值。

例子:

Python3

from sqlalchemy import create_engine
  
user, password, host, database = 'root', '123', 'localhost', 'geeksforgeeks'
engine = create_engine(
    url=f'mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8')
  
connection = engine.connect()
  
table_name = 'to_do_list'
  
query = f'SELECT start_datetime FROM {table_name}'
result = connection.execute(query)
  
for elem in result:
    value = elem[0]
    print(value, type(value))
    converted_value = str(value)
    print(converted_value, type(converted_value))
    # just a line for much more readable output
    print("____________________________\n")

输出: