Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "import ibm_db;"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "#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 = \"bkl21830\" # e.g. \"abc12345\"\n",
- "dsn_pwd = \"1nhzc^3dv4cn9pn9\" # 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\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "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=bkl21830;PWD=1nhzc^3dv4cn9pn9;\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": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Connected to database: BLUDB as user: bkl21830 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() )\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [],
- "source": [
- "dropQuery = \"drop table INSTRUCTOR\"\n",
- "dropStmt = ibm_db.exec_immediate(conn,dropQuery)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "module 'ibm_db' has no attribute 'replace_with_name_of_execution_method'",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-9-c75c773fd9a3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mcreateQuery\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"create table INSTRUCTOR(id INTEGER PRIMAR KEY NOT NULL, fname VARCHAR(10),lname VARCHAR(10),city VARCHAR(10),ccode CHAR(2))\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcreatestmt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mibm_db\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreplace_with_name_of_execution_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcreateQuery\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0m: module 'ibm_db' has no attribute 'replace_with_name_of_execution_method'"
- ]
- }
- ],
- "source": [
- "createQuery = \"create table INSTRUCTOR(id INTEGER PRIMAR KEY NOT NULL, fname VARCHAR(10),lname VARCHAR(10),city VARCHAR(10),ccode CHAR(2))\"\n",
- "createstmt = ibm_db.replace_with_name_of_execution_method(conn, createQuery)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "module 'ibm_db' has no attribute 'replace_with_name_of_execution_method'",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-10-c75c773fd9a3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mcreateQuery\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"create table INSTRUCTOR(id INTEGER PRIMAR KEY NOT NULL, fname VARCHAR(10),lname VARCHAR(10),city VARCHAR(10),ccode CHAR(2))\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcreatestmt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mibm_db\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreplace_with_name_of_execution_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcreateQuery\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;31mAttributeError\u001b[0m: module 'ibm_db' has no attribute 'replace_with_name_of_execution_method'"
- ]
- }
- ],
- "source": [
- "createQuery = \"create table INSTRUCTOR(id INTEGER PRIMAR KEY NOT NULL, fname VARCHAR(10),lname VARCHAR(10),city VARCHAR(10),ccode CHAR(2))\"\n",
- "createstmt = ibm_db.replace_with_name_of_execution_method(conn, createQuery)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "ename": "Exception",
- "evalue": "[IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token \"KEY\" was found following \"OR(id INTEGER PRIMAR\". Expected tokens may include: \"<references_spec>\". SQLSTATE=42601 SQLCODE=-104",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mException\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-11-7a276474dc18>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mcreateQuery\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"create table INSTRUCTOR(id INTEGER PRIMAR KEY NOT NULL, fname VARCHAR(10),lname VARCHAR(10),city VARCHAR(10),ccode CHAR(2))\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcreatestmt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mibm_db\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexec_immediate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcreateQuery\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;31mException\u001b[0m: [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token \"KEY\" was found following \"OR(id INTEGER PRIMAR\". Expected tokens may include: \"<references_spec>\". SQLSTATE=42601 SQLCODE=-104"
- ]
- }
- ],
- "source": [
- "createQuery = \"create table INSTRUCTOR(id INTEGER PRIMARY KEY NOT NULL, fname VARCHAR(10),lname VARCHAR(10),city VARCHAR(10),ccode CHAR(2))\"\n",
- "createstmt = ibm_db.exec_immediate(conn, createQuery)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [],
- "source": [
- "createQuery = \"create table INSTRUCTOR(id INTEGER PRIMARY KEY NOT NULL, fname VARCHAR(10),lname VARCHAR(10),city VARCHAR(10),ccode CHAR(2))\"\n",
- "createstmt = ibm_db.exec_immediate(conn, createQuery)\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [],
- "source": [
- "insertQuery = \"INSERT INTO INSTRUCTOR(id,fname,lname,city,ccode) values (1,'Rav','Ahuja','Toronto','CA'),(2,'Raul','Chong','Markham','CA'),(3,'Hima','Vasudevan','Chicago','US')\"\n",
- "insertStmt = ibm_db.exec_immediate(conn,insertQuery)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [],
- "source": [
- "selectQuery = \"select * from INSTRUCTOR\"\n",
- "selectstmt = ibm_db.exec_immediate(conn,selectQuery)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'ID': 1,\n",
- " 0: 1,\n",
- " 'FNAME': 'Rav',\n",
- " 1: 'Rav',\n",
- " 'LNAME': 'Ahuja',\n",
- " 2: 'Ahuja',\n",
- " 'CITY': 'Toronto',\n",
- " 3: 'Toronto',\n",
- " 'CCODE': 'CA',\n",
- " 4: 'CA'}"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "ibm_db.fetch_both(selectstmt)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [],
- "source": [
- "import pandas"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'ID': 2,\n",
- " 0: 2,\n",
- " 'FNAME': 'Raul',\n",
- " 1: 'Raul',\n",
- " 'LNAME': 'Chong',\n",
- " 2: 'Chong',\n",
- " 'CITY': 'Markham',\n",
- " 3: 'Markham',\n",
- " 'CCODE': 'CA',\n",
- " 4: 'CA'}"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "ibm_db.fetch_both(selectstmt)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [],
- "source": [
- "while ibm_db.fetch_row(selectstmt) != False:\n",
- " print(\" ID:\", ibm_db.result(selectstmt,0), \"Fname: \", ibm_db.result(selectstmt,\"fname\"))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [],
- "source": [
- "updateQuery = \"update INSTRUCTOR set CITY='MOOSETOWN' WHERE FNAME ='Rav'\"\n",
- "updatestmt = ibm_db.exec_immediate(conn,updateQuery)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [],
- "source": [
- "import ibm_db_dbi"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [],
- "source": [
- "pconn = ibm_db_dbi.Connection(conn)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {},
- "outputs": [],
- "source": [
- "selectQuery = \"select *from INSTRUCTOR\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [],
- "source": [
- "pdf = pandas.read_sql(selectQuery,pconn\n",
- " )"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'Ahuja'"
- ]
- },
- "execution_count": 31,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pdf.LNAME[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style scoped>\n",
- " .dataframe tbody tr th:only-of-type {\n",
- " vertical-align: middle;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: right;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>ID</th>\n",
- " <th>FNAME</th>\n",
- " <th>LNAME</th>\n",
- " <th>CITY</th>\n",
- " <th>CCODE</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>1</td>\n",
- " <td>Rav</td>\n",
- " <td>Ahuja</td>\n",
- " <td>MOOSETOWN</td>\n",
- " <td>CA</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>2</td>\n",
- " <td>Raul</td>\n",
- " <td>Chong</td>\n",
- " <td>Markham</td>\n",
- " <td>CA</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2</th>\n",
- " <td>3</td>\n",
- " <td>Hima</td>\n",
- " <td>Vasudevan</td>\n",
- " <td>Chicago</td>\n",
- " <td>US</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " ID FNAME LNAME CITY CCODE\n",
- "0 1 Rav Ahuja MOOSETOWN CA\n",
- "1 2 Raul Chong Markham CA\n",
- "2 3 Hima Vasudevan Chicago US"
- ]
- },
- "execution_count": 32,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pdf"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(3, 5)"
- ]
- },
- "execution_count": 33,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pdf.shape\n",
- "\n"
- ]
- },
- {
- "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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement