python基于Pandas讀寫(xiě)MySQL數(shù)據(jù)庫(kù)
要實(shí)現(xiàn) pandas 對(duì) mysql 的讀寫(xiě)需要三個(gè)庫(kù)
pandas sqlalchemy pymysql可能有的同學(xué)會(huì)問(wèn),單獨(dú)用 pymysql 或 sqlalchemy 來(lái)讀寫(xiě)數(shù)據(jù)庫(kù)不香么,為什么要同時(shí)用三個(gè)庫(kù)?主要是使用場(chǎng)景不同,個(gè)人覺(jué)得就大數(shù)據(jù)處理而言,用 pandas 讀寫(xiě)數(shù)據(jù)庫(kù)更加便捷。
1、read_sql_query 讀取 mysqlread_sql_query 或 read_sql 方法傳入?yún)?shù)均為 sql 語(yǔ)句,讀取數(shù)據(jù)庫(kù)后,返回內(nèi)容是 dateframe 對(duì)象。普及一下:dateframe 其實(shí)也是一種數(shù)據(jù)結(jié)構(gòu),類似 excel 表格一樣。
import pandasfrom sqlalchemy import create_engineclass mysqlconn: def __init__(self):mysql_username = ’root’mysql_password = ’123456’# 填寫(xiě)真實(shí)數(shù)庫(kù)ipmysql_ip = ’x.x.x.x’port = 3306db = ’work’# 初始化數(shù)據(jù)庫(kù)連接,使用pymysql庫(kù)self.engine = create_engine(’mysql+pymysql://{}:{}@{}:{}/{}’.format(mysql_username, mysql_password, mysql_ip, port,db)) # 查詢mysql數(shù)據(jù)庫(kù) def query(self,sql):df = pandas.read_sql_query(sql,self.engine)# df = pandas.read_sql(sql,self.engine) 這種讀取方式也可以# 返回dateframe格式return dfif __name__ ==’__main__’: # 查詢的 sql 語(yǔ)句 SQL = ’’’select * from working_time order by id desc ’’’ # 調(diào)用 mysqlconn 類的 query() 方法 df_data = mysqlconn().query(sql=SQL)2、to_sql 寫(xiě)入數(shù)據(jù)庫(kù)
使用 to_sql 方法寫(xiě)入數(shù)據(jù)庫(kù)之前,先把數(shù)據(jù)轉(zhuǎn)化成 dateframe 。
import pandasfrom sqlalchemy import create_engineclass mysqlconn: def __init__(self):mysql_username = ’root’mysql_password = ’123456’# 填寫(xiě)真實(shí)數(shù)庫(kù)ipmysql_ip = ’mysql.mall.svc.test.local’port = 3306db = ’work’# 初始化數(shù)據(jù)庫(kù)連接,使用pymysql庫(kù)self.engine = create_engine(’mysql+pymysql://{}:{}@{}:{}/{}’.format(mysql_username, mysql_password, mysql_ip, port,db)) # 查詢mysql數(shù)據(jù)庫(kù) def query(self,sql):df = pandas.read_sql_query(sql,self.engine)# df = pandas.read_sql(sql,self.engine)# 返回dateframe格式return df # 寫(xiě)入mysql數(shù)據(jù)庫(kù) def to_sql(self,table,df):# 第一個(gè)參數(shù)是表名# if_exists:有三個(gè)值 fail、replace、append# 1.fail:如果表存在,啥也不做# 2.replace:如果表存在,刪了表,再建立一個(gè)新表,把數(shù)據(jù)插入# 3.append:如果表存在,把數(shù)據(jù)插入,如果表不存在創(chuàng)建一個(gè)表!!# index 是否儲(chǔ)存index列df.to_sql(table, con=self.engine, if_exists=’append’, index=False)if __name__ ==’__main__’: # 創(chuàng)建 dateframe 對(duì)象 df = pandas.DataFrame([{’name’:’小米’,’price’:’3999’,’colour’:’白色’},{’name’:’華為’,’price’:’4999’,’colour’:’黑色’}]) # 調(diào)用 mysqlconn 類的 to_sql() 方法 mysqlconn().to_sql(’phonetest’,df)
插入數(shù)據(jù)庫(kù)的數(shù)據(jù):

以上就是python基于Pandas讀寫(xiě)MySQL數(shù)據(jù)庫(kù)的詳細(xì)內(nèi)容,更多關(guān)于Python讀寫(xiě)MySQL數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. XML在語(yǔ)音合成中的應(yīng)用2. 使用php數(shù)據(jù)緩存技術(shù)提高執(zhí)行效率3. Python字符串及文本模式方法詳解4. 關(guān)于html嵌入xml數(shù)據(jù)島如何穿過(guò)樹(shù)形結(jié)構(gòu)關(guān)系的問(wèn)題5. Python unittest基本使用方法代碼實(shí)例6. Python 調(diào)用 ES、Solr、Phoenix的示例代碼7. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)8. python tkiner實(shí)現(xiàn) 一個(gè)小小的圖片翻頁(yè)功能的示例代碼9. python的json包位置及用法總結(jié)10. python如何刪除文件、目錄

網(wǎng)公網(wǎng)安備