Friday, October 26, 2012

SQLite3 class in Python

For my working, i created class using sqlite3 to manipluate database:

#!/usr/bin/python
import sqlite3, sys
class database:
    'Database sqlite3 class'
    def __init__(self, name):
        self.name = name
        self.create_database()
    def create_database(self):
        self.conn = sqlite3.connect(self.name)
        self.c = self.conn.cursor()
    def query(self, query):
        try:  
            r = self.c.execute(query)
            self.conn.commit()
            return r
        except sqlite3.Error, e:
            print "Error %s:" % e.args[0]
            sys.exit(1)
    def query2(self, query):
        try:  
            self.c.execute(query)
            r = self.c.fetchall()
            return r
        except sqlite3.Error, e:
            print "Error %s:" % e.args[0]
            sys.exit(1)
    def check_database_exits(self,table_name):
        re = self.query2("SELECT name FROM sqlite_master WHERE type='table' AND name='"+table_name+"'")
        if (len(re) > 0):
            return True
        else:
            return False
    def insert_table(self,table_name,data):
        sql = "INSERT into "+table_name+" values ("          
        for item in data:
            if type(item) == int:
                sql = sql + str(item) + ","
            else:
                if type(item) == str:
                    sql = sql + "'" + item + "'"+ ","
        sql = sql[:-1] + ")"
        self.query(sql)
    def get_row(self,table_name,key,value):
        if type(value) == int:
            sql = "SELECT * FROM "+table_name + " WHERE "+key+"="+str(value)
        else:
            if type(value) == str:
                sql = "SELECT * FROM "+table_name + " WHERE "+key+"='"+value+"'"
        re = db.query2(sql)
        return re
    def delete_row(self,table_name,key,value):
        if type(value) == int:
            sql = "DELETE FROM "+table_name + " WHERE "+key+"="+str(value)
        else:
            if type(value) == str:
                sql = "DELETE FROM "+table_name + " WHERE "+key+"='"+value+"'"
        db.query(sql)
    def update_row(self,table_name,key,kvalue,nkey, nvalue):
        if type(nvalue) == int:
            sql = "UPDATE "+table_name + " SET " + nkey + "=" + str(nvalue)
            if type(kvalue) == int:
                sql = sql + " WHERE " + key + "=" + str(kvalue)
            else:
                if type(kvalue) == str:
                    sql = sql + " WHERE " + key + "='" + kvalue + "'"
        else:
            if type(nvalue) == str:
                sql = "UPDATE "+table_name + " SET " + nkey + "='" + nvalue + "'"
                if type(kvalue) == int:
                    sql = sql + " WHERE " + key + "=" + str(kvalue)
                if type(kvalue) == str:
                    sql = sql + " WHERE " + key + "='" + kvalue + "'"
        self.query(sql)

------------------------------------------------------------
Thanks for reading
--------------------------------------------------------------------------
Security Research
All my Lab:
Linux Lab -- window and Cisco Lab
to be continued - I will update more.  

No comments:

Install Xposed Inspector and Frida on Genymotion

Today i had some work with android. So i need trace application. I found 2 nice tool can help me: Xposed Inspector and Frida. To setup ther...