cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-632953%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9C%A8%20JMP%C2%AE%2018%20%E5%8F%8A%E6%9B%B4%E9%AB%98%E7%89%88%E6%9C%AC%E4%B8%AD%E4%BD%BF%E7%94%A8%20JMP%20Python%20%E7%92%B0%E5%A2%83%E5%92%8C%20ODBC%20%E5%B0%8E%E5%85%A5%E6%95%B8%E6%93%9A%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-632953%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E5%A6%82%E6%9E%9C%E6%82%A8%E6%83%B3%E7%9F%A5%E9%81%93%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20JMP%20%E7%9A%84%E5%B5%8C%E5%85%A5%E5%BC%8F%20Python%20%E7%92%B0%E5%A2%83%E5%B0%8E%E5%85%A5%E8%B3%87%E6%96%99%E5%BA%AB%EF%BC%8C%E9%80%99%E8%A3%A1%E6%9C%89%E4%B8%80%E4%BA%9B%E7%AF%84%E4%BE%8B%E5%8F%AF%E4%BB%A5%E5%B9%AB%E5%8A%A9%E6%82%A8%E5%85%A5%E9%96%80%E3%80%82%E5%9C%A8%E7%AC%AC%E4%B8%80%E5%80%8B%E7%AF%84%E4%BE%8B%E4%B8%AD%EF%BC%8C%E6%88%91%E4%BD%BF%E7%94%A8%20pyodbc%20%E6%A8%A1%E7%B5%84%E5%BE%9E%20SQL%20Server%20%E8%B3%87%E6%96%99%E5%BA%AB%E5%8C%AF%E5%85%A5%E8%B3%87%E6%96%99%EF%BC%9B%E6%88%91%E5%9C%A8%E7%AC%AC%E4%BA%8C%E5%80%8B%E7%AF%84%E4%BE%8B%E4%B8%AD%E4%BD%BF%E7%94%A8%20OracleDB%20%E6%A8%A1%E7%B5%84%E3%80%82%E5%A6%82%E6%9E%9C%E6%82%A8%E7%86%9F%E6%82%89%20SQL%20%E5%92%8C%20Python%EF%BC%8C%E4%B8%A6%E4%B8%94%E9%9C%80%E8%A6%81%E5%9C%A8%E4%B8%8D%E9%9B%A2%E9%96%8B%20JMP%20%E7%9A%84%E6%83%85%E6%B3%81%E4%B8%8B%E5%BF%AB%E9%80%9F%E5%9F%B7%E8%A1%8C%20SQL%EF%BC%8C%E6%88%91%E6%83%B3%E6%82%A8%E6%9C%83%E5%96%9C%E6%AD%A1%E9%80%99%E4%BA%9B%E5%BF%AB%E9%80%9F%E6%8F%90%E7%A4%BA%E3%80%82%3C%2FP%3E%0A%3CP%3E%E6%B3%A8%E6%84%8F%EF%BC%9A%E6%82%A8%E5%8F%AF%E4%BB%A5%E5%B0%87%20pyodbc%20%E8%88%87%E5%A4%9A%E7%A8%AE%E8%B3%87%E6%96%99%E5%BA%AB%E7%AE%A1%E7%90%86%E7%B3%BB%E7%B5%B1%EF%BC%88MySQL%E3%80%81SQL%20Server%E3%80%81PostgreSQL%20%E7%AD%89%EF%BC%89%E4%B8%80%E8%B5%B7%E4%BD%BF%E7%94%A8%E3%80%82%20Oracledb%E5%8F%AA%E8%83%BD%E7%94%A8%E6%96%BC%E5%BB%BA%E7%AB%8BOracle%E8%B3%87%E6%96%99%E5%BA%AB%E9%80%A3%E7%B7%9A%E3%80%82%3C%2FP%3E%0A%3CP%3E%E5%85%88%E6%B1%BA%E6%A2%9D%E4%BB%B6%EF%BC%9A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%E8%88%87%E6%82%A8%E7%9A%84%E8%B3%87%E6%96%99%E5%BA%AB%E7%AE%A1%E7%90%86%E7%B3%BB%E7%B5%B1%20(DBMS)%20%E7%9B%B8%E5%AE%B9%E7%9A%84%20ODBC%203.5%20Unicode%20%E9%A9%85%E5%8B%95%E7%A8%8B%E5%BC%8F%3C%2FLI%3E%0A%3CLI%3EJMP%2018%20%E6%88%96%E6%9B%B4%E9%AB%98%E7%89%88%E6%9C%AC%3C%2FLI%3E%0A%3CLI%3EPython%20%E6%A8%A1%E7%B5%84%EF%BC%9A%20%3CA%20href%3D%22https%3A%2F%2Fpypi.org%2Fproject%2Fpandas%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EPandas%3C%2FA%3E%20%E3%80%81%20%3CA%20href%3D%22https%3A%2F%2Fpypi.org%2Fproject%2Fpyodbc%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Epyodbc%3C%2FA%3E%E6%88%96%3CA%20href%3D%22https%3A%2F%2Fpypi.org%2Fproject%2Foracledb%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EOracleDB%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20data-unlink%3D%22true%22%3E%E7%AF%84%E4%BE%8B%201%EF%BC%9A%E4%BD%BF%E7%94%A8%20Python%20%E6%A8%A1%E7%B5%84%20pyodbc%3C%2FP%3E%0A%3CPRE%3E%23Imports%20the%20jmp%20modules%20jmp.%20Pandas%20and%20jmputils.%0Aimport%20jmp%0Aimport%20jmputils%0Aimport%20pandas%20as%20pd%0A%0A%23Installs%20pyodbc%20andpandas%20if%20they%20have%20not%20been%20installed%20before.%0A%23jmputils.jpip('install'%2C%20'pyodbc%20pandas')%0A%20%0A%23import%20the%20module%20pyodbc.%0Aimport%20pyodbc%0A%0A%23Defines%20variables%20for%20the%20driver's%20ODBC%20parameters.%0Adrv%20%3D%20'%3CDRIVER%20name%3D%22%22%3E'%0Adb%20%3D%20'%3CDATABASE%3E'%0Aserver%20%3D%20'%3CSERVER%3E'%0Auid%20%3D%20'%3CUSERNAME%3E'%0Apw%20%3D%20'%3CPASSWORD%3E'%0A%0A%23Passes%20the%20defined%20variables%20to%20the%20connection%20string%20parameters%20and%20establishes%20a%20connection.%0Acnxn%20%3D%20pyodbc.connect('DRIVER%3D%7B%25s%7D%3BServer%3D%25s%3BDatabase%3D%25s%3Buid%3D%25s%3Bpwd%3D%25s'%20%25%20(drv%2C%20server%2C%20db%2C%20uid%2C%20pw))%0A%0A%23Defines%20a%20variable%20for%20the%20SQL%20query.%0Aquery%20%3D%20'SELECT%20*%20FROM%20dbo.tablename'%0A%0A%23Executes%20the%20SQL%20query%20and%20prints%20the%20DataFrame%20df.%0Acursor%20%3D%20cnxn.cursor()%0Adf%20%3D%20pd.read_sql(query%2C%20con%3Dcnxn)%0Aprint%20(df)%0Acnxn.close()%0A%20%20%0A%23Run%20jmp.run_JSL%20to%20get%20the%20DataFrame%20'df'.%0Ajmp.run_jsl('python%20get%20(df)%26lt%3B%26lt%3B%20newdataview%3B'%0A)%3B%3C%2FPASSWORD%3E%3C%2FUSERNAME%3E%3C%2FSERVER%3E%3C%2FDATABASE%3E%3C%2FDRIVER%3E%3C%2FPRE%3E%0A%3CP%20data-unlink%3D%22true%22%3E%E7%AF%84%E4%BE%8B%202%EF%BC%9A%E4%BD%BF%E7%94%A8%20Python%20%E6%A8%A1%E7%B5%84%20Oracledb%3C%2FP%3E%0A%3CPRE%3E%23Imports%20the%20jmp%20modules%20jmp%20and%20jmputils.%0Aimport%20jmp%0Aimport%20jmputils%0Aimport%20pandas%20as%20pd%0A%0A%23Installs%20oracledb%20and%20pandas%20if%20they%20have%20not%20been%20installed%20before.%0A%23jmputils.jpip('install'%2C%20'oracledb%20pandas')%0A%0A%23Imports%20the%20module%20oracledb.%0Aimport%20oracledb%0A%0A%23Passes%20the%20connection%20string%20parameters%20to%20the%20database%20to%20establish%20a%20connection.%0Acon%20%3D%20oracledb.connect(user%3D'%3CUSERNAME%3E'%2C%20password%3D'%3CPASSWORD%3E'%2C%20service_name%3D'%3CSERVICE%20name%3D%22%22%3E'%2C%20host%3D'%3CSERVERNAME%3E'%2C%20port%3D'%3CPORT%20number%3D%22%22%3E')%0A%0A%23Executes%20the%20SQL%20query%20and%20prints%20it%20as%20the%20list%20'res'.%0Acur%20%3D%20con.cursor()%0Acur.execute('SELECT%20*%20FROM%20dbo.tablename')%0Ares%20%3D%20cur.fetchall()%0Afor%20row%20in%20res%3A%0A%20%20%20%20print(row)%0A%20%20%0A%23Converts%20the%20list%20'res'%20to%20a%20pandas%20DataFrame.%0Adf_ora%20%3D%20pd.DataFrame(res)%0Aprint(df_ora)%0A%20%20%0A%23Runs%20jmp.run_JSL%20to%20get%20the%20DataFrame%20'df_ora'.%0Ajmp.run_jsl('python%20get%20(%22df_ora%22)%20%26lt%3B%26lt%3B%20newdataview%3B')%20%20%20%3C%2FPORT%3E%3C%2FSERVERNAME%3E%3C%2FSERVICE%3E%3C%2FPASSWORD%3E%3C%2FUSERNAME%3E%3C%2FPRE%3E%0A%3CP%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-632953%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3E%E8%B3%87%E6%96%99%E5%AD%98%E5%8F%96%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3E%E5%85%B6%E4%BB%96%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Choose Language Hide Translation Bar
Importing data using the JMP Python environment with ODBC in JMP® 18 and later

If you've wondered how to import your database using JMP's embedded Python environment, here are some examples that may help you get started. In the first example, I am importing data from a SQL Server database using the pyodbc modules; I use the OracleDB module in the second example. If you are familiar with SQL and Python and need to quickly execute SQL without leaving JMP, I think you'll enjoy these quick tips.

Note: You can use pyodbc with numerous database management systems (MySQL, SQL Server, PostgreSQL, etc.). Oracledb can only be used to establish Oracle database connections. 

Pre-requisites:

  • ODBC 3.5 Unicode driver compatible with your database management system (DBMS)
  • JMP 18 or higher
  • Python modules: Pandaspyodbc, or OracleDB

Example 1: Using the Python module 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 = '<driver name>'
db = '<database>'
server = '<server>'
uid = '<username>'
pw = '<password>'

#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;'
);

Example 2: Using the Python module 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='<username>', password='<password>', service_name='<service name>', host='<servername>', port='<port number>')

#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;')   

 

Last Modified: Sep 16, 2024 9:00 AM