diff --git a/examples/nzpywithpanda1.ipynb b/examples/nzpywithpanda1.ipynb new file mode 100644 index 0000000..5fd1c8f --- /dev/null +++ b/examples/nzpywithpanda1.ipynb @@ -0,0 +1,447 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Nzpy simple load and unload example" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Setup" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "pip install nzpy" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Make connection" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import nzpy\n", + "import pandas as pd\n", + "\n", + "conn = nzpy.connect(user=\"admin\", password=\"password\",host='localhost', port=5480, database=\"DB1\", securityLevel=0)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Drop table if exists" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "cursor = conn.cursor()\n", + "try:\n", + " cursor.execute(\"drop table customerAddress\")\n", + "except nzpy.DatabaseError as e:\n", + " pass\n", + "except Exception as e:\n", + " pass\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Create table and insert rows" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cursor.execute(\"create table customerAddress(Id int, Name varchar(10), Address varchar(50), Email varchar(20) )\")\n", + "cursor.execute(\"insert into customerAddress values (1,'Jack','High street London', 'jack4321@ibm.com')\")\n", + "cursor.execute(\"insert into customerAddress values (2,'Tom', 'Park street NY','tom1234@ibm.com')\")\n", + "cursor.execute(\"insert into customerAddress values (3,'James', 'MG street SG','james678@ibm.com')\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Select rows from table using fetchall()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "colums1 = 1\n", + "colums2 = Jack\n", + "colums3 = High street London\n", + "colums1 = 2\n", + "colums2 = Tom\n", + "colums3 = Park street NY\n", + "colums1 = 3\n", + "colums2 = James\n", + "colums3 = MG street SG\n" + ] + } + ], + "source": [ + "cursor.execute(\"select * from customerAddress\")\n", + "results = cursor.fetchall()\n", + "\n", + "for c1,c2,c3,c4 in results:\n", + " print(\"colums1 = %s\" % (c1))\n", + " print(\"colums2 = %s\" % (c2))\n", + " print(\"colums3 = %s\" % (c3))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Select rows from table using pandas read_sql_query()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
idnameaddressemail
01JackHigh street Londonjack4321@ibm.com
12TomPark street NYtom1234@ibm.com
23JamesMG street SGjames678@ibm.com
\n", + "
" + ], + "text/plain": [ + " id name address email\n", + "0 1 Jack High street London jack4321@ibm.com\n", + "1 2 Tom Park street NY tom1234@ibm.com\n", + "2 3 James MG street SG james678@ibm.com" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result = pd.read_sql_query(\"select * from customerAddress\", conn)\n", + "result.columns = [c.decode().lower() for c in result.columns]\n", + "result\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Create external table and dump data to csv file" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
idnameaddressemail
01JackHigh street Londonjack4321@ibm.com
12TomPark street NYtom1234@ibm.com
23JamesMG street SGjames678@ibm.com
\n", + "
" + ], + "text/plain": [ + " id name address email\n", + "0 1 Jack High street London jack4321@ibm.com\n", + "1 2 Tom Park street NY tom1234@ibm.com\n", + "2 3 James MG street SG james678@ibm.com" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "with conn.cursor() as cursor:\n", + " cursor.execute(\"create external table 'cust.csv' using ( remotesource 'python' delim ',' logDir '.') as select * from customerAddress\")\n", + "\n", + "df = pd.read_csv('cust.csv', names=['id', 'name', 'address', 'email'])\n", + "df\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Read data from csv file and load into table" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "with conn.cursor() as cursor:\n", + " cursor.execute(\"insert into customerAddress select * from external 'cust.csv' using (delim ',' remotesource 'python' logDir '.')\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
idnameaddressemail
01JackHigh street Londonjack4321@ibm.com
12TomPark street NYtom1234@ibm.com
23JamesMG street SGjames678@ibm.com
31JackHigh street Londonjack4321@ibm.com
42TomPark street NYtom1234@ibm.com
53JamesMG street SGjames678@ibm.com
\n", + "
" + ], + "text/plain": [ + " id name address email\n", + "0 1 Jack High street London jack4321@ibm.com\n", + "1 2 Tom Park street NY tom1234@ibm.com\n", + "2 3 James MG street SG james678@ibm.com\n", + "3 1 Jack High street London jack4321@ibm.com\n", + "4 2 Tom Park street NY tom1234@ibm.com\n", + "5 3 James MG street SG james678@ibm.com" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result = pd.read_sql_query(\"select * from customerAddress\", conn)\n", + "result.columns = [c.decode().lower() for c in result.columns]\n", + "result" + ] + }, + { + "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.7.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}