Skip to main content

A lightweight PostgreSQL database driver for MicroPython, designed for microcontrollers (e.g., ESP8266) with limited RAM.

Project description

micropg_lite V3

The world's lightest PostgreSQL driver for MicroPython, made for ESP8266

This README contains the most important things you need to know about micropg_lite. You can find detailed documentation in the wiki of this repository.

If there are any problems, questions, bugs or suggestions for improvement, please open an issue on this Github repository or write an email to the email address linked in my profile. We are happy to provide the necessary free support to help you with your micropg_lite requests.

About micropg_lite

Difference between micropg_lite and micropg

micropg_lite is a lightweight version based on micropg by nakagami. If you have RAM/memory issues with micropg than this library might solve this issue.

micropg_lite has been specially optimised for the ESP8266 microchip and other microchips that have little RAM. micopg_lite works on any microchip that runs micropyhton.

Major changes in micropg_lite V3

Those who have already worked with micropg_lite V2 know that the micropg_lite V2 driver has some limitations in terms of functionality. Therefore, micropg_lite V3 was optimized from scratch to bring back the functionality that is present in Nakagami's micropg.

Installation

  1. Download the micropg_lite.py file from github.com or pypi.org to your local computer.

  2. Copy the micropg_lite.py file to the "/lib" folder on the microcontroller using the Thonny IDE or another program. If there is no "lib" folder in the root directory, you have to create it.

    Hint for the Thony IDE:

    Open in the top bar the "View" menu and make sure that the entry "Files" has a "+", if not then click on "Files". Now you can directly download and upload files from your computer to the microcontroller. You also can create folders on the microcontroller.

  3. Now you should be able to import the library to your microcontroller in a MicroPython file.

import micropg_lite

microcontroller file tree

/
|- example.py
|- lib/
    |- micropg_lite.py

Examples

You need to establish a network connection before executing micropg_lite code. The SELECT example inclueds the wifi template. All other examples do not include the wifi template.

examples/ folder on github.com

The examples folder has a docker-postgres-container-setup.sh script to create an empty PostgreSQL container and an exampleDatabase.sql file which contains the database used in the examples. You will also find complete test scripts with WLAN connection templates and examples for CREATE/DROP TABLE and DATABASE in the examples folder.

SELECT example with wifi connection:

import time
import network   # Handles the wifi connection
import micropg_lite

### To Do: Fill in your wifi connection data and change the server data
ssid = 'wifissid'
password = 'secret'

# Connect to network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

while not wlan.isconnected():
    print("Wifi Status: ", wlan.status())
    time.sleep(1)


print("Wifi connected")

conn = micropg_lite.connect(host='127.0.0.1', # To Do: Replace this string with the IP address of your server
                    user='postgres',
                    password='123456',
                    database='exampledatabase')
cur = conn.cursor()

cur.execute('select * from customers')
selectresult = cur.fetchall()
conn.close()

# EXAMPLE: Print raw data
print(selectresult)

# EXAMPLE: Print data table
for r1 in selectresult:
    for r2 in r1:
        print(r2, end="\t")
    print()

INSERT example

conn = micropg_lite.connect(host='127.0.0.1', # To Do: Replace this string with the IP address of your server
                    user='postgres',
                    password='123456',
                    database='exampledatabase')
cur = conn.cursor()

cur.execute('INSERT INTO customers (id, firstName, lastName, email) values (%s, %s, %s, %s)', ['5', 'David', 'Wilson', 'david.wilson@example.com'])
conn.commit()
conn.close()

UPDATE example

conn = micropg_lite.connect(host='127.0.0.1', # To Do: Replace this string with the IP address of your server
                    user='postgres',
                    password='123456',
                    database='exampledatabase')
cur = conn.cursor()

cur.execute("update customers set firstName='UpdatedFirstName' where id=2;")
conn.commit()
conn.close()

DELETE example

conn = micropg_lite.connect(host='127.0.0.1', # To Do: Replace this string with the IP address of your server
                    user='postgres',
                    password='123456',
                    database='exampledatabase')
cur = conn.cursor()

cur.execute("delete from customers where id=1;")
conn.commit()
conn.close()

micropg_lite limitations

  • reduced error handling
  • no MD5 auth method support
  • No native support for the so-called "executemany" function

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

micropg_lite-3.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

micropg_lite-3.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file micropg_lite-3.1.1.tar.gz.

File metadata

  • Download URL: micropg_lite-3.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for micropg_lite-3.1.1.tar.gz
Algorithm Hash digest
SHA256 ba04814b2a6b45bac3977c5c812324e39b4ef8a4e7ca7ef2db1f03447bcf6e1e
MD5 af77ed32c5e3e410df633135ebf74416
BLAKE2b-256 33d5741603a6c2990bc0bbe332c50c26dbd17c8f74ecfc15542f8237cf7bca14

See more details on using hashes here.

File details

Details for the file micropg_lite-3.1.1-py3-none-any.whl.

File metadata

  • Download URL: micropg_lite-3.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for micropg_lite-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 23edec5e64a25f4f69824b8def3645456a73bb5dad2ae3860bcca6394b5fe33e
MD5 1bb8e809eeb616b8494a7935de5f7d0c
BLAKE2b-256 e0c9e77166c198211a7132509f0cb2ef5dce9e0af72dccefcc11965da84a23cf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page