Wenn Sie sich gefragt haben, wie Sie Ihre Datenbank mithilfe der eingebetteten Python-Umgebung von JMP importieren können, finden Sie hier einige Beispiele, die Ihnen den Einstieg erleichtern können. Im ersten Beispiel importiere ich Daten aus einer SQL Server-Datenbank mithilfe der pyodbc-Module; im zweiten Beispiel verwende ich das OracleDB-Modul. Wenn Sie mit SQL und Python vertraut sind und SQL schnell ausführen müssen, ohne JMP zu verlassen, werden Ihnen diese Kurztipps bestimmt gefallen.
Hinweis: Sie können pyodbc mit zahlreichen Datenbankverwaltungssystemen (MySQL, SQL Server, PostgreSQL usw.) verwenden. Oracledb kann nur zum Herstellen von Oracle-Datenbankverbindungen verwendet werden.
Voraussetzungen:
- ODBC 3.5 Unicode-Treiber, kompatibel mit Ihrem Datenbankverwaltungssystem (DBMS)
- JMP 18 oder höher
- Python-Module: Pandas , pyodbc oder OracleDB
Beispiel 1: Verwenden des Python-Moduls 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;'
);
Beispiel 2: Verwenden des Python-Moduls 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;')