一.資料庫讀取(mysql)
| 引數 | 接受 | 作用 | 默認 |
|---|---|---|---|
| sql or table_name | string | 讀取的表名,或sql陳述句 | 無 |
| con | 資料庫連接 | 資料庫連接資訊 | 無 |
| index_col | Int/sequence/False | 設定列作行名,(一個數列為多重索引) | None |
| coerce_float | Boolean | 將資料庫中decimal型別轉換為pandas的float64 | True |
1.連通器: 連接mysql
'資料庫型別+資料庫驅動名稱://用戶名:口令@機器地址:埠號/資料庫名'
from sqlalchemy import create_engine
連通器=create_engine('mysql+pymysql://root:root密碼@127.0.0.1:3306/庫名?charset=utf8')
示例

2.讀sql
(1)read_sql_query
只能使用sql語言進行讀取操作
pd.read_sql_query('sql語言',con=data_1)
示例

(2)read_sql_table
無法使用sql陳述句
pd.read_sql_table('資料表名',con=連通器)
示例

(3)read_sql
同時兼顧read_sql_table,read_sql_query的操作
pd.read_sql('資料表名',con=連通器)
pd.read_sql('sql語言',con=data_1)
示例


二.mysql寫入
to_sql()
| 引數名 | 接收 | 作用 | 默認 |
|---|---|---|---|
| name | string | 資料庫名 | 無 |
| con | 資料庫鏈接 | 資料庫鏈接資訊 | 無 |
| If_exists | fail/replace/append | Fail同表名取消寫入,replace同圖表名覆寫,append 同表名追加資料 | fail |
| Index | Boolean | 是否將索引作為資料傳入 | true |
| Lindex_label | String/sequence | 是否參考索引名稱 | |
| Dtype | Dict | 寫入資料型別(列名key,資料格式values) | None |
to_sql



轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/237872.html
標籤:Python
下一篇:2.pandas常用讀取
