注意:本教程基于python3.9捧挺,不一定適用于2.x版本器虾。
需要用到pymysql標(biāo)準(zhǔn)庫(kù)
pip3 install pymysql
import pymysql.cursors
# 創(chuàng)建SQL連接
connect = pymysql.Connect(
host='',
port=3306,
user='',
passwd='',
db='',
charset='utf8'
)
# 獲取游標(biāo)(帶字段名輸出)
cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
# 獲取游標(biāo)(僅輸出數(shù)據(jù))
# cursor = connect.cursor()
goodsId = ' '
# SQL文(增刪改查巴拉巴拉的一些操作)
querySQL = f'SELECT id,created_at FROM product_goods_stock_bill_item where goods_id = "{goodsId}" AND deleted = false ORDER BY check_in_out_date_time ASC'
# 執(zhí)行SQL文
cursor.execute(querySQL)
# 獲取數(shù)據(jù)
result = cursor.fetchall()
for row in result:
print(row)
# 關(guān)閉游標(biāo)
cursor.close()
# 關(guān)閉連接
connect.close()
# 帶字段名輸出
{'id': 'C89YZV684LCZX5PR', 'created_at': datetime.datetime(2021, 1, 4, 9, 8, 2)}
{'id': 'C89ZFIWQEJ5K4VQE', 'created_at': datetime.datetime(2021, 1, 4, 9, 28, 29)}
{'id': 'C89ZQ5P3KW1Q4ORT', 'created_at': datetime.datetime(2021, 1, 4, 9, 42, 22)}
{'id': 'C8A1KLBNAC4TT490', 'created_at': datetime.datetime(2021, 1, 4, 11, 9, 8)}
{'id': 'C8A93R4GGIUNJGMZ', 'created_at': datetime.datetime(2021, 1, 4, 17, 3, 17)}
# 僅數(shù)據(jù)輸出
('C89YZV684LCZX5PR', datetime.datetime(2021, 1, 4, 9, 8, 2))
('C89ZFIWQEJ5K4VQE', datetime.datetime(2021, 1, 4, 9, 28, 29))
('C89ZQ5P3KW1Q4ORT', datetime.datetime(2021, 1, 4, 9, 42, 22))
('C8A1KLBNAC4TT490', datetime.datetime(2021, 1, 4, 11, 9, 8))
('C8A93R4GGIUNJGMZ', datetime.datetime(2021, 1, 4, 17, 3, 17))