테이블 생성
# 라이브러리 불러오기
import cx_Oracle as oci
# 디비 연결
# "ID/PW@localhost:1521/orcl"
con = oci.connect("scott/tiger@localhost:1521/orcl")
# 커서 생성
cur = con.cursor()
# create table
# sql 실행 - DDL = 자동 커밋
sql = "CREATE TABLE TEST (ID NUMBER(2), NAME VARCHAR2(10))"
cur.execute(sql)
#
print(cur.execute("SELECT * FROM TEST").fetchall())
# 디비 연결 종료
con.close()
데이터 입력
# 라이브러리 불러오기
import cx_Oracle as oci
# 디비 연결
# "ID/PW@localhost:1521/orcl"
con = oci.connect("scott/tiger@localhost:1521/orcl")
# 커서 생성
cur = con.cursor()
# insert
while True:
data1 = input("ID를 입력하세요(정수)> ")
if data1 == "":
break
data2 = input("이름을 입력하세요(한글 5글자, 영어 10글자)> ")
sql = "INSERT INTO TEST VALUES('" + data1 + "','" + data2 + "')" # 작은 따옴표와 큰 따옴표 이용해서 문자 연결
cur.execute(sql)
# COMMIT
con.commit()
# 데이터 확인
print(cur.execute("SELECT * FROM TEST").fetchall())
# 디비 연결 종료
con.close()
데이터 조회
import cx_Oracle as oci
# 디비 연결
# "ID/PW@localhost:1521/orcl"
con = oci.connect("scott/tiger@localhost:1521/orcl")
# 커서 생성
cur = con.cursor()
#
sql = "SELECT ID, NAME FROM TEST"
cur.execute(sql)
print(" ID 이름")
print("-" * 15)
#
rows = cur.fetchall()
if rows:
for row in rows:
data1 = row[0]
data2 = row[1]
print("%3s %10s" % (data1, data2))
# 디비 연결 종료
con.close()
테이블 삭제
import cx_Oracle as oci
# 디비 연결
# "ID/PW@localhost:1521/orcl"
con = oci.connect("scott/tiger@localhost:1521/orcl")
# 커서 생성
cur = con.cursor()
# drop
sql = "DROP TABLE TEST"
cur.execute(sql)
#
print(cur.execute("SELECT * FROM TEST").fetchall())
# 디비 연결 종료
con.close()