From 14c4adbd3718ada673245a2e31c06bd58a54ead0 Mon Sep 17 00:00:00 2001 From: ravimurthy <2237332+ravimurthy@users.noreply.github.com> Date: Thu, 21 Feb 2019 08:21:05 -0800 Subject: [PATCH] Client driver for YSQL / Python (#4) * Client drivers for YSQL * Client drivers for YSQL * Client Drivers for YSQL / Java * Client driver for YSQL / Go * Addressed review comments * Client drivers for YSQL / Python * Put back redis section that was mistakenly removed --- .../latest/develop/client-drivers/python.md | 9 +++ .../develop/client-drivers/ysql/python.md | 71 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 docs/content/latest/develop/client-drivers/ysql/python.md diff --git a/docs/content/latest/develop/client-drivers/python.md b/docs/content/latest/develop/client-drivers/python.md index 9efbfda50e4a..695a9e3c51b9 100644 --- a/docs/content/latest/develop/client-drivers/python.md +++ b/docs/content/latest/develop/client-drivers/python.md @@ -24,6 +24,12 @@ menu: YEDIS +
  • + + + YSQL + +
  • @@ -33,4 +39,7 @@ menu:
    {{% includeMarkdown "redis/python.md" /%}}
    +
    + {{% includeMarkdown "ysql/python.md" /%}} +
    diff --git a/docs/content/latest/develop/client-drivers/ysql/python.md b/docs/content/latest/develop/client-drivers/ysql/python.md new file mode 100644 index 000000000000..505d7c680fa4 --- /dev/null +++ b/docs/content/latest/develop/client-drivers/ysql/python.md @@ -0,0 +1,71 @@ + +## Installation + +Install the python PostgreSQL driver using the following command. You can get further details for the driver [here](https://pypi.org/project/psycopg2/). + +```sh +$ pip install psycopg2-binary +``` + +## Working Example + +### Pre-requisites + +This tutorial assumes that you have: + +- installed YugaByte DB and created a universe with YSQL enabled. If not, please follow these steps in the [quick start guide](../../../quick-start/test-ysql/). + + +### Writing the python code + +Create a file `yb-sql-helloworld.py` and add the following content to it. + +```python +import psycopg2 + +# Create the database connection. +conn = psycopg2.connect("host=127.0.0.1 port=5433 dbname=postgres user=postgres password=postgres") + +# Open a cursor to perform database operations +cur = conn.cursor() + +# Create the table. +cur.execute( + """ + CREATE TABLE employee (id int PRIMARY KEY, + name varchar, + age int, + language varchar); + """) +print "Created table employee" + +# Insert a row. +cur.execute("INSERT INTO employee (id, name, age, language) VALUES (%s, %s, %s, %s)", + (1, 'John', 35, 'Python')) +print "Inserted (id, name, age, language) = (1, 'John', 35, 'Python')" + +# Query the row. +cur.execute("SELECT name, age, language FROM employee WHERE id = 1;") +row = cur.fetchone() +print "Query returned: %s, %s, %s" % (row[0], row[1], row[2]) + +# Close the connection. +cur.close() +conn.close() +``` + +### Running the application + +To run the application, type the following: + +```sh +$ python yb-sql-helloworld.py +``` + +You should see the following output. + +``` +Created table employee +Inserted (id, name, age, language) = (1, 'John', 35, 'Python') +Query returned: John, 35, Python +```