python3 pandas 讀取MySQL數(shù)據(jù)和插入的實(shí)例
python 代碼如下:
# -*- coding:utf-8 -*-
import pandas as pd
import pymysql
import sys
from sqlalchemy import create_engine
def read_mysql_and_insert():
try:
conn = pymysql.connect(host='localhost',user='user1',password='123456',db='test',charset='utf8')
except pymysql.err.OperationalError as e:
print('Error is '+str(e))
sys.exit()
try:
engine = create_engine('mysql+pymysql://user1:123456@localhost:3306/test')
except sqlalchemy.exc.OperationalError as e:
print('Error is '+str(e))
sys.exit()
except sqlalchemy.exc.InternalError as e:
print('Error is '+str(e))
sys.exit()
try:
sql = 'select * from sum_case'
df = pd.read_sql(sql, con=conn)
except pymysql.err.ProgrammingError as e:
print('Error is '+str(e))
sys.exit()
print(df.head())
df.to_sql(name='sum_case_1',con=engine,if_exists='append',index=False)
conn.close()
print('ok')
if __name__ == '__main__':
df = read_mysql_and_insert()
另外需要注意的還有。
1) test數(shù)據(jù)庫(kù)里有兩個(gè)表,建表語(yǔ)句如下:
CREATE TABLE `sum_case` ( `type_id` tinyint(2) DEFAULT NULL, `type_name` varchar(5) DEFAULT NULL, KEY `b` (`type_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `sum_case_1` ( `type_id` tinyint(2) DEFAULT NULL, `type_name` varchar(5) DEFAULT NULL, KEY `b` (`type_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入初始數(shù)據(jù)
insert into sum_case (type_id,type_name) values (1,'a'),(2,'b'),(3,'c')
2)創(chuàng)建user1用戶
grant select, update,insert on test.* to 'user1'@'localhost' identified by '123456'
以上這篇python3 pandas 讀取MySQL數(shù)據(jù)和插入的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- pandas讀取CSV文件時(shí)查看修改各列的數(shù)據(jù)類型格式
- Pandas讀取行列數(shù)據(jù)最全方法
- python?pandas庫(kù)讀取excel/csv中指定行或列數(shù)據(jù)
- pandas讀取csv格式數(shù)據(jù)時(shí)header參數(shù)設(shè)置方法
- 使用python的pandas讀取excel文件中的數(shù)據(jù)詳情
- 利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的方法
- Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)
- Python如何利用pandas讀取csv數(shù)據(jù)并繪圖
- Python 中pandas索引切片讀取數(shù)據(jù)缺失數(shù)據(jù)處理問(wèn)題
- Pandas讀取外部數(shù)據(jù)的幾種實(shí)現(xiàn)方法
相關(guān)文章
Python2到Python3的遷移過(guò)程中報(bào)錯(cuò)AttributeError: ‘str‘ objec
在 Python 編程過(guò)程中,AttributeError: 'str' object has no attribute 'decode' 是一個(gè)常見(jiàn)的錯(cuò)誤,這通常會(huì)在處理字符串時(shí)出現(xiàn),尤其是在 Python 2 到 Python 3 的遷移過(guò)程中,本文將詳細(xì)介紹該問(wèn)題的根源,并提供解決方案,需要的朋友可以參考下2025-04-04
通過(guò)數(shù)據(jù)庫(kù)向Django模型添加字段的示例
這篇文章主要介紹了通過(guò)數(shù)據(jù)庫(kù)向Django模型添加字段的示例,Django是人氣最高的Python web開發(fā)框架,需要的朋友可以參考下2015-07-07
Python類的繼承、多態(tài)及獲取對(duì)象信息操作詳解
這篇文章主要介紹了Python類的繼承、多態(tài)及獲取對(duì)象信息操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類、繼承、多態(tài)等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-02-02
Pycharm主題切換(禁用)導(dǎo)致UI界面顯示異常的解決方案
這篇文章主要介紹了Pycharm主題切換(禁用)導(dǎo)致UI界面顯示異常的原因分析和解決方案,文中通過(guò)圖文結(jié)合的方式給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
Python產(chǎn)生Gnuplot繪圖數(shù)據(jù)的方法
今天小編就為大家分享一篇Python產(chǎn)生Gnuplot繪圖數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
PyTorch 中的 torch.utils.data 解析(推薦)
這篇文章主要介紹了PyTorch?torch.utils.data.Dataset概述案例詳解,主要介紹對(duì)?torch.utils.data.Dataset?的理解,需要的朋友可以參考下2023-02-02

