GithubHelp home page GithubHelp logo

carlossilva2 / knexpy Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 658 KB

An abstraction for SQLite3 based on Knexjs

Home Page: https://knexpy.readthedocs.io

License: GNU General Public License v3.0

Python 100.00%
abstraction human-readable module python3 sql sqlite3 knex

knexpy's Introduction

Knexpy

Knexpy License Downloads Supported Versions Documentation Status Black pre-commit Buy Me A Coffee

A query builder for SQLite3 based on Knexjs

Features

  • Transactions
  • Type Checking
  • Bulk Insert
  • JSON mapping

You can report bugs and discuss feature on the GitHub issues page. For more detailed information check Readthedocs

Examples

Creating a Table

from Knexpy import Knex, Field

db = Knex("<Database File Name/Path>")

db.table(
    "c",
    [
        Field.integer("field"),
    ],
    not_exists=False,
)

db.table(
    "t",
    [
        Field.varchar("field"), # Default size: 255
        Field.varchar("field2"),
        Field.varchar("field3"),
        Field.foreign_key("field4", "c", "id"),
    ],
    not_exists=False, # IF NOT EXISTS clause. Defaults to True
)

When creating a table the fields id, created_at, modified_at are automatically generated. The id field is a hash based on the information of the Row

Basic Select

from Knexpy import Knex

db = Knex("<Database File Name/Path>")

query = (
    db.select("id", "field", "field2", ["field3", "test"],...) # List type on select acts as an alias
    .from_("t")
    .where("field", "=", "12345")
    .order_by("id")
)

query.query() # Returns data as JSON
query.query(False) # Returns data as tuples

Select with Subquery

from Knexpy import Knex

db = Knex("<Database File Name/Path>")

query = (
    db.select("id", "field", "field2", ["field3", "test"],...)
    .from_("t")
    .where("field", "=", "12345")
    .where(
        "field4",
        "=",
        db.subquery().select("id").from_("c").where("field", "=", 12345),
        join_type="OR", # If attribute not present defaults to "AND".
    )
    .order_by("id")
)

query.query() # Returns data as JSON

Insert JSON Data

from Knexpy import Knex

db = Knex("<Database File Name/Path>", type_check=True) # type_check enables type checking (duh) when inserting/updating data

db.insert_json("<table>", {
    "field": "1",
    "field2": "2",
    "field3": "3"
})

knexpy's People

Contributors

carlossilva2 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.