天天看点

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  '''

            cur = self.conn.cursor()

            cur.execute(sql)

            self.conn.commit()

            return traceback.print_exc()

    def insert(self,sql):

        ''' Insert and update databases operation'''

            self.create(sql)

    def select(self,sql):

        ''' Query the database operation '''

            print sql

            return cur.fetchall()

    def close(self):

        ''' Close cursor operation '''

            self.conn.close()

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()

本文转自 swq499809608 51CTO博客,原文链接:http://blog.51cto.com/swq499809608/1186528