Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mde-2590/5339164be42d82ac089a2649643955f9 to your computer and use it in GitHub Desktop.
Save mde-2590/5339164be42d82ac089a2649643955f9 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import ibm_db"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#Connecting to dashDB or DB2 database requires the following information:\n",
"\n",
"#Driver Name\n",
"#Database name\n",
"#Host DNS name or IP address\n",
"#Host port\n",
"#Connection protocol\n",
"#User ID (or username)\n",
"#User Password\n",
"\n",
"\n",
"#Replace the placeholder values with your actual Db2 hostname, username, and password:\n",
"dsn_hostname = \"dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net\" # e.g.: \"dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net\"\n",
"dsn_uid = \"dfk30111\" # e.g. \"abc12345\"\n",
"dsn_pwd = \"b6q01rtcjz^q4sk8\" # e.g. \"7dBZ3wWt9XN6$o0J\"\n",
"\n",
"dsn_driver = \"{IBM DB2 ODBC DRIVER}\"\n",
"dsn_database = \"BLUDB\" # e.g. \"BLUDB\"\n",
"dsn_port = \"50000\" # e.g. \"50000\" \n",
"dsn_protocol = \"TCPIP\" # i.e. \"TCPIP\"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DRIVER={IBM DB2 ODBC DRIVER};DATABASE=BLUDB;HOSTNAME=dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net;PORT=50000;PROTOCOL=TCPIP;UID=dfk30111;PWD=b6q01rtcjz^q4sk8;\n"
]
}
],
"source": [
"#DO NOT MODIFY THIS CELL. Just RUN it with Shift + Enter\n",
"#Create the dsn connection string\n",
"dsn = (\n",
" \"DRIVER={0};\"\n",
" \"DATABASE={1};\"\n",
" \"HOSTNAME={2};\"\n",
" \"PORT={3};\"\n",
" \"PROTOCOL={4};\"\n",
" \"UID={5};\"\n",
" \"PWD={6};\").format(dsn_driver, dsn_database, dsn_hostname, dsn_port, dsn_protocol, dsn_uid, dsn_pwd)\n",
"\n",
"#print the connection string to check correct values are specified\n",
"print(dsn)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connected to database: BLUDB as user: dfk30111 on host: dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net\n"
]
}
],
"source": [
"#DO NOT MODIFY THIS CELL. Just RUN it with Shift + Enter\n",
"#Create database connection\n",
"\n",
"try:\n",
" conn = ibm_db.connect(dsn, \"\", \"\")\n",
" print (\"Connected to database: \", dsn_database, \"as user: \", dsn_uid, \"on host: \", dsn_hostname)\n",
"\n",
"except:\n",
" print (\"Unable to connect: \", ibm_db.conn_errormsg() )"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DBMS_NAME: DB2/LINUXX8664\n",
"DBMS_VER: 11.01.0303\n",
"DB_NAME: BLUDB\n"
]
}
],
"source": [
"#Retrieve Metadata for the Database Server\n",
"server = ibm_db.server_info(conn)\n",
"\n",
"print (\"DBMS_NAME: \", server.DBMS_NAME)\n",
"print (\"DBMS_VER: \", server.DBMS_VER)\n",
"print (\"DB_NAME: \", server.DB_NAME)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment