SQL-ORM Documentation

Python SQL-ORM Project.

Namaste Friends,
               

Documentation Of Python SQL-ORM Project



So, Let's Start.


About Python SQL-ORM




'   ________  ________  ___       ________  ________  _____ ______      
'  |\   ____\|\   __  \|\  \     |\   __  \|\   __  \|\   _ \  _   \    
'  \ \  \___|\ \  \|\  \ \  \    \ \  \|\  \ \  \|\  \ \  \\\__\ \  \   
'   \ \_____  \ \  \\\  \ \  \    \ \  \\\  \ \   _  _\ \  \\|__| \  \  
'    \|____|\  \ \  \\\  \ \  \____\ \  \\\  \ \  \\  \\ \  \    \ \  \ 
'      ____\_\  \ \_____  \ \_______\ \_______\ \__\\ _\\ \__\    \ \__\
'     |\_________\|___| \__\|_______|\|_______|\|__|\|__|\|__|     \|__|
'     \|_________|     \|__|                                            
'                                                                       
'                                                                       

SQL-ORM Is a Python Object Relational Mapper for SQLite3. 
With These Script, We Can Use Python SQLite3 API Very Easly.
  • Easy To Use
  • Easy To Maintain
     

Features


  • Can Access More Than One Database At a Time.
  • Can Access More Than One Table At a Time.
  • Can Access More Than One Row At a Time.
  • No External Dependencies

Reference





Examples



Input : [1]  


#!/usr/bin/python
#
#   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#       Object InterFace Manual
#   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#   author:
#       surajsinghbisht054@gmail.com
#       https://bitforestinfo.blogspot.com
#
#
#    About sqlorm:  
#          A Python object relational mapper for SQLite3.
#
#    Reference:  https://www.sqlite.org/lang.html
#               https://docs.python.org/3/library/sqlite3.html
#
#    Import:
#      from sqlorm import Model, Field
#

Output : [1]  

  

Input : [2]  

# Import Module
from sqlorm import Field, Model

Output : [2]  

  

Input : [3]  

# Creating A Database Models Structure
#
# Here, "TestingDataBase" is used as default Sqlite Database File Name 
class TestingDataBase(Model):
    # Creating New Table. Name="AboutTeachers"
    class AboutTeachers:
        # Creating 4 Columns. Name = Name, Age, Income, Phone
        #
        #Columns     Datatype
        Name    =   Field.String
        Age     =   Field.Integer
        Income  =   Field.Integer
        Phone   =   Field.LongInt
        #

    # Creating New Table. Name="AboutBooks"
    class AboutBooks:
        # Creating 4 Columns. Name = Name, Price, Class
        #
        # Columns   DataType
        Name    =   Field.String
        Price   =   Field.Integer
        Class   =   Field.String
        #                   

    # Creating New Table. Name="AboutStudents"
    class AboutStudents:
        # Creating 4 Columns. Name = Name, Age, Class, Phone
        #
        #Columns     Datatype
        Name    =   Field.String
        Age     =   Field.Integer
        Class  =   Field.String
        Phone   =   Field.LongInt
        #

Output : [3]  

  

Input : [4]  

#
#    =====================================
#    =========== DataBase ================
#    =====================================
#
#   connect(path="", dbname=None)   --> Connect To Database
#   close()                         --> Close Database    
#   save()                          --> Save All Changes
#   get_db_names()                  --> Get Default Database Name
#   get_all_columns_(TableName)     --> Get All Columns Name List
#   get_all_table_with_class()      --> Get All Tables With Class Object As Dictonery
#   get_all_tables_name()           --> Get All Tables Name
#   get_tables()                    --> Get All Tables Object
#   Table(name=None)                --> Get Table Object
#
# Create Object For InterFace        
DBase = TestingDataBase()

# Connect To DataBase
DBase.connect()
# or 
# DBase.connect(dbname="Testing_DataBase")

Output : [4]  

[Info] Accessing Database ....  Database Ready!
[*] Checking Tables And Columns

Input : [5]  

# Get All Available Tables Names as list
print DBase.get_all_tables_name()

# Get All Tables With Interface Object In Dictionery
print DBase.get_tables()

Output : [5]  

['AboutBooks', 'AboutTeachers', 'AboutStudents']
{'AboutBooks': < TableControls:AboutBooks >, 'AboutTeachers': < TableControls:AboutTeachers >, 'AboutStudents': < TableControls:AboutStudents >}

Input : [6]  

# Get All Columns Names From Tables
for table in DBase.get_all_tables_name():
    print "Table Name : {}".format(table) 
    print DBase.get_all_columns_(table) # As List

Output : [6]  

Table Name : AboutBooks
[('Price', 'INTEGER'), ('Name', 'TEXT'), ('Class', 'TEXT')]
Table Name : AboutTeachers
[('Phone', 'INTEGER'), ('Name', 'TEXT'), ('Income', 'INTEGER'), ('Age', 'INTEGER')]
Table Name : AboutStudents
[('Phone', 'INTEGER'), ('Name', 'TEXT'), ('Age', 'INTEGER'), ('Class', 'TEXT')]

Input : [7]  

# Default Database Name 
print DBase.get_db_names()

Output : [7]  

TestingDataBase

Input : [8]  

