029_读取数据库

 

import pandas as pd
import pyodbc
import sqlalchemy

if __name__ == '__main__':
    # 方法一
    connection = pyodbc.connect("driver={sql server}; server=(local); \
                                database=AdventureWorks2012;  user=sa; password=123456")
    query_v1 = "select FirstName, LastName from Person.Person"
    df1 = pd.read_sql_query(query_v1, connection)
    print(df1.head(20))


    # 方法二
    engine = sqlalchemy.create_engine("mssql+pyodbc://sa:123456@(local)/AdventureWorks2012?driver=SQL+Server")
    query_v2 = "select count(FirstName) from Person.Person where FirstName = 'Timothy'"
    df2 = pd.read_sql_query(query_v2, engine)
    print(df2.head(20))

 

posted @ 2021-02-17 21:11  火焰马  阅读(47)  评论(0编辑  收藏  举报