1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| import pandas as pd from sqlalchemy import create_engine
engine = create_engine(r'sqlite:///<path><name>.sqlite3', echo=True) engine = create_engine("mysql://<name>:<password>@<ip>/db?charset=utf8") engine = create_engine('mssql+pymssql://<ip>/db?')
df = pd.read_sql('<SelectSQL>', engine)
df.to_sql('<TableName>', engine, if_exists='append', index=False)
conn = engine.connect() conn.execute('<ExecSQL>') conn.close()
import pymssql
conn = pymssql.connect(host='<ip>', database='<db>', user='<usr>', password='<pwd>') cursor = conn.cursor() sql_exec = """ exec ProcedureName @ArgName = ArgValue""" cursor.execute(sql_exec) conn.commit() cursor.close() conn.close()
import cx_Oracle
conn = cx_Oracle.connect('<name>/<password>@<ip>/<ServiceName>')
df = pd.read_sql('<SelectSQL>', conn)
def insert_df(df, table): """向oracle数据库插dataframe(pd.to_sql)""" conn = cx_Oracle.connect('<name>/<password>@<ip>/<ServiceName>') sql = "insert into " + table + "(" for col in df.columns: sql += str(col) + ',' sql = sql[:-1] sql += ') values(:' for col in df.columns: sql += str(col) + ',:' sql = sql[:-2] sql += ')' rec = df.to_json(orient='records', force_ascii=False) rec = rec.replace("\\", "") cursor = conn.cursor() cursor.prepare(sql) cursor.executemany(None, eval(rec)) conn.commit() cursor.close() conn.close()
def execute_sql(sql): """oracle,执行一句sql""" conn = cx_Oracle.connect('<name>/<password>@<ip>/<ServiceName>') cursor = conn.cursor() cursor.execute(sql) conn.commit() cursor.close() conn.close()
conn = cx_Oracle.connect('<name>/<password>@<ip>/<ServiceName>') cursor = conn.cursor() cursor.callproc("<schema.ProcedureName>", [ < ArgValues >]) cursor.close() conn.close()
|