"Python oracle connect"의 두 판 사이의 차이
DB CAFE
(→oracledb 라이브러리 설치) |
(→ORACLE DB 라이브러리 설치) |
||
(같은 사용자의 중간 판 3개는 보이지 않습니다) | |||
3번째 줄: | 3번째 줄: | ||
<source lang=sql> | <source lang=sql> | ||
pip install oracledb # oracledb 라이브러리 설치 | pip install oracledb # oracledb 라이브러리 설치 | ||
− | |||
</source> | </source> | ||
=== 오라클 DB 연결 === | === 오라클 DB 연결 === | ||
<source lang=sql> | <source lang=sql> | ||
+ | import oracledb # oracledb 라이브러리 임포트(불러오기) | ||
+ | |||
con = oracledb.connect(user="사용자이름", password="비밀번호", dsn="호스트이름:port/SID") # DB에 연결 (호스트이름 대신 IP주소 가능) | con = oracledb.connect(user="사용자이름", password="비밀번호", dsn="호스트이름:port/SID") # DB에 연결 (호스트이름 대신 IP주소 가능) | ||
cursor = con.cursor() # 연결된 DB 지시자(커서) 생성 | cursor = con.cursor() # 연결된 DB 지시자(커서) 생성 | ||
42번째 줄: | 43번째 줄: | ||
print('데이터베이스 연결 성공~!!!') | print('데이터베이스 연결 성공~!!!') | ||
+ | </source> | ||
+ | <source lang=sql> | ||
# 1. SELECT - emp테이블의 내용 확인 | # 1. SELECT - emp테이블의 내용 확인 | ||
cursor.execute("select * from emp") # 데이터베이스 명령 실행( cursor가 임시로 보관) | cursor.execute("select * from emp") # 데이터베이스 명령 실행( cursor가 임시로 보관) | ||
51번째 줄: | 54번째 줄: | ||
</source> | </source> | ||
+ | <source lang=sql> | ||
# 2. INSERT - dept테이블에 2개의 레코드를 삽입 후 승인 | # 2. INSERT - dept테이블에 2개의 레코드를 삽입 후 승인 | ||
cursor.execute("insert into dept values(50, 'DEVELOPER','LA')") | cursor.execute("insert into dept values(50, 'DEVELOPER','LA')") | ||
60번째 줄: | 64번째 줄: | ||
print(record) | print(record) | ||
print('-'*50) | print('-'*50) | ||
− | + | </source> | |
+ | |||
+ | <source lang=sql> | ||
# 3. DELETE - dept 테이블에서 dname이 "DEVELOPER"인 레코드 삭제 후 승인 | # 3. DELETE - dept 테이블에서 dname이 "DEVELOPER"인 레코드 삭제 후 승인 | ||
cursor.execute("delete from dept where dname = 'DEVELOPER'") | cursor.execute("delete from dept where dname = 'DEVELOPER'") | ||
71번째 줄: | 77번째 줄: | ||
print(record) | print(record) | ||
print('-'*50) | print('-'*50) | ||
+ | </source> | ||
+ | <source lang=sql> | ||
# 4. CREATE - dept테이블의 구조와 내용을 이용하여 dept_ddl 테이블을 생성 | # 4. CREATE - dept테이블의 구조와 내용을 이용하여 dept_ddl 테이블을 생성 | ||
cursor.execute("create table dept_ddl as select * from dept") | cursor.execute("create table dept_ddl as select * from dept") |
2024년 4월 23일 (화) 16:15 기준 최신판
thumb_up 추천메뉴 바로가기
- DBA { Oracle DBA 명령어 > DBA 초급 과정 > DBA 고급 과정 }
- 튜닝 { 오라클 튜닝 목록 }
- 모델링 { 데이터 모델링 가이드 }
1 파이썬 오라클 DB 접속[편집]
1.1 ORACLE DB 라이브러리 설치[편집]
pip install oracledb # oracledb 라이브러리 설치
1.2 오라클 DB 연결[편집]
import oracledb # oracledb 라이브러리 임포트(불러오기)
con = oracledb.connect(user="사용자이름", password="비밀번호", dsn="호스트이름:port/SID") # DB에 연결 (호스트이름 대신 IP주소 가능)
cursor = con.cursor() # 연결된 DB 지시자(커서) 생성
- dsn (Database Source Name)
- 호스트이름 또는 IP주소 부분이 localhost 일 경우 : 로컬 시스템, 즉 현재 시스템에 있는 오라클 DB 를 가리킴
- port번호 : 오라클 DB 서버의 기본 포트번호는 1521
1.3 SQL 실행[편집]
cursor.execute("SQL문장") # DB 명령 실행 (cursor가 임시 보관)
out_data = cursor.fetchall() # cursor가 임시 보관한 내용을 out_data에 저장 (결과는 리스트)
# out_data 내용 출력해보기
for record in ou_data :
print(record)
- execute 함수 여러 줄 사용한 후, fetchall() 함수로 한 번에 저장 가능
- fetchone : 하나 읽어옴
- fetchall : 여러 개 읽어옴 (결과는 리스트)
- cursor 내용을 저장하여 출력할 때는 select문처럼 데이터가 조회되는 경우에만(sql실행 시 반환되는 결과가 있을 경우에만) 내용이 저장됨
- 주의할 점 : DML 작업을 실행했을 경우, commit 명령도 execute로 실행해야 DB에 적용됨 !
1.4 DB 연결 해제[편집]
con.close() # DB 연결 해제
1.5 사용 예시[편집]
con = oracledb.connect(user="system", password="oracle", dsn="localhost:1521/XEPDB1") # 데이터베이스에 연결
cursor = con.cursor() # 연결된 데이터베이스 지시자 생성
print('데이터베이스 연결 성공~!!!')
# 1. SELECT - emp테이블의 내용 확인
cursor.execute("select * from emp") # 데이터베이스 명령 실행( cursor가 임시로 보관)
out_data = cursor.fetchall() # 커서의 내용을 out_data에 저장
for record in out_data: # out_data의 내용을 출력
print(record)
print('-'*50)
# 2. INSERT - dept테이블에 2개의 레코드를 삽입 후 승인
cursor.execute("insert into dept values(50, 'DEVELOPER','LA')")
cursor.execute("insert into dept values(60, 'DEVELOPER','ATL')")
cursor.execute('commit') # sqldeveloper에 커밋
cursor.execute("select * from dept")
out_data2 = cursor.fetchall()
for record in out_data2:
print(record)
print('-'*50)
# 3. DELETE - dept 테이블에서 dname이 "DEVELOPER"인 레코드 삭제 후 승인
cursor.execute("delete from dept where dname = 'DEVELOPER'")
cursor.execute("commit")
cursor.execute("select * from dept")
out_data2 = cursor.fetchall()
for record in out_data2:
print(record)
print('-'*50)
# 4. CREATE - dept테이블의 구조와 내용을 이용하여 dept_ddl 테이블을 생성
cursor.execute("create table dept_ddl as select * from dept")
cursor.execute("select * from dept_ddl")
out_data = cursor.fetchall()
for record in out_data:
print(record)
print('-'*50)
con.close() # 데이터베이스 연결 해제