From 8f86efcee944b172eb9c525077b3b2425e5730ae Mon Sep 17 00:00:00 2001 From: mikeqfu Date: Mon, 20 Jan 2020 12:21:25 +0000 Subject: [PATCH] updated README.md --- README.md | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3b37959..5117566 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ A toolkit of helper functions to facilitate data manipulation. ## Installation ``` -pip install --upgrade pyhelpers +pip install pyhelpers ``` **Note:** @@ -46,6 +46,7 @@ The current version includes the following modules: - [`geom`](#geom) - [`text`](#text) - [`ops`](#ops) +- [`sql`](#sql) There are a number of functions included in each of the above-listed modules. For a quick start, one example is provided for each module to demonstrate how '**pyhelpers**' may assist you in your work. @@ -170,7 +171,8 @@ import pandas as pd xy_array = np.array([(530034, 180381), # London (406689, 286822), # Birmingham (383819, 398052), # Manchester - (582044, 152953)]) # Leeds + (582044, 152953)], # Leeds + dtype=np.int64) dat = pd.DataFrame(xy_array, columns=['Easting', 'Northing']) ``` @@ -308,3 +310,67 @@ Continue?... [No]|Yes: If you input `Yes` (or `Y`, `yes`, or something like `ye`), it should return `True`; otherwise, `False` given the input being `No` (or something like `n`). When `confirmation_required` is `False`, this function would be null, as it would always return `True`. + + +### sql + +This module provides a convenient way to establish a connection with a SQL server. The current release supports only [PostgreSQL](https://www.postgresql.org/). A quick example is given below. + +```python +from pyhelpers.sql import PostgreSQL +``` + +Now you can connect the PostgreSQL server by specifying **host** (default: `'localhost'`), **port** (default: `5432`), **username** (default: `'postgres'`), **password** and **name of the database** (default: `'postgres'`) + +```python +# Connect to 'postgres' (using all default parameters) +test_db = PostgreSQL(host='localhost', port=5432, username='postgres', password=None, + database_name='postgres', verbose=True) + +# Or simply, +# test_db = PostgreSQL() +``` + +If `password=None`, you will be asked to input your password. + +After you have successfully established the connection, you could try to dump `dat` ([see above](#store)) into the database "postgres": + +```python +test_db.dump_data(dat, table_name='test_table', schema_name='public', + if_exists='replace', chunk_size=None, + force_replace=False, col_type=None, verbose=True) +``` + +To retrieve the dumped data: + +```python +dat_retrieved = test_db.read_table('test_table') +# print(dat.equals(dat_retrieved)) +``` + +To drop the table 'test_table' (If `confirmation_required=True`(default: False), you will be asked to confirm whether you are sure to drop the table): + +```python +test_db.drop_table('test_table', confirmation_required=True, verbose=True) +# Confirmed to drop the table "test_table" from the database "postgres"? [No]|Yes: +# >? Yes +# The table "test_table" has been dropped successfully. +``` + +If you would like to create your own database, for example, which is named 'test_database': + +```python +test_db.create_db("test_database", verbose=True) + +# After successfully creating the database, you could check +print(test_db.database_name) +``` + +To drop this database, similarly, you will also be asked to confirm whether you are sure to drop the database if `confirmation_required=True` (default): + +```python +test_db.drop(confirmation_required=True, verbose=True) +# Confirmed to drop the database "test_database" for postgres@localhost? [No]|Yes: +# >? Yes +``` +