python sqlite3 operation

#!/usr/bin/env python
#-*-coding:UTF-8-*-
"""
@Item   :  ABC
@Author :  william
@Group  :   ABC Group
@Date   :  2013-04-25
@Funtion:
        sqlite function
        Defind describe:
            path  :  local sqlite3 databases
            tetchall  :  return all values
            commit  :   submit databases updates
"""

import sys,time,os,sqlite3,traceback


from abc import log


LOG = log.get_logger(__name__)

class sqlites(object):
    ''' Connection databases operation'''
    def __init__ (self):
        path = '/data/abc.db3'

        try:
            self.conn = sqlite3.connect(path)
        except:
            LOG.warning(traceback.print_exc())

    def create(self,sql):
        '''Create tables operation  '''
        try:
            cur = self.conn.cursor()
            cur.execute(sql)
            self.conn.commit()
        except:
            LOG.warning(traceback.print_exc())
            return traceback.print_exc()


    def insert(self,sql):
        ''' Insert and update databases operation'''
        try:
            self.create(sql)
        except:
            LOG.warning(traceback.print_exc())
            return traceback.print_exc()

    def select(self,sql):
        ''' Query the database operation '''
        try:
            print sql
            cur = self.conn.cursor()
            cur.execute(sql)
            return cur.fetchall()
        except:
            LOG.warning(traceback.print_exc())
            return traceback.print_exc()

    def close(self):
        ''' Close cursor operation '''
        try:
            self.conn.close()
        except:
            LOG.warning(traceback.print_exc())
            return traceback.print_exc()


if __name__ == "__main__":
    st = sqlites()
    #st.create('CREATE TABLE abc(id INTEGER PRIMARY KEY, hosts VARCHAR(20), info LONGTEXT)')
    st.insert(" INSERT INTO abc(id,hosts,info) values(NUll,'192.168.10.1' , 'abc')")
    print st.select("select * from abc")
    st.close()
 

你可能感兴趣的:(python,sqlite3)