SQLalchemy連線資料庫

2021-09-25 22:54:27 字數 3086 閱讀 1919

1、連線命令

from sqlalchemy import create_engine

from sqlite3 import dbapi2 as sqlite

engine = create_engine('mysql+mysqldb://username:password@hostname:3306/database?charset=utf8')

engine = create_engine('mysql+pymysql://username:password@hostname:3306/database?charset=utf8')

engine = create_engine('sqlite+pysqlite:/', module=sqlite)

#連線posgresql資料庫

engine = create_engine('postgres://user:password@hostname/database')

#擴充套件當passwd包含"@"的時候這串url就沒法正常解析了

python2

from urllib import quote_plus as urlquote

from sqlalchemy.engine import create_engine

engine = create_engine('mysql+pymysql://username:%s@hostname:3306/database?charset=utf8' % urlquote('bad@pass'))

python3

from urllib import parse

from sqlalchemy.engine import create_engine

engine = create_engine('mysql+pymysql://username:%s@hostname:3306/database?charset=utf8' % parse.unquote_plus('bad@pass'))

更多詳情請檢視官網:

2、連線超時和斷開

mysql有乙個自動的連線關閉行為,用於固定時間空閒的連線,預設為8小時。要避免出現此問題,請使用create_engine.pool_recycle選項,該選項確保如果連線在池中存在固定的秒數,則將丟棄該連線並替換為新連線

from sqlalchemy.engine import create_engine

engine = create_engine('mysql+pymysql://username:password@hostname:3306/database?charset=utf8',pool_recycle=3600)

3、處理二進位制資料警告和unicode

當試圖將二進位制資料傳遞到資料庫時,mysql 5.6、5.7及更高版本(撰寫本文時不是mariadb)現在發出警告,而當二進位制資料本身對該編碼無效時,字符集編碼也就位

from sqlalchemy.engine import create_engine

# mysqlclient

engine = create_engine("mysql+mysqldb://username:password@hostname:3306/database?charset=utf8mb4&binary_prefix=true")

# pymysql

engine = create_engine("mysql+pymysql://username:password@hostname:3306/database?charset=utf8mb4&binary_prefix=true")

備註:其他mysql驅動程式可能支援或不支援binary_prefix標誌。

4、pandas批量將資料庫資料儲存到excel或者csv檔案中
import pandas as pd

import re

from sqlalchemy import create_engine

name = '大王叫我來巡山'

engine = create_engine('mysql+mysqldb://username:password@hostname:3306/database?charset=utf8', echo=false)

data = engine.execute('select d.id,d.store_name,d.create_time,d.type,d.status,item.product_name,item.retail_price,item.sale_price,item.amount from ds_order d left join order_item item on d.id = item.order_id where d.company_name="%s";' %name)

df = pd.dataframe(data=data,columns=['訂單號','門店名稱','訂單建立時間','訂單型別','訂單狀態','商品名稱','商品現價','商品零售價','銷售數量'])

#替換df['訂單狀態']=df['訂單狀態'].map(lambda s:re.sub('paid','已支付',s))

df['訂單狀態']=df['訂單狀態'].map(lambda s:re.sub('refund','已退款',s))

#儲存df.to_excel(f'.xls',index=false)

5、pandas批量將excel或者csv檔案資料匯入至資料庫
import pandas as pd

from sqlalchemy import create_engine

engine = create_engine('mysql+mysqldb://username:password@hostname:3306/database?charset=utf8', echo=false)

df = pd.read_excel('大王叫我來巡山.xlsx',encoding='utf-8',sheet_name='sheet1')

pd.dataframe(df,

columns=['訂單號','門店名稱','訂單建立時間','訂單型別','訂單狀態','商品名稱','商品現價','商品零售價','銷售數量']

SQLAlchemy連線資料庫建立表

連線資料庫,建立表 defcreate all engine create engine mysql pymysql max overflow 0,超過連線池大小外最多建立的連線 pool size 5,連線池大小 pool timeout 30,池中沒有執行緒最多等待的時間,否則報錯 pool r...

連線資料庫

2 documents 目錄就是我們可以用來寫入並儲存檔案得地方,一般可通過 nsarray paths nssearchpathfordirectoriesindomains nsdocumentdirectory,nsuserdomainmask,yes nsstring documentsdi...

連線資料庫

1 oracle8 8i 9i資料庫 thin模式 class.forname oracle.jdbc.driver.oracledriver newinstance string url jdbc oracle thin localhost 1521 orcl orcl為資料庫的sid strin...