π‘ λ³μλ μμ μ΄ μμ±λ λ²μ(μ½λλΈλ)μμμλ§ μ ν¨νλ€.
ν¨μ μμμ λ§λ λ³μλ ν¨μ μμμ μ΄μμλ€κ° ν¨μ μ½λμ μ€νμ΄ μ’
λ£λλ©΄ μλͺ
μ λ€νλ€. μ΄κ²μ μ§μλ³μ
λΌκ³ ν©λλ€.
μ΄μλ λ°λλ‘ ν¨μ μΈλΆμμ λ§λ λ³μλ νλ‘κ·Έλ¨μ΄ μ΄μμλ λμμ ν¨κ» μ΄μμλ€κ° νλ‘κ·Έλ¨μ΄ μ’
λ£λλ©΄ κ°μ΄ μλ©Έν©λλ€. μ΄λ κ² νλ‘κ·Έλ¨ μ 체λ₯Ό μ ν¨λ²μλ‘ κ°μ§λ λ³μλ₯Ό μ μλ³μ
λΌκ³ ν©λλ€.
λ¬Έλ²μμ 1.
def StarBucks():
mug_cup = 'Americano' # μ§μλ³μ
return mug_cup
print(StarBucks())
print(mug_cup) # β
μ΄λ κ² λ¨Έκ·Έμ»΅(μ§μλ³μ)λ₯Ό ν¨μ λ°μΌλ‘ κ°μ Έμ€λ©΄ μλ¬κ° λλ€.
λ¬Έλ²μμ 2.
μ μλ³μ
tumbler = 'water' # λ§μ½ λΉμ΄μλ μνλ‘ λκ³ μΆλ€λ©΄ Noneμ ν λΉ. noneν λλ μΏΌν
μ΄μ
λ§ν¬ x
def StarBucks():
global tumbler # μ μλ³μλ₯Ό ν¨μ λ΄μμ μ¬μ©νλ €λ©΄ globalμ λΆμ¬μ€λ€.
tumbler = 'Americano'
return tumbler
print(StarBucks()) # Americano
print(tumbler) # Americano
λΈλ£¨λ³΄νμ κ°λ³΄μ
tumbler = 'water'
def StarBucks():
global tumbler
tumbler = 'Americano'
return tumbler
def BlueBottle():
global tumbler
tumbler = 'Cappuccino'
return tumbler
print (StarBucks()) # Americano
print (BlueBottle()) # Cappuccino
print (tumbler) # Cappuccino
π‘ λ³μμ λ΄κΈ΄ κ°μ΄ ν¨μλ§λ€ 곡ν΅μ μΌλ‘ μ¬μ©λμ΄μΌ νλ κ²½μ°μ μ μ©νλ€.
ex) μμ λμ΄λ₯Ό κ³μ°νλ λ€μν ν¨μλ€μ΄ μλ€λ©΄ κ·Έ ν¨μλ€μ΄ μ λΆ λμΌν μμ£Όμ¨μ μ¬μ©ν΄μΌ νλ€.
λ¬Έμ 96. μμ λμ΄λ₯Ό ꡬνλ ν¨μλ₯Ό μμ±νμμ€
def cycle_function(num1):
pi = 3.14
return pi * num1 * num1 # μ λμ΄ κ³΅μ : 3.14 * λ°μ§λ¦ * λ°μ§λ¦
print(cycle_function(5)) # 78.5
λ¬Έμ 97. λΆμ±κΌ΄μ λμ΄λ₯Ό ꡬνλ ν¨μλ₯Ό μμ±νμμ€
def cycle_function2(num1):
pi = 3.14159 # μ¬κΈ° μ΄λ κ² μΌκ΄μ± μμΌλ©΄ κ²°κ³Όκ°μ΄ λ¬λΌμ§λ€.
return 1/4 * pi * num1 * num1
print(cycle_function2(5)) # 19.6349375
β μμ λμ΄λ₯Ό ꡬν λλ piκ° 3.14μκ³ λΆμ±κΌ΄μ λμ΄λ₯Ό ꡬν λλ piκ° 3.1459λ©΄ κ²°κ³Όμ μΌλ‘ κ°μ νλ‘κ·Έλ¨ λ΄μμ λμΌν piλ₯Ό μ¬μ©νμ§ μμ κ²°κ³Όκ° λ©λλ€.
λ¬Έμ 98. μ λκ°μ ν¨μκ° κ°μ piλ₯Ό μ¬μ©ν μ μλλ‘ μ μλ³μλ₯Ό μ¬μ©νμμ€
pi = 3.14
def cycle_function(num1):
global pi
return pi * num1 * num1
def cycle_function2(num1):
global pi
return 1/4 * pi * num1 * num1
print(cycle_function(5))
print(cycle_function2(5))
β
μμκ°μ΄ piκ°μ΄ μ΄λ ν¨μμμ μ°μ΄λ λμΌν piκ°μ μ¬μ©νλ €λ©΄, μ μλ³μ
λ‘ λ§λ€μ΄μ ν¨μλ΄μμ globalλ‘ νΈμΆνμ¬ μ¬μ©!
<λ¬Έλ²>
def my_abs(x):
if x < 0 :
return x * -1
else :
return x
print (my_abs(-7))
μ my_abs ν¨μλ₯Ό lambdaμμΌλ‘ λ³κ²½
my_abs2 = lambda x : x * -1 if x < 0 else x
# lambda μ
λ ₯λ³μ : μΆλ ₯μ μν μ€νλ¬Έ
print(my_abs2(-7))
# 1. μλ ifλ¬Έ vs # νμ€μ§λ¦¬ ifλ¬Έ
# if x < 0: # x * -1 if x < 0 else x
# return x * -1
# else :
# return x
λ¬Έμ 99. μλμ ν¨μλ₯Ό lambdaμμΌλ‘ ꡬννμμ€
def add_func(a,b) :
return a + b
print (add_func(3,5))
# lambdaμμΌλ‘
add_func = lambda a,b : a + b
print (add_func(3,5))
λ¬Έμ 100. db μλν ν¨μμ 4λ²μ½λμ λ²κ·Έλ₯Ό μμ νμμ€!
# 1. μ€λΌν΄ μ°λ ν¨μ
def oracle_table(table_name):
import cx_Oracle
import pandas as pd
dsn = cx_Oracle.makedsn('localhost', 1521, 'xe')
db = cx_Oracle.connect('c##scott', 'tiger', dsn)
cursor = db.cursor()
query = f"SELECT * FROM {table_name}"
cursor.execute(query )
row = cursor.fetchall()
colnames = cursor.description
col = [ ]
for i in colnames:
col.append( i[0].lower() )
df = pd.DataFrame(row , columns = col )
return ( df )
#2. dba μλν μ€ν¬λ¦½νΈ
def easy_dba():
while True:
print( """ === dba μμ
μ νΈνκ² μννκΈ° μν μ€ν¬λ¦½νΈ μ΄λͺ¨μ ====
0. νλ‘κ·Έλ¨μ μ’
λ£νλ €λ©΄ 0λ²μ λλ₯΄μΈμ.
1. ν
μ΄λΈ μ€νμ΄μ€ μ¬μ©λμ νμΈνλ €λ©΄ 1λ²μ λλ₯΄μΈμ
2. νμ¬ λ°μ΄ν°λ² μ΄μ€ λ½(lock) λ°μνλμ§ νμΈνλ €λ©΄ 2λ²μ λλ₯΄μΈμ
3. μ€λΌν΄μ μ°λνκ² μΆμΌλ©΄ 3λ²μ λλ₯΄μΈμ
4. alert log file μ λΆμνκ³ μΆλ€λ©΄ 4λ²μ λλ₯΄μΈμ
""")
num = int( input('μνλ λ²νΈλ₯Ό μ
λ ₯νμΈμ ~' ) )
if num == 0:
break
elif num == 1:
import cx_Oracle
import pandas as pd
dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe')
db = cx_Oracle.connect('c##scott','tiger', dsn)
cursor = db.cursor()
cursor.execute(""" select t.tablespace_name,
round(((t.total_size - f.free_size) / t.total_size),2) * 100 usedspace
from (select tablespace_name, sum(bytes)/1024/1024 total_size
from dba_data_files
group by tablespace_name) t,
(select tablespace_name, sum(bytes)/1024/1024 free_size
from dba_free_space
group by tablespace_name) f
where t.tablespace_name = f.tablespace_name(+) """)
row = cursor.fetchall()
col=[]
for i in colname:
col.append( i[0].lower() )
df = pd.DataFrame (list(row), columns=col)
print(df)
break
elif num == 2:
continue
elif num ==3:
import cx_Oracle # μ€λΌν΄κ³Ό νμ΄μ¬μ μ°λνκΈ° μν λͺ¨λ
import pandas as pd
dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe')
db = cx_Oracle.connect('c##scott','tiger', dsn)
cursor = db.cursor()
cursor.execute(""" select * from emp """)
row = cursor.fetchall()
colname = cursor.description
col=[]
for i in colname:
col.append( i[0].lower() )
emp = pd.DataFrame (list(row), columns=col)
print(' μ μ°λλμμ΅λλ€')
break
elif num == 4:
jobs = open("C:\\app\\ITWILL\\product\\18.0.0\\diag\\rdbms\\xe\\xe\\trace\\alert_xe.log", encoding='cp949', errors='ignore')
data = jobs.read()
data2= data.split()
k = [ ]
for i in data2:
if 'ora-' in i.lower():
k.append(i)
import pandas as pd
df = pd.DataFrame( k, columns=['col1'] )
from pandasql import sqldf
q = """ select col1, count(*)
from df
group by col1
order by 2 desc; """
print(sqldf(q) )
break
π‘ μμΈμ²λ¦¬λ μ΄λ¦ κ·Έλλ‘ μμΈμ¬νμΌλ‘ μ²λ¦¬νκ² λ€! λΌλ κ². νμ΄μ¬ μ½λμ λ¬Έμ κ° μμ΄μ λλ μ€λ₯κ° μλκ³ μ λ ₯λλ λ°μ΄ν°κ° μλͺ»λμ΄ λλ μ€λ₯μ λν μμΈμ²λ¦¬μ΄λ€.
λ¬Έλ²μμ 1.
λκ°μ μ«μλ₯Ό λ¬Όμ΄λ³΄κ³ λλκΈ°λ₯Ό νλ ν¨μλ₯Ό μμ±νκΈ°
def my_devide():
x = input('λΆμμ μ«μ μ
λ ₯')
y = input('λΆλͺ¨μ μ«μ μ
λ ₯')
return int(x) / int(y)
print (my_devide())
β my_devide ν¨μλ₯Ό μ€νν λ μ¬μ©μκ° μ«μλ₯Ό λ£μ§ μκ³ λ¬Έμμ΄μΈ 'a'λ₯Ό λ£μΌλ©΄ μλ¬κ° λλ€. μ΄λ΄ λ μμΈμ²λ¦¬λ₯Ό ν΄μΌν¨.
λ¬Έλ²μμ 2.
μλͺ»λ κ°μ μ
λ ₯νμ λ μμΈμ²λ¦¬ νλμ½λ (try ~ except)
try:
μ€ν λ μ£Όμ μ½λ
except:
tryμ exceptμ¬μ΄μ μ½λμ λ¬Έμ κ° μκ²Όμ λ μ€νλ μ½λ
--------------------------------------------------------------------
def my_devide():
try:
x = input('λΆμμ μ«μ μ
λ ₯')
y = input('λΆλͺ¨μ μ«μ μ
λ ₯')
return int(x) / int(y)
except:
return 'μλͺ»λ κ°μ μ
λ ₯ν΄μ λλκΈ°λ₯Ό ν μ μμ΄μ'
print (my_devide())
λ¬Έμ 101. λ€μμ gegop() ν¨μλ₯Ό λ§λ€κ³ μ€ννμΈμ
def gegop() :
num = int(input('μ«μλ₯Ό μ
λ ₯νμΈμ'))
return num ** 2
print(gegop())
λ¬Έμ 102. μ μμΈμ²λ¦¬
def gegop() :
try:
num = int(input('μ«μλ₯Ό μ
λ ₯νμΈμ'))
return num ** 2
except:
return 'μλͺ»λ κ°μ μ
λ ₯νμ΅λλ€.'
print(gegop())
<λ¬Έλ²>
def gegop() :
try:
num = int(input('μ«μλ₯Ό μ
λ ₯νμΈμ'))
print( num ** 2 )
except:
print( 'μλͺ»λ κ°μ μ
λ ₯νμ΅λλ€.' )
else:
print( 'κ³μ°μ μ±κ³΅νμ΅λλ€.' )
-------------------------------------------------------------------
gegop()
β
returnμ νλλμ€κ³ λλ΄λ²λ €μ printλ‘ λ°κΏμ€!
λ¬Έμ 103. my_devide() ν¨μλ₯Ό μμ ν΄μ λ€μκ³Όκ°μ΄ κ³μ°μ΄ μ±κ³΅νμ΅λλ€ λΌλ λ©μΈμ§κ° μΆλ ₯λκ² νκ³ , μλͺ»λ κ°μ μ λ ₯λ°μΌλ©΄ μλͺ»λ κ°μ μ λ ₯νλ€λ λ©μΈμ§ μΆλ ₯λκ² νκΈ°
def my_devide():
try:
x = input('λΆμμ μ«μ μ
λ ₯')
y = input('λΆλͺ¨μ μ«μ μ
λ ₯')
print (int(x) / int(y))
except:
print ('μλͺ»λ κ°μ μ
λ ₯ν΄μ λλκΈ°λ₯Ό ν μ μμ΄μ')
else:
print ('κ³μ° μλ£!')
----------------------------------------------------------------
my_devide()
μμμλ λ°μ΄ν° μ€λ₯λ‘ μΈν΄ νλ‘κ·Έλ¨μ΄ μ’ λ£λμ§ μλλ‘ μμΈμ²λ¦¬λ₯Ό νλ€. κ·Έλ°λ° μμΈμ²λ¦¬κ° κΌ νλ‘κ·Έλ¨μ μ’ λ£μν€μ§ μκΈ°μν΄μλ§ μ¬μ©νλκ²μ μλλ€. νλ‘κ·Έλ¨μ μΌλΆλ¬ μ’ λ£μμΌλ²λ¦¬κΈ° μν΄μλ μ¬μ©νλ€.
<λ¬Έλ²> νλ‘ꡬλ¨μ΄ μ’ λ£λκΈ° μ μ μ μλ₯Ό λ¨κΈ°κ³ μ’ λ£λκ²νλ μμΈμ²λ¦¬ μ½λ
def my_devide():
x = input('λΆμμ μ«μ μ
λ ₯')
y = input('λΆλͺ¨μ μ«μ μ
λ ₯')
if y == '0':
raise Exception ('λΆλͺ¨μ 0μ μ
λ ₯νλ©΄ λλκΈ°λ₯Ό ν μ μμ΄μ.')
else:
print (int(x) / int(y))
print ('μ±κ³΅μ μΌλ‘ κ³μ°μ΄ μλ£λμμ΅λλ€.')
<λ¬Έλ²>
1. μ¬μλ²νΈλ₯Ό μ
λ ₯νκ³ νλ‘μμ λ₯Ό μννλ©΄ ν΄λΉ μ¬μμ μκΈμ΄ μΆλ ₯λλ νλ‘μμ λ₯Ό μμ±νμμ€
create or replace procedure find_sal
( p_empno in number,
p_sal out number )
is
begin
select sal into p_sal
from emp
where empno = p_empno;
end;
/
SQL> variable v_result number;
SQL>
SQL> exec find_sal( 7788, :v_result );
PL/SQL μ²λ¦¬κ° μ μμ μΌλ‘ μλ£λμμ΅λλ€.
SQL> print v_result
V_RESULT
----------
3000
import cx_Oracle # μ€λΌν΄κ³Ό νμ΄μ¬μ μ°λνκΈ° μν λͺ¨λ
dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe')
db = cx_Oracle.connect('c##scott','tiger', dsn)
cursor = db.cursor()
v_result = cursor.var(cx_Oracle.STRING)
cursor.callproc('find_sal',(7788,v_result))
# νλ‘μμ μ΄λ¦ / v_resultμ κ°μ΄ λ΄κΈΈκ²μ΄λ€ !
result = v_result.getvalue()
print(result)
λ¬Έμ 104. μ¬μλ²νΈλ₯Ό μ λ ₯νλ©΄ ν΄λΉ μ¬μμ μ§μ μ΄ μΆλ ₯λλ νλ‘μμ λ₯Ό find_jobμ΄λΌλ μ΄λ¦μΌλ‘ μμ±νκΈ°
create or replace procedure find_job
( p_empno in number,
p_job out emp.job%type )
is
begin
select job into p_job
from emp
where empno = p_empno;
end;
/
SQL> variable v_result varchar2(20);
SQL>
SQL> exec find_job( 7788, :v_result );
SQL> print v_result
λ¬Έμ 105. νμ΄μ¬μΌλ‘
import cx_Oracle # μ€λΌν΄κ³Ό νμ΄μ¬μ μ°λνκΈ° μν λͺ¨λ
dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe')
db = cx_Oracle.connect('c##scott','tiger', dsn)
cursor = db.cursor()
v_result = cursor.var(cx_Oracle.STRING)
cursor.callproc('find_job',(7788,v_result))
# νλ‘μμ μ΄λ¦ / v_resultμ κ°μ΄ λ΄κΈΈκ²μ΄λ€ !
result = v_result.getvalue()
print(result)
λ¬Έμ 106. μ μ½λλ₯Ό ν¨μλ‘ λ§λ€μ΄μ λ€μκ³Ό κ°μ΄ μνλ μ μκ² νκΈ°
oracle_find_job(7788)
def oracle_find_job(num):
import cx_Oracle # μ€λΌν΄κ³Ό νμ΄μ¬μ μ°λνκΈ° μν λͺ¨λ
dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe')
db = cx_Oracle.connect('c##scott','tiger', dsn)
cursor = db.cursor()
v_result = cursor.var(cx_Oracle.STRING)
cursor.callproc('find_job',(num,v_result))
# νλ‘μμ μ΄λ¦ / v_resultμ κ°μ΄ λ΄κΈΈκ²μ΄λ€ !
result = v_result.getvalue()
print(result)
oracle_find_job(7788) # ANALYST
λ¬Έμ 107. μ ν¨μλ₯Ό μμ ν΄μ μλ μ¬μλ²νΈλ₯Ό λ£κ³ μ€ννλ©΄ 'ν΄λΉ μ¬μλ²νΈλ μλ μ¬μλ²νΈμ
λλ€' λΌκ³ μΆλ ₯νκΈ°
oracle_find_job(9999)
'ν΄λΉ μ¬μλ²νΈλ μλ μ¬μλ²νΈμ
λλ€'
def oracle_find_job(num): try: import cx_Oracle dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe') db = cx_Oracle.connect('c##scott','tiger', dsn) cursor = db.cursor() v_result = cursor.var(cx_Oracle.STRING) cursor.callproc('find_job',(num,v_result)) result = v_result.getvalue() print(result) except: print('μλ μ¬μλ²νΈ μ λλ€.') -------------------------------------------------------------- oracle_find_job(9999)
select decode(status,'INACTIVE',username || ' ' || sid || ',' || serial#,'lock') as Holder,
decode(status,'ACTIVE', username || ' ' || sid || ',' || serial#,'lock') as waiter, sid, serial#, status
from( select level as le, NVL(s.username,'(oracle)') AS username,
s.osuser,
s.sid,
s.serial#,
s.lockwait,
s.module,
s.machine,
s.status,
s.program,
to_char(s.logon_TIME, 'DD-MON-YYYY HH24:MI:SS') as logon_time
from v$session s
where level>1
or EXISTS( select 1
from v$session
where blocking_session = s.sid)
CONNECT by PRIOR s.sid = s.blocking_session
START WITH s.blocking_session is null);
β
μ κ²°κ³Όμμ statusκ° μ‘ν°λΈλ‘ λμ€λ μΈμ
μ΄ λ°λ‘ waitingνκ³ μλ μΈμ
μ
λλ€.
select count(*)
from( select level as le, NVL(s.username,'(oracle)') AS username,
s.osuser,
s.sid,
s.serial#,
s.lockwait,
s.module,
s.machine,
s.status,
s.program,
to_char(s.logon_TIME, 'DD-MON-YYYY HH24:MI:SS') as logon_time
from v$session s
where level>1
or EXISTS( select 1
from v$session
where blocking_session = s.sid)
CONNECT by PRIOR s.sid = s.blocking_session
START WITH s.blocking_session is null)
where status = 'ACTIVE';
create or replace procedure lock_find
( p_cnt out number )
is
begin
select count(*) into p_cnt
from (select level as le, NVL(s.username,'(oracle)') AS username,
s.osuser,
s.sid,
s.serial#,
s.lockwait,
s.module,
s.machine,
s.status,
s.program,
to_char(s.logon_TIME, 'DD-MON-YYYY HH24:MI:SS') as logon_time
from v$session s
where level>1
or EXISTS( select 1
from v$session
where blocking_session = s.sid)
CONNECT by PRIOR s.sid = s.blocking_session
START WITH s.blocking_session is null)
where status = 'ACTIVE';
end;
/
v$session
λ₯Ό λͺ»λ³Έλ€.
alter system set 07_DICTIONARY_ACCESSIBILIY = true
μ΄λ κ² ν΄λ΄€λλ°, μ§μ λ μ΄κΈ°ν λ§€κ°λ³μλ₯Ό μμ ν μ μλ€λ μλ¬λ©μΈμ§κ° λλ€. DBλ΄λ Έλ€ μ¬λ €μΌν¨ !!
// sysμ μ μμ Bλ΄λ¦¬κΈ°
alter system set 07_DICTIONARY_ACCESSIBILIY = true
scope = spfile;
-------------------------------------
shutdown immediate
// μ¬λ¦¬κΈ°
startup
β νλ‘μμ μμ±μ μν μ¬μ μμ μν!
βοΈ λ¨Όμ sysμ μ λ‘ μ μν©λλ€.connect / as sysdba
βοΈ μ€λΌν΄ μ€μ νλΌλ―Έν° νμΌμ λ°±μ λ°μ΅λλ€.
// λ΄ μμΉνμΈ show parameter spfile // μ΄ κ²½λ‘μ μλ νμΌμ λ°ννλ©΄μ λ³΅μ¬ C:\APP\ITWILL\PRODUCT\18.0.0\DBHOMEXE\DATABASE\SPFILEXE.ORA
βοΈ
select any table
κΆνμ c##scottμ μ μκ² λΆμ¬ν©λλ€.// λ°μ΄ν°λ² μ΄ν°μ μλ μ΄λ€ ν μ΄λΈμ΄λ λ€ μ½μ μ μλ κΆνμ μ€λ€. grant select any table to c##scott;
βοΈ O7_DICTIONARY_ACCESSIBILITYλ₯Ό trueλ‘ λ³κ²½ν©λλ€.
μ΄ νλΌλ―Έν°λ νλ‘μμ μμμ v$sessionκ³Ό κ°μ dbaν μ΄λΈμ μ λ νΈ ν μ μκ² ν΄μ£Όλ νλΌλ―Έν° μ λλ€.
show parameter o7
alter system set O7_DICTIONARY_ACCESSIBILITY=true scope=spfile; // μμ€ν μ΄ λ³κ²½λμμ΅λλ€.
βοΈ db λ΄λ Έλ€ μ¬λ¦½λλ€.
shutdown immediate // λ΄λ¦¬κΈ°
startup // μ¬λ¦¬κΈ°
βοΈ c##scott μ μ λ‘ μ μν΄μ μλμ νλ‘μμ μμ±νκΈ°! (μ μ½λμ κ°μ)
connect c##scott/tiger
create or replace procedure lock_find ( p_cnt out number ) is begin select count(*) into p_cnt from (select level as le, NVL(s.username,'(oracle)') AS username, s.osuser, s.sid, s.serial#, s.lockwait, s.module, s.machine, s.status, s.program, to_char(s.logon_TIME, 'DD-MON-YYYY HH24:MI:SS') as logon_time from v$session s where level>1 or EXISTS( select 1 from v$session where blocking_session = s.sid) CONNECT by PRIOR s.sid = s.blocking_session START WITH s.blocking_session is null) where status = 'ACTIVE'; end; /
βοΈ νλ‘μμ λ₯Ό μ€νν΄μ μ«μ 0μ΄ μΆλ ₯λλμ§ νμΈνμμ€ (μ§κΈ lockλκ² μλμνμ)SQL> variable v_result number; SQL> exec lock_find(:v_result); PL/SQL μ²λ¦¬κ° μ μμ μΌλ‘ μλ£λμμ΅λλ€. SQL> print v_result V_RESULT ---------- 0
βοΈ νμ΄μ¬μμ lock_find νλ‘μμ νΈμΆνκΈ°
import cx_Oracle # μ€λΌν΄κ³Ό νμ΄μ¬μ μ°λνκΈ° μν λͺ¨λ dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe') db = cx_Oracle.connect('c##scott','tiger', dsn) cursor = db.cursor() v_result = cursor.var(cx_Oracle.STRING) cursor.callproc('lock_find',[v_result]) # [v_result] μ΄λ κ² λ¦¬μ€νΈλ‘ λ°κΎΈλκΉ λ¨ result = v_result.getvalue() print(result)
λ¬Έμ 108. μμ μ½λλ₯Ό ν¨μλ‘ μμ±νκ³ μ€ννκΈ°! ν¨μμ΄λ¦μ find_lock
def find_lock (): import cx_Oracle # μ€λΌν΄κ³Ό νμ΄μ¬μ μ°λνκΈ° μν λͺ¨λ dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe') db = cx_Oracle.connect('c##scott','tiger', dsn) cursor = db.cursor() v_result = cursor.var(cx_Oracle.STRING) cursor.callproc('lock_find',[v_result]) result = v_result.getvalue() print(result ,'κ°μ λ½ waiting μΈμ μ΄ λ°κ²¬λμμ΅λλ€.') ------------------------------------------------------------------------- find_lock ()
λ¬Έμ 109. dba μλν μ€ν¬λ¦½νΈ 2λ²μ μ΄ ν¨μλ₯Ό μ€ννλ μ½λλ₯Ό μΆκ°νκ³ μ λλμ§ νμΈ
# 1. μ€λΌν΄ μ°λ ν¨μ
def oracle_table(table_name):
import cx_Oracle
import pandas as pd
dsn = cx_Oracle.makedsn('localhost', 1521, 'xe')
db = cx_Oracle.connect('c##scott', 'tiger', dsn)
cursor = db.cursor()
query = f"SELECT * FROM {table_name}"
cursor.execute(query )
row = cursor.fetchall()
colnames = cursor.description
col = [ ]
for i in colnames:
col.append( i[0].lower() )
df = pd.DataFrame(row , columns = col )
return ( df )
#2. dba μλν μ€ν¬λ¦½νΈ
def find_lock():
import cx_Oracle # μ€λΌν΄κ³Ό νμ΄μ¬μ μ°λνκΈ° μν λͺ¨λ
dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe')
db = cx_Oracle.connect('c##scott','tiger', dsn)
cursor = db.cursor()
v_result = cursor.var( cx_Oracle.STRING )
cursor.callproc( 'lock_find', [v_result] )
result = v_result.getvalue()
return str(result) + 'κ°μ λ½ waiting μΈμ
μ΄ λ°κ²¬λμμ΅λλ€.'
def easy_dba():
while True:
print( """ === dba μμ
μ νΈνκ² μννκΈ° μν μ€ν¬λ¦½νΈ μ΄λͺ¨μ ====
0. νλ‘κ·Έλ¨μ μ’
λ£νλ €λ©΄ 0λ²μ λλ₯΄μΈμ.
1. ν
μ΄λΈ μ€νμ΄μ€ μ¬μ©λμ νμΈνλ €λ©΄ 1λ²μ λλ₯΄μΈμ
2. νμ¬ λ°μ΄ν°λ² μ΄μ€ λ½(lock) λ°μνλμ§ νμΈνλ €λ©΄ 2λ²μ λλ₯΄μΈμ
3. μ€λΌν΄μ μ°λνκ² μΆμΌλ©΄ 3λ²μ λλ₯΄μΈμ
4. alert log file μ λΆμνκ³ μΆλ€λ©΄ 4λ²μ λλ₯΄μΈμ
""")
num = int( input('μνλ λ²νΈλ₯Ό μ
λ ₯νμΈμ ~' ) )
if num == 0:
print('μ’
λ£λμμ΅λλ€.')
break
elif num == 1:
import cx_Oracle
import pandas as pd
dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe')
db = cx_Oracle.connect('c##scott','tiger', dsn)
cursor = db.cursor()
cursor.execute(""" select t.tablespace_name,
round(((t.total_size - f.free_size) / t.total_size),2) * 100 usedspace
from (select tablespace_name, sum(bytes)/1024/1024 total_size
from dba_data_files
group by tablespace_name) t,
(select tablespace_name, sum(bytes)/1024/1024 free_size
from dba_free_space
group by tablespace_name) f
where t.tablespace_name = f.tablespace_name(+) """)
row = cursor.fetchall()
col=[]
for i in colname:
col.append( i[0].lower() )
df = pd.DataFrame (list(row), columns=col)
print(df)
break
elif num == 2:
message = find_lock()
print(message)
break
elif num ==3:
import cx_Oracle # μ€λΌν΄κ³Ό νμ΄μ¬μ μ°λνκΈ° μν λͺ¨λ
import pandas as pd
dsn = cx_Oracle.makedsn( 'localhost' , 1521, 'xe')
db = cx_Oracle.connect('c##scott','tiger', dsn)
cursor = db.cursor()
cursor.execute(""" select * from emp """)
row = cursor.fetchall()
colname = cursor.description
col=[]
for i in colname:
col.append( i[0].lower() )
emp = pd.DataFrame (list(row), columns=col)
print(' μ μ°λλμμ΅λλ€')
break
elif num == 4:
jobs = open("C:\\app\\ITWILL\\product\\18.0.0\\diag\\rdbms\\xe\\xe\\trace\\alert_xe.log", encoding='cp949', errors='ignore')
data = jobs.read()
data2= data.split()
k = [ ]
for i in data2:
if 'ora-' in i.lower():
k.append(i)
import pandas as pd
df = pd.DataFrame( k, columns=['col1'] )
from pandasql import sqldf
q = """ select col1, count(*)
from df
group by col1
order by 2 desc; """
print(sqldf(q) )
break
import dba_auto as a
a.easy_dba()
μ dba_auto.py μμ μλμκ°μ΄ 3κ°μ ν¨μκ° μλ€.
1. orcle_table
2. find_lock
3. easy_dba
μ΄ ν¨μλ€μ΄ μλ‘ λ°μ΄ν°λ₯Ό μ£Όκ³ λ°λ κ΄κ³λ©΄ μ΄ ν¨μλ€μ λ¬Άμ΄μ ν΄λμ€λ‘ λ§λ€μ΄ κ΄λ¦¬ν΄μΌνλ€. κ·Έλ°λ° κ·Έλ μ§ μλ€λ©΄ ν΄λμ€λ‘ λ§λ€ νμλ μλ€. (μλ ν΄λμ€λ‘ λ§λ€ νμκ° μλ ν¨μλ€!)
λ¬Έλ²μμ 1.
μ΄ ν΄λμ€λ₯Ό μμ±νκ² μ΅λλ€. 콩μ κΈ°λ₯μ ν¨μλ‘ λ§λ€λ©΄ 2κ°μ§κ° μμ΅λλ€.
1. μ΄μ μλ κΈ°λ₯ (shoot)
2. μ΄μμ μΆ©μ νλ κΈ°λ₯ (charge)
1. μ΄μμ μΆ©μ νλ ν¨μ μμ±
def charge(num):
bullet = num
print(bullet,'λ°μ΄ μ₯μ λμμ΅λλ€.')
charge(3)
2. μ΄μμ μλ ν¨μ μμ±
def shoot(num):
for i in range (1,num+1):
print('ν!')
shoot(3)
3. μμμ λ§λ chargeν¨μ, shootν¨μλ₯Ό κ°κ° μ€ννλλ° λ€μκ³Ό κ°μ΄ μ΄μμ 2λ° μ₯μ νκ³ μ΄μμ 3λ° μ΄μ§λμ§ μ€ν
charge(2)
shoot(3)
β
2λ° μ₯μ νλλ°, 3λ°μ΄ μ΄μ§λ€. κ·Έλμ μ₯μ ν λ§νΌ μ΄μμ΄ μ΄μ§κ² νλ €λ©΄ classλ‘ μ΄ ν¨μλ€μ ꡬμ±ν΄μ£Όμ΄μΌ νλ€.
4. ν΄λμ€λ‘ λ§λ€μ΄μ μΆ©μ ν λ§νΌλ§ μ΄μ§κ² νμμ€
# μ΄ ν΄λμ€ μμ±
class Gun(): # classμ΄λ¦μ μΉ΄λ©μΌμ΄μ€λ‘ μκΈμ λλ¬Έμ !
def charge(self,num):
self.bullet = num
print(self.bullet, 'λ°μ΄ μ₯μ λμμ΅λλ€.')
def shoot(self,num):
for i in range(1, num + 1):
if self.bullet > 0: # μΆ©μ ν μ΄μμ΄ μλ€λ©΄
print('ν!') # νν!
self.bullet = self.bullet -1 # μ΄μ νλ κΉκ³
elif self.bullet == 0: # μ΄μμ΄ 0κ°λ©΄(λ€μΌμΌλ©΄)
print('μ΄μμ΄ μμ΅λλ€!')
break
# μ΄ ν΄λμ€(μ€κ³λ)λ₯Ό κ°μ§κ³ μ΄(μ ν)μ λ§λλλ€.
gun1 = Gun() # μ νμ΄λ¦ = μ€κ³λ
# gun1 μ νμ μ΄μμ 10λ° μΆ©μ ν©λλ€.
gun1.charge(10) # 10 λ°μ΄ μ₯μ λμμ΅λλ€.
# gun1 μ΄μ νλ°μ© μ΄λ³΄κΈ°
gun1.shoot(1)
γ
γ
γ
γ
γ
γ
γ
γ
γ
γ
γ
β
ν΄λμ€λ‘ λ§λ€μ΄μ μ΄μ μμ°ν΄λ³΄λ μΆ©μ ν λ§νΌλ§ μ΄μμ μ μ μκ² λμλ€ !
5. Gun() ν΄λμ€λ‘ λ€λ₯Έ μ νλ λ§λ€ μ μλ€. λ§λ€μ΄λ³΄κΈ°!
gun2 = Gun()
gun2.charge(5)
β
μ
νκ° νλ μν . μ΄λ κ² Gun() ν΄λμ€λ‘ λ€λ₯Έ μ νμ λ§λ€μμ λ λκ°κ° ꡬλΆλλλ‘ ν΄μ€λ€. μκΉ gun1μ μ΄μ λ€μΌκ³ gun2μ μΆ©μ ν΄λμ μνμμ gun1λ‘ μ΄λ³΄λ©΄ μκΉμ²λΌ μ΄μμ΄ μλ€κ³ λμ¨λ€. κ°μ ν΄λμ€λ‘ λκ°λ₯Ό λ§λ€μμ§λ§ λκ°κ° λ€λ₯΄λ€.
λ¬Έμ 110. κ³ λ¦΄λΌ ν΄λμ€λ₯Ό λ€μκ³Ό κ°μ΄ μμ±νμμ€! (ν΄λμ€λͺ
: Gorilla)
1. λ°λλλ₯Ό λ¨Ήλ κΈ°λ₯ (ν¨μλͺ
: eat)
2. μ리μ§λ₯΄λ κΈ°λ₯ (ν¨μλͺ
: shaut)
class Gorilla(): def eat(self,num): self.bullet = num print('λ°λλ', self.bullet,'κ°λ₯Ό λ¨Ήμμ΅λλ€.') def shaut(self,num): for i in range(1, num + 1): if self.bullet > 0: # μΆ©μ ν μ΄μμ΄ μλ€λ©΄ print('μ°μ~') # νν! self.bullet = self.bullet -1 # μ΄μ νλ κΉκ³ elif self.bullet == 0: # μ΄μμ΄ 0κ°λ©΄(λ€μΌμΌλ©΄) print('λ°λλλ₯Ό λ€λ¨ΉμΌλ©΄ μ리λ₯Ό μ§λ₯Ό μ μμ΄μ!') break #μ΄ ν΄λμ€(μ€κ³λ)λ₯Ό κ°μ§κ³ μ΄(μ ν)μ λ§λλλ€. gorilla1 = Gorilla() # μ νμ΄λ¦ = μ€κ³λ #gun1 μ νμ μ΄μμ 10λ° μΆ©μ ν©λλ€. gorilla1.eat(10) # 10 λ°μ΄ μ₯μ λμμ΅λλ€. #gun1 μ΄μ νλ°μ© μ΄λ³΄κΈ° gorilla1.shaut(1)
λ¬Έμ 110. (λ§μ§λ§ λ¬Έμ )μλμ κ°μ΄ ν
μ΄λΈ μ€νμ΄μ€ μ΄λ¦κ³Ό ν
μ΄λΈ μ€νμ΄μ€ μ¬μ©λμ μΆλ ₯νλ νλ‘μμ λ₯Ό μμ±νμμ€
exec ts_space;
set serveroutput on
create or replace procedure ts_space
is
cursor ts_cursor is
select t.tablespace_name tablespaceName,
round(((t.total_size - f.free_size) / t.total_size),2) * 100 usedspace
from (select tablespace_name, sum(bytes)/1024/1024 total_size
from dba_data_files
group by tablespace_name) t,
(select tablespace_name, sum(bytes)/1024/1024 free_size
from dba_free_space
group by tablespace_name) f
where t.tablespace_name = f.tablespace_name(+);
begin
for ts_recode in ts_cursor loop
dbms_output.put_line(ts_recode.tablespaceName || chr(9) || ts_recode.usedspace);
end loop;
end;
/
exec ts_space;
β
μ΅λͺ
λΈλμ΄ μλλλ declareλ₯Ό μ°μ§ μλλ€ ! -> νλ‘μμ λ§λ€λλ μμ΄λ€.
β
for ts_recode in ts_cursor loop
λ‘ μ΄λ―Έ μ μΈμ΄ λμλ€.
β
declareμ¬μ©νλ λ!
π‘ μ΅λͺ
λΈλ‘μ΄ μλκ²λ€
π‘ μ΅λͺ
λΈλ‘μ΄λ?
μ΅λͺ λΈλ‘(Anonymous Block)μ λ°μ΄ν°λ² μ΄μ€μμ μ€νλλ PL/SQL μ½λ λ©μ΄λ¦¬λ₯Ό λ§ν©λλ€. PL/SQL(Procedural Language/Structured Query Language)μ Oracle λ°μ΄ν°λ² μ΄μ€μμ μ¬μ©λλ νλ‘κ·Έλλ° μΈμ΄λ‘, SQL λ¬Έμ ν¬ν¨νμ¬ νλ‘κ·Έλλ° λ‘μ§μ μμ±νκ³ μ€νν μ μλ κΈ°λ₯μ μ 곡ν©λλ€. μ΅λͺ λΈλ‘μ μ΄λ¦ μμ΄ μ€νλλ PL/SQL μ½λ λΈλ‘μ μλ―Έν©λλ€.
μ΅λͺ λΈλ‘μ μ£Όλ‘ λ€μκ³Ό κ°μ λͺ©μ μΌλ‘ μ¬μ©λ©λλ€:
μΌμμ μΈ μμ : νΉμ μμ μ μννκΈ° μν΄ μμλ‘ PL/SQL μ½λλ₯Ό μ€νν΄μΌ ν λ μ΅λͺ λΈλ‘μ μ¬μ©ν μ μμ΅λλ€. μλ₯Ό λ€μ΄, λ°μ΄ν° μ‘°μ, μ²λ¦¬, κ³μ° λ±μ μΌνμ±μΌλ‘ μνν λ μ¬μ©λ©λλ€.
ν μ€νΈ: μ½λ λΈλ‘μ μ¬μ©νμ¬ μ΄λ€ μ½λκ° μμλλ‘ λμνλμ§ ν μ€νΈν μ μμ΅λλ€. SQL λ¬Έκ³Ό νλ‘κ·Έλλ° λ‘μ§μ ν μ€νΈνκ³ λλ²κΉ ν λ μ μ©ν©λλ€.
μ€ν¬λ¦½νΈ μμ±: λ°μ΄ν°λ² μ΄μ€ κ°μ²΄λ₯Ό μμ±, μμ , μμ νλ μ€ν¬λ¦½νΈλ₯Ό μμ±ν λ μ΅λͺ λΈλ‘μ μ¬μ©νμ¬ λ³΅μ‘ν μμ μ μννκ±°λ 쑰건μ λ°λΌ μμ μ λ€λ₯΄κ² μνν μ μμ΅λλ€.
μ΅λͺ λΈλ‘μ λ€μκ³Ό κ°μ ννλ‘ μμ±λ©λλ€:
DECLARE -- λ³μλ μμ μ μΈ BEGIN -- PL/SQL μ½λ μμ± -- SQL λ¬Έ μ€ν -- λ³μ μ¬μ© -- μμΈ μ²λ¦¬ λ± END; /
μ΅λͺ λΈλ‘μ DECLARE μΉμ κ³Ό BEGIN ~ END; μ¬μ΄μ PL/SQL μ½λλ₯Ό μμ±ν©λλ€. DECLARE μΉμ μ μ νμ μ΄λ©°, λ³μλ μμλ₯Ό μ μΈν λ μ¬μ©λ©λλ€. BEGINμμλ μ€μ PL/SQL μ½λλ₯Ό μμ±νκ³ SQL λ¬Έμ μ€ννλ©°, νμν κ²½μ° μμΈ μ²λ¦¬λ₯Ό ν μ μμ΅λλ€. λΈλ‘μ λμλ / κΈ°νΈκ° μμΉνμ¬ μ€νμ μμν©λλ€.
μ΅λͺ λΈλ‘μ νλ‘μμ λ ν¨μμ λ¬λ¦¬ μ΄λ¦μ΄ μκΈ° λλ¬Έμ λ¨μν μμ μ μ²λ¦¬νκ±°λ ν μ€νΈλ₯Ό μν΄ κ°λ¨νκ² μ¬μ©ν μ μλ μ₯μ μ΄ μμ΅λλ€.