您的位置:首页 > 数据库 > Oracle

oracle数据库批量插入数据,及pl/sql Developer、python连接教程 含源码 -- 数据库

2019-04-09 12:07 417 查看
版权声明:篇章限本人所有,转载请标明出处。 https://blog.csdn.net/qq_31779317/article/details/89139671

oracle数据库批量插入数据及python连接教程

一、oracle环境配置

https://www.2cto.com/database/201701/588135.html (转自jffhy2017的博客)

二、创建表

Keys
主键:唯一关键字(如:学生表中的学号)
外键:与其他表联系的字段(如除学生表外,还有一张选课表,若修改了学生表中的学号,选修表也需要变,则需要给选修表中的学号作为外键约束)

Indexes
唯一索引:在普通索引的基础上,增加唯一约束(即必须唯一值)(另:主键约束的列,会自动为该列创建唯一索引)

三、批量插入数据到表

根据如下几种命令方法在数据库表中插入数据:

  1. loop循环
declare
x number;
begin
x := 0;
loop
x := x + 1;
Exit when x>9;
insert into HEYONGLIN select * from HEYONGLIN;
end loop;
end;
  1. for循环
declare
x number;
begin
x := 0;
For x in reverse 1..10 loop
x := x + 1;
insert into HEYONGLIN select * from HEYONGLIN;
end loop;
end;
  1. while循环
declare
x number;
begin
x := 0;
while x<10 loop
x := x + 1;
insert into HEYONGLIN select * from HEYONGLIN;
end loop;
end;
  1. goto循环
declare
x number;
begin
x := 0;
<<repeat_loop>>
x := x + 1;
insert into HEYONGLIN select * from HEYONGLIN;
If x<9 then
Goto repeat_loop
end if;
end;
  1. excel处理后复制粘贴


  2. 反复复制原数据
insert into tname select * from tname2
Insert into tname(a,b,c) select a,c,5 from tname2 #要求表tname必须存在
selcet * into tname from tname2 #要求表tname不存在

四、pl\sql Developer连接oracle数据库

本章使用oracle11g版本(python最低支持版本)

  1. 下载oracle数据库工具Instant Client,与pl\sql Developer(注:下载的版本需选择相应主机版本匹配如64位相应64位)
  1. 配置文件
    在oracle路径中如x:\oracleapp\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora ,把配置好的tnsnames.ora文件复制放进instantclient_11_2文件夹中

附配置文件tnsnames.ora配置方法:

XE:数据库名称,随意起名
HOST:服务器ip
PORT:端口
SERVICE_NAME:服务器名称,随意起名

  1. plsql设置
    打开plsql点Cancel进入主界面

    在pl\sql中tools\perferences设置instantclient_11_2目录与oci.dll路径

    重新登录连接即可。

五、python连接oracle数据库

import cx_Oracle
# 建立连接
db = cx_Oracle.connect('STD2017/STD20171QAZ@218.16.100.87:8017/xe')

# 数据库操作
cur = db.cursor()
cur.execute('select * from hyl_table_a')
result = cur.fetchall()

# 关闭数据库
cur.close()

个人代码行已存储在github:https://github.com/onlyhyl/database/tree/master/oracle

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: