Skip to main content

Simple database connector for python

Project description

Simple_Python_DB_Connector

Python module for simple connection and editing of databases.

Installation

To install the python module, enter the following code in the terminal. Remember, "python" must be swapped with the Python interpreter installed on your system:

python -m pip install simple-db-connector

Usage Example

Call Database Class

First of all, we call the "database" class. Im using the os modul to call my database parameter from my environment variables. I advise you to do this straight away, as it is very bad to have sensitive data such as passwords and IP addresses in plain text:

import  os
from  simple_db_connector  import  database

# Call the database class
db  =  database(
	os.environ["db_url"],
	os.environ["db_user"],
	os.environ["db_password"],
	os.environ["db_port"],
	os.environ["db_name"],
)

After we have called the class, we can also use the functions of the class. All functions are self-explanatory. Nevertheless, I will briefly explain the use of each function:

class database.check_table

Parameter

table name: string

# Example Table Name
table_name = "test_table"

# if "test_table" exists return true, otherwise return false
print(db.check_table(table_name))

It is also executed within the create_table function, so it is not necessary to execute this before you create a table.

class database.create_table

Parameter

table name: string, table content: dict

# Example Table Name
table_name = "cars_table"

# Example Table Content
table_content = {
	"manufacture" : "toyoat",
	"model" : "Aygo X yalp",
	"ps" : 72, 
} 

# Example Table creation
db.create_table(table_name, table_content)

This will create the table "cars_table" with the information contained in "test_content". In addition, a primary key field with the name "id" will be created too. Currently this field is hard coded with a GUID field. In the near future, however, it will be possible to declare your own primary key field.

class database.check_db_entry

Parameter

table: string, search_parameter: dict

# Example Table Name 
table_name =  "cars_table"  

# Example Search Parameter
search_parameter =  {
	"manufacture" : "toyoat"
}  

# Example Table creation 
db.check_db_entry(table_name, search_parameter)

This will create the table "cars_table" with the information contained in "test_content". In addition, a primary key field with the name "ID" will be created too. Currently this field is hard coded with a GUID field. In the near future, however, it will be possible to declare your own primary key field.

# Example Table Name 
table_name =  "cars_table"  

# Example Search Parameter
search_parameter =  {
	"manufacture" : "toyoat",
	"model" : "Aygo X yalp",
}
# Example Search Operator
search_operator = ["AND"]

# Example Table creation 
db.check_db_entry(table_name, search_parameter, search_operator)

It's also possible to have multiple search parameter

class database.create_db_entry

Parameter

table: string, data: dict, prime_key: string; default value = "id"

# Example Table Name 
table_name =  "cars_table"  

# Example Table Content
table_content = {
	"manufacture" : "toyoat",
	"model" : "Aygo X yalp",
	"ps" : 72, 
} 

# Example Table prime key
prime_key = "id"
# To illustrate this, I have entered "id". However, if "prime_key" is empty, "id" is selected.

# Example 
db.create_db_entry(table_name, table_content, prime_key)

class database.get_db_entrys

Parameter

table: string, search_parameter: dict

# Example Table Name 
table_name =  "cars_table"  

# Example Search Parameter
search_parameter =  {
	"manufacture" : "toyoat"
} 

# Example Table creation 
db.get_db_entrys(table_name, search_parameter)
# Example Table Name 
table_name =  "cars_table"  

# Example Search Parameter
search_parameter =  {
	"manufacture" : "toyoat",
	"model" : "Aygo X yalp",
}
# Example Search Operator
search_operator = ["AND"]

# Example Table creation 
db.get_db_entrys(table_name, search_parameter, search_operator)

As with check_db_entry, it is also possible to use several search parameters here

class database.update_entry

Parameter

table: string, search_parameter: dict, update_parameter dict

# Example Table Name 
table_name =  "cars_table"  

# Example Search Parameter
search_parameter =  {
	"manufacture"  :  "toyoat"
}  
# Example Update Parameter
search_parameter =  {
	"manufacture"  :  "toyota"
} 

# Example Table creation 
db.update_entry(table_name, search_parameter)
# Example Table Name 
table_name =  "cars_table"  

# Example Search Parameter
search_parameter =  {
	"manufacture" : "toyoat",
	"model" : "Aygo X yalp",
}
# Example Update Parameter
search_parameter =  {
	"manufacture"  :  "toyota"
	"model" : "Aygo X play",
} 
# Example Search Operator
search_operator = ["AND"]

# Example Table creation 
db.update_entry(table_name, search_parameter, search_operator)

It's also possible to use multiple search and update parameter here

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

simple_db_connector-0.0.8.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

simple_db_connector-0.0.8-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file simple_db_connector-0.0.8.tar.gz.

File metadata

  • Download URL: simple_db_connector-0.0.8.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for simple_db_connector-0.0.8.tar.gz
Algorithm Hash digest
SHA256 02c1cbfe07865745e102f9d129547105c2151e51dca24da8b536e9db04312201
MD5 c35323f48188370de3711049a322bf26
BLAKE2b-256 41e0fa5e3f8b6e3a8f24ff07882b587ce66fe79e3360efdf5ab79776bc40711e

See more details on using hashes here.

File details

Details for the file simple_db_connector-0.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_db_connector-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 651d5be5ddeaf045f7c0dab35c29a66e4e582017663da67d5c066daa27e12696
MD5 964208298def633277e1b967e29c90d7
BLAKE2b-256 9edc5f562db2dc9c8b3659d33a912b6a04d3efe3c007f5342a7738a9e5a9bec8

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