环境配置:
- 操作系统:win10(64位)
- oracle客户端:instantclient_11_2(64位)
- python版本:python3.6.3(64位)
- python相关包:sqlalchemy, pandas, pymysql,cx_oracle
示例代码
from sqlalchemy import create_engine
import pandas as pd
"""
mysql
-- 建表:
CREATE TABLE students (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
name NVARCHAR(16),
age INT,
address NVARCHAR(256)
);
-- 插入数据:
INSERT INTO students(name,age,address) VALUES('zhangsan',18,'北京');
INSERT INTO students(name,age,address) VALUES('lisi',19,'上海');
INSERT INTO students(name,age,address) VALUES('wangyu',19,'天津');
INSERT INTO students(name,age,address) VALUES('xiaoliu',20,'重庆');
"""
import pymysql
pymysql.install_as_MySQLdb()
engine = create_engine(
'mysql+mysqldb://root:[email protected]:3306/marsapp?charset=utf8')
df = pd.read_sql('SELECT * FROM students', engine, index_col='id')
print(df)
dft = df.drop(['age'], axis=1)
dft.to_sql('students', engine, index=False, if_exists='append')
"""
Oracle
-- 建表:
CREATE TABLE STUDENTS (
ID NUMBER PRIMARY KEY KEY NOT NULL,
NAME VARCHAR2(16),
AGE NUMBER,
ADDRESS VARCHAR2(256)
);
-- 创建自增序列:
CREATE SEQUENCE STUDENTS_SEQ
MINVALUE 1
NOMAXVALUE
START WITH 1
INCREMENT BY 1
NOCYCLE
NOCACHE
-- 创建自增触发器:
CREATE OR REPLACE TRIGGER STUDENTS_TG
BEFORE INSERT ON STUDENTS FOR EACH ROW WHEN(NEW.ID IS NULL)
BEGIN
SELECT STUDENTS_SEQ.NEXTVAL INTO:NEW.ID FROM DUAL;
END;
-- 插入数据(语法直接复制的mysql,oracle若提示不正确,请根据提示改正):
INSERT INTO STUDENTS(NAME,AGE,ADDRESS) VALUES('ZHANGSAN',18,'北京');
INSERT INTO STUDENTS(NAME,AGE,ADDRESS) VALUES('LISI',19,'上海');
INSERT INTO STUDENTS(NAME,AGE,ADDRESS) VALUES('WANGYU',19,'天津');
INSERT INTO STUDENTS(NAME,AGE,ADDRESS) VALUES('XIAOLIU',20,'重庆');
"""
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
engine = create_engine("oracle://ORIGIN:[email protected]:1521/ORCL")
engine = create_engine(
'oracle+cx_oracle://ORIGIN:[email protected]:1521/ORCL')
df = pd.read_sql('SELECT * FROM STUDENTS', engine, index_col='ID')
print(df)
dft = df.drop(['age'], axis=1)
dft.to_sql('STUDENTS', engine, index=0, if_exists='append')