Skip to content

Commit

Permalink
Client driver for YSQL / Python (#4)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ravimurthy authored Feb 21, 2019
1 parent e79e918 commit 14c4adb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/content/latest/develop/client-drivers/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ menu:
YEDIS
</a>
</li>
<li>
<a href="#ysql" class="nav-link" id="ysql-tab" data-toggle="tab" role="tab" aria-controls="ysql" aria-selected="false">
<i class="icon-ysql" aria-hidden="true"></i>
YSQL
</a>
</li>
</ul>

<div class="tab-content">
Expand All @@ -33,4 +39,7 @@ menu:
<div id="redis" class="tab-pane fade" role="tabpanel" aria-labelledby="redis-tab">
{{% includeMarkdown "redis/python.md" /%}}
</div>
<div id="ysql" class="tab-pane fade" role="tabpanel" aria-labelledby="ysql-tab">
{{% includeMarkdown "ysql/python.md" /%}}
</div>
</div>
71 changes: 71 additions & 0 deletions docs/content/latest/develop/client-drivers/ysql/python.md
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit 14c4adb

Please sign in to comment.