A small sampl program to share with everyone on sqlalchemy. The attached 
file contain sample and test database for you to try out. I welcome commend 
and a littler encouragement :). Have been explore python for 3 years, need 
to contribute back some good thing to the community :)

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/IE_wXO50JykJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

#!/bin/env python
#----------------------------------------------------------------------------
#NAME:  sqlalchemy_samply5.py
#
#SQLALCHEMY Sample program Version 0.5
#
#Author:    Rungroj Teekamongkol, AKA "VOODOOKING"
#
#EMAIL:     [email protected]
#
#This Codes intent to be simple and easy for intermediate user
#It might be a bit confusing for beginner but you don't have to be
#discourage because it so short that it will sink down into you later, else
# read more on internet help sites.
#Feel free to modify it, and if you like it do praise me a little, it will
#at least motivate me further in the quest to explore or become a python
#Zen master :)
#----------------------------------------------------------------------------
from sqlalchemy import create_engine,MetaData, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, mapper
from sqlalchemy import Column, Integer, String, Sequence, update



class user_table():
    '''USERS Table Class to manupulate the data inside it table only'''
    def __init__(self):
        #create engine to connect to your database
        self.user_database = 'sqlite:///E:/pythonpractice/Database/test.db'
        self.engine = create_engine(self.user_database,echo=True)
        #Create Metadata to link to your table
        self.md = MetaData(self.engine) 
        #Link to you table and bind the metadata to your table
        self.userstable = Table('users', self.md, autoload=True)
        #Create new session for users table
        Session = sessionmaker(bind=self.engine)
        self.session = Session()
    
    def user_insert(self,uname,fname,upwd):
        '''Insert function for the user table'''
        
self.Iuser=self.userstable.insert().values(name=uname,fullname=fname,password=upwd)
        self.session.execute(self.Iuser)
    
    def user_update(self,uname,fname):
        '''Update function for the users table. This function only update 
        FULL NAME base on NAME'''
        
self.upQuser=self.userstable.update().values(fullname=fname).where(self.userstable.c.name=='John')
        self.session.execute(self.upQuser)
        
    def user_delete(self,uname):
        '''Delete function for the user table'''
        
self.Deluser=self.userstable.delete().where(self.userstable.c.name==uname)
        self.session.execute(self.Deluser)
    
    def user_print(self):
        '''Print users table dta'''
        for instance in 
self.session.query(self.userstable).order_by(self.userstable.c.name):      
            print instance.name,"|",instance.fullname,"|",instance.password
    
    def close_usersession(self):
        '''Close users table session'''
        self.session.close()

if __name__ == "__main__":
    
    calluser = user_table()
    calluser.user_update('John','John Gang')
    calluser.user_insert('paga','Paga Zue','joo93939')
    calluser.user_print()
    calluser.close_usersession()

Attachment: test.db
Description: Binary data

Reply via email to