#
#
#    =====================================
#    ============= Table =================   
#    =====================================
#
#   new()               --> New Row Interface
#   insert(**kwargs)    --> Insert New Values Into Row, id argument Required
#   update(id=None, data={}) --> Update Values Into Row
#   delete(id)          --> Delete Row , Id argument Required
#   search(**kwargs)    --> Search Row Object InterFace
#   get_all()           --> Get All Row Object InterFace as list
#   has(id)             --> Check Row
#   columns_name()      --> Get All Columns Names As List
#   save()              --> Save All Changes
#   get_tables_name()   --> Get Current Object Table Name
#   table_access(TableName) --> Change Current Object Table Access
#
#
# Get Table Interface
t = DBase.Table(name="AboutTeachers")
# or
t = DBase.get_tables()["AboutTeachers"]

print t

Output : [8]  

< TableControls:AboutTeachers >

Input : [9]  

#
# =====================================
# ============= Row ===================
# =====================================
#
#   get_all()           --> Get All Values In Dictionery
#   set_all(**kwargs)   --> Set Values
#   save()              --> Save Changes Into Database
#
#
# Create New Row Object Interface
nr = t.new()

print nr

Output : [9]  

< Row-ID:NEW >

Input : [10]  

# Set Values

nr.Age = 10        # Column Age

nr.Income = 45000  

nr.Name = "Suraj"

Output : [10]  

  

Input : [11]  

# Print Assign Values
print nr.get_all()

Output : [11]  

{'id': 'NEW', 'Phone': None, 'Age': 10, 'Name': 'Suraj', 'Income': 45000}

Input : [12]  

# Save Data Values In Database
nr.save()
#
# You Don't Need To Create New Row Object Again And Again.
# Because After Saving Data., 
# Row Object Interface Reset Automatically.
# And Then You Can Use Object 

Output : [12]  

True

Input : [13]  

# Get All Values From Row Object Interface After Saving
print nr.get_all()

Output : [13]  

{'id': 'NEW', 'Phone': None, 'Age': None, 'Name': None, 'Income': None}

Input : [14]  

# Set New Values Using set_all
nr.set_all(Age=10, Income = 45000, Name = "Sam")

Output : [14]  

  

Input : [15]  

# Get All Assigned Values As Dictonery
print nr.get_all()

Output : [15]  

{'id': 'NEW', 'Phone': None, 'Age': 10, 'Name': 'Sam', 'Income': 45000}

Input : [16]  

# Save Changes
print nr.save()

Output : [16]  

True

Input : [17]  

# Get All Row Object Interface As List
print t.get_all()

Output : [17]  

[< Row-ID:1 >, < Row-ID:3 >, < Row-ID:4 >]

Input : [18]  

# Get Row Object Interface, Using Id As List
print t.get(id=2)

Output : [18]  

[]

Input : [19]  

# For Accessing Row Object Interface
storeobj = t.get(id=1)[0]

Output : [19]  

  

Input : [20]  

# Get All Values As Dictonery From Row Object Interface
print storeobj.get_all()

Output : [20]  

{'id': 1, 'Phone': None, 'Age': 10, 'Name': u'Suraj', 'Income': 50000}

Input : [21]  

# Updae Value
storeobj.Income = 50000

Output : [21]  

  

Input : [22]  

# Save Update
storeobj.save()

Output : [22]  

True

Input : [23]  

# Get All Row Object Interface 
for i in t.get_all():
    # Get All Values From Row OBject Interface As Dictonery
    print i.get_all()

Output : [23]  

{'id': 1, 'Phone': None, 'Age': 10, 'Name': u'Suraj', 'Income': 50000}
{'id': 3, 'Phone': None, 'Age': 10, 'Name': u'Suraj', 'Income': 45000}
{'id': 4, 'Phone': None, 'Age': 10, 'Name': u'Sam', 'Income': 45000}

Input : [24]  

# Search Row Object Interface Using Any Values of column
obj = t.search(Income=45000)[0]

print obj

Output : [24]  

Search trgger
< Row-ID:3 >

Input : [25]  

# Delete Row Using Row OBject Interface Or Id 
t.delete(obj)

Output : [25]  

  

Input : [26]  

# Again! Get All Row Object Interface
t.get_all()

Output : [26]  

[< Row-ID:1 >, < Row-ID:4 >]

Input : [27]  

# Get All Column Names Of Current Selected Table Object Interface
t.columns_name()

Output : [27]  

[('Phone', 'INTEGER'),
 ('Name', 'TEXT'),
 ('Income', 'INTEGER'),
 ('Age', 'INTEGER')]

Input : [28]  

# Get All Table Names 
t.get_tables_name()

Output : [28]  

['AboutBooks', 'AboutTeachers', 'AboutStudents']

Input : [29]  

# Change Current Table Object Interface
t.TableHook

Output : [29]  

'AboutTeachers'

Input : [30]  

# Save Changes
DBase.save()

# Close Database
DBase.close()

raw_input("[+] Testing Complete.")

Output : [30]  

[+] Testing Complete.


Author.

            By S.S.B Group                          
Suraj Singh
Admin
S.S.B Group
surajsinghbisht054@gmail.com
http://bitforestinfo.blogspot.com/

Note: We Feel Proud To Be Indian

Development.

Want to Contribute? Great!
There Are 2 Methods.
  1. Pull Request ( Github Account Required ).
  2. Through Email.

1. Pull Request ( Github A/c Required ).

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

2. Through Email.

  1. Send Your Updated Version On My Email.

Contributing


License

Apache License






For Project Repository. Click Here.

For More Update, Visit Our Regularly. 
And Subscribe Our Blog, 

Follow Us and share it.
For Any Type of Suggestion Or Help
Contact me:
S.S.B
surajsinghbisht054@gmail.com