Build a Python application
The following tutorial creates a simple Python application that connects to a YugabyteDB cluster using the
aiopg database adapter, performs a few basic database operations — creating a table, inserting data, and running a SQL query — and prints the results to the screen.
Before you begin
This tutorial assumes that you have satisfied the following prerequisites.
YugabyteDB is up and running. If you are new to YugabyteDB, you can have YugabyteDB up and running within five minutes by following the steps in Quick start.
Python 3, or later, is installed.
aiopg database adapter
aiopg package is installed. To install the package using
$ pip3 install aiopg
For details about using this database adapter, see aiopg documentation.
Create the sample Python application
Create a file
yb-sql-helloworld.py and add the following content to it.
import asyncio import aiopg dsn = 'dbname=yugabyte user=yugabyte password=yugabyte host=127.0.0.1 port=5433' async def go(): async with aiopg.create_pool(dsn) as pool: async with pool.acquire() as conn: # Open a cursor to perform database operations. async with conn.cursor() as cur: await cur.execute(f""" DROP TABLE IF EXISTS employee; CREATE TABLE employee (id int PRIMARY KEY, name varchar, age int, language varchar); """) print("Created table employee") # Insert a row. await 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. await cur.execute("SELECT name, age, language FROM employee WHERE id = 1") async for row in cur: print("Query returned: %s, %s, %s" % (row, row, row)) loop = asyncio.get_event_loop() loop.run_until_complete(go())
Run the application
To use the application, run the following Python script you just created.
$ 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