Urgent.News

What's breaking now, across thousands of outlets.

Tech

SQL fluido sin riesgos de inyección: El QueryBuilder de wsqlite.

SQL fluido sin riesgos de inyección: El QueryBuilder de wsqlite. Formatear strings de SQL en Python es el camino más rápido a una fuga de datos. El QueryBuilder fluido de wsqlite te permite crear consultas con JOINs y agrupaciones con inmunidad total a inyecciones. Así se implementa QueryBuilder Fluent SQL API en entornos reales con wsqlite : from wsqlite.builders import QueryBuilder from…

Original Spanish Read in English

SQL injection-free fluid SQL: Introducing wsqlite's QueryBuilder. Writing SQL strings in Python often leads to data leaks. The fluid QueryBuilder of wsqlite enables the creation of queries with JOINs and aggregations while providing complete protection against injections. Here's how to implement the QueryBuilder Fluent SQL API in real-world scenarios with wsqlite: import the QueryBuilder from wsqlite.builders and the get_connection from wsqlite.core.connection.

Use the `with get_connection(ecommerce.db) as conn:` context manager to establish a connection. Inside the connection block, employ the QueryBuilder to select specific user columns and count associated orders using `QueryBuilder(users).select(users.id, users.name, COUNT(orders.id) as order_count)`. Join the `users` and `orders` tables on the user_id relationship, using a LEFT JOIN.

Apply filters to include only active users with `where(users.status = 'active')`. Group the results by user ID with `group_by(users.id)`. Filter groups with an order count greater than 5 using `having(order_count > 5)`. Sort the results by the order count in descending order using `order_by(order_count, DESC)`. Limit the output to the top 10 users with `limit(10)`.

Finally, execute the query against the established connection. Iterate through the results and print a formatted string showing each user's name and their order count. wsqlite distinguishes itself by defining database tables using standard Pydantic v2 models. It automatically synchronizes columns upon initialization without requiring manual migrations.

The connection pool is secure for multithreading, employing the Write-Ahead Logging (WAL) mode by default, capable of handling over 5,000 inserts per second. Full support for both synchronous and asynchronous operations is provided, with async/await syntax. Explore more in the GitHub repository.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Sunday 27 September →