JMP의 임베디드 Python 환경을 사용하여 데이터베이스를 가져오는 방법에 대해 궁금했다면 시작하는 데 도움이 될 수 있는 몇 가지 예가 있습니다. 첫 번째 예에서는 pyodbc 모듈을 사용하여 SQL Server 데이터베이스에서 데이터를 가져오고 두 번째 예에서는 OracleDB 모듈을 사용합니다. SQL과 Python에 익숙하고 JMP를 벗어나지 않고도 SQL을 빠르게 실행해야 하는 경우 이러한 빠른 팁을 즐기실 것이라고 생각합니다.
참고: pyodbc는 다양한 데이터베이스 관리 시스템(MySQL, SQL Server, PostgreSQL 등)과 함께 사용할 수 있습니다. Oracledb는 Oracle 데이터베이스 연결을 설정하는 데만 사용할 수 있습니다.
필수 조건:
예제 1: Python 모듈 pyodbc 사용
#Imports the jmp modules jmp. Pandas and jmputils.
import jmp
import jmputils
import pandas as pd
#Installs pyodbc andpandas if they have not been installed before.
#jmputils.jpip('install', 'pyodbc pandas')
#import the module pyodbc.
import pyodbc
#Defines variables for the driver's ODBC parameters.
drv = ''
db = ''
server = ''
uid = ''
pw = ''
#Passes the defined variables to the connection string parameters and establishes a connection.
cnxn = pyodbc.connect('DRIVER={%s};Server=%s;Database=%s;uid=%s;pwd=%s' % (drv, server, db, uid, pw))
#Defines a variable for the SQL query.
query = 'SELECT * FROM dbo.tablename'
#Executes the SQL query and prints the DataFrame df.
cursor = cnxn.cursor()
df = pd.read_sql(query, con=cnxn)
print (df)
cnxn.close()
#Run jmp.run_JSL to get the DataFrame 'df'.
jmp.run_jsl('python get (df)<< newdataview;'
);
예제 2: Python 모듈 Oracledb 사용
#Imports the jmp modules jmp and jmputils.
import jmp
import jmputils
import pandas as pd
#Installs oracledb and pandas if they have not been installed before.
#jmputils.jpip('install', 'oracledb pandas')
#Imports the module oracledb.
import oracledb
#Passes the connection string parameters to the database to establish a connection.
con = oracledb.connect(user='', password='', service_name='', host='', port='')
#Executes the SQL query and prints it as the list 'res'.
cur = con.cursor()
cur.execute('SELECT * FROM dbo.tablename')
res = cur.fetchall()
for row in res:
print(row)
#Converts the list 'res' to a pandas DataFrame.
df_ora = pd.DataFrame(res)
print(df_ora)
#Runs jmp.run_JSL to get the DataFrame 'df_ora'.
jmp.run_jsl('python get ("df_ora") << newdataview;')