Python uchun oddiy PostgreSQL wrapper
Project description
PostgresDB
Oddiy va qulay Python wrapper PostgreSQL bazasi bilan ishlash uchun.
O'rnatish
pip install postgresdb3
Foydalanish (Sync)
from postgresdb3 import PostgresDB
# Bazaga ulanish
db = PostgresDB(
database="mydb",
user="postgres",
password="mypassword",
host="localhost", # ixtiyoriy
port=5432 # ixtiyoriy
)
# Jadval yaratish
db.create("users", "id SERIAL PRIMARY KEY, name VARCHAR(100), age INT")
# Ma'lumot qo'shish
db.insert("users", "name, age", ("Ali", 25))
# Ma'lumotlarni olish
users = db.select("users")
print(users)
# Bitta qatorni olish
user = db.select("users", where=("name=%s", ["Ali"]), fetchone=True)
print(user)
# Shart bilan ma'lumot olish
adults = db.select("users", where=("age > %s", [18]))
print(adults)
# Bir nechta shart
specific_users = db.select("users", where=("age > %s AND name = %s", [18, "Ali"]))
print(specific_users)
# Jadvaldagi ma'lumotni yangilash
db.update("users", "age", 26, "name", "Ali")
# Ma'lumotni o'chirish
db.delete("users", "name", "Ali")
# Jadvalni o'chirish
db.drop("users")
Foydalanish (Async)
import asyncio
from postgresdb3 import AsyncPostgresDB
async def main():
# Bazaga ulanish
db = AsyncPostgresDB(
database="mydb",
user="postgres",
password="mypassword",
host="localhost", # ixtiyoriy
port=5432 # ixtiyoriy
)
# Jadval yaratish
await db.create("users", "id SERIAL PRIMARY KEY, name VARCHAR(100), age INT")
# Ma'lumot qo'shish
await db.insert("users", "name, age", ["Ali", 25])
await db.insert("users", "name, age", ["Vali", 30])
# Barcha ma'lumotlarni olish
users = await db.select("users")
print("Barcha foydalanuvchilar:", users)
# Bitta qatorni olish
user = await db.select("users", where=("name=$1", ["Ali"]), fetchone=True)
print("Foydalanuvchi Ali:", user)
# Shart bilan ma'lumot olish
adults = await db.select("users", where=("age > $1", [18]))
print("Kattalar:", adults)
# Bir nechta shart
specific_users = await db.select("users", where=("age > $1 AND name = $2", [18, "Ali"]))
print("Maxsus foydalanuvchi:", specific_users)
# Jadvaldagi ma'lumotni yangilash
await db.update("users", "age", 26, "name", "Ali")
# Ma'lumotni o'chirish
await db.delete("users", "name", "Ali")
# Jadvalni o'chirish
await db.drop("users")
# Connection poolni yopish
await db.close_pool()
# Asinxron loop ishga tushirish
asyncio.run(main())
Parametrlar va metodlar
PostgresDB(database, user, password, host="localhost", port=5432) --- bazaga ulanish.
create(table, columns) --- jadval yaratish, columns SQL
sintaksisida.
drop(table) --- jadvalni o'chirish.
insert(table, columns, values) --- ma'lumot qo'shish.
select(table, columns="*", where=None, join=None, group_by=None, order_by=None, limit=None, fetchone=False) --- ma'lumotlarni olish.
where misollar:
db.select("users", where=("age > %s", [18]))
db.select("users", where=("age > %s AND name = %s", [18, "Ali"]))
join misol:
db.select("orders", join=[("INNER JOIN", "users", "users.id = orders.user_id")])
update(table, set_column, set_value, where_column, where_value) --- ma'lumotni yangilash.
delete(table, where_column, where_value) --- ma'lumotni o'chirish.
manager(sql, params=None, commit=False, fetchall=False, fetchone=False) --- barcha SQL amallarni bajaruvchi yagona metod.
Qo‘shimcha
%s bilan parametrizatsiya qilish xavfsiz va SQL injection'dan himoya qiladi.
where va join list yoki tuple yordamida murakkab so‘rovlar yozish mumkin.
limit va order_by parametrlaridan foydalanib ma'lumotlarni tartiblash va cheklash oson.
commit=True bo‘lgan amallar darhol bazaga yoziladi, select() esa hech qachon o‘zgartirish kiritmaydi.
manager() — barcha SQL buyruqlarini bajaruvchi yagona metod bo‘lib, kerak bo‘lganda to‘g‘ridan‑to‘g‘ri SQL yozish imkonini beradi.
PostgreSQL bilan ishlash uchun psycopg2 kutubxonasi talab qilinadi.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file postgresdb3-0.3.0.tar.gz.
File metadata
- Download URL: postgresdb3-0.3.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
368f16f6f29d3859e86f2f7bcab02309c65df68123c74f6906e9d0fd04b05703
|
|
| MD5 |
1f6d4762ea3144b9be52277ad8c89ddb
|
|
| BLAKE2b-256 |
a49c17b651210cd8a5535e4e9f7717c4f7641f7d3eab1e0424f08987958d848a
|
File details
Details for the file postgresdb3-0.3.0-py3-none-any.whl.
File metadata
- Download URL: postgresdb3-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d4b919dfdebc5769ecff9fa7a025bbbc6e64b0eb9d16be5f004360ba83001f7
|
|
| MD5 |
98e30a1f480cc16403a01b1059be6e9e
|
|
| BLAKE2b-256 |
78fb9d379cf035cbb91d4c4a8e5c5fd8d77c4bf9dee7d749e9f32a7756231f8b
|