python sqlite help类

import sqlite3
import uuid
import time

class dbsqlite(object):
    def __init__(self):
        self.cx = sqlite3.connect("d:/test.db")
        self.cu = self.cx.cursor()
        self.create()

    def create(self):
        self.cu.execute(
            "create table   if not exists show (id integer primary key,  city varchar(100), venue varchar(100), time integer,state integer,title varchar(100) )")

    def no_select_action(self, actionStr):
        try:
            self.cu.execute(actionStr)
            self.cx.commit()
        except:
            return

    def _select(self, selectStr):
        self.cu.execute(selectStr)
        return self.cu.fetchall()

    def update(self,str ):
        self.cu.execute(str)
        self.cx.commit()

class db(dbsqlite):
    def __init__(self):
        dbsqlite.__init__(self)

    def insert(self, data):
        insert_str = 'insert into show values(' + str(int(time.time())) \
                     + ',' + '"' + data['city'] + '"' \
                     + ',' + '"' + data['venue'] + '"' \
                     + ',' + '"' + str(data['time']) + '"' \
                     + ',' + '"' + str(data['state']) + '"' \
                     + ',' + '"' + data['title'] + '"' \
                     + ')'
        self.no_select_action(insert_str)

    def select_by_title(self,title):
        str = 'SELECT * FROM show where  title=' +  '"'+    title +  '"'
        return  self._select(str)

    def selectAll(self):
        str = 'SELECT * FROM show ;'
        return self._select(str)

    def select_by_state(self, state):
        str = 'SELECT * FROM show where  state=' +  '"'+    str(state) +  '"'
        return  self._select(str)
    def update_itemstate_by_id(self, id , state):
        strxx = 'update show set state=' + '"'+ str(state)  + '"'  +  ' where id = ' + str(id)
        self.update(strxx)

if __name__ == "__main__":
    xx = db()
    xx.insert({'city': '南京', 'venue': 'venue', 'time': 2233, 'state': 1,
               'title': 'xx' + str(int(time.time()))})

    item = xx.select_by_title('xx1498529836')
    if len(item):
        print ("select succ")
    else :
        print ('select fail ')

    xx.update_itemstate_by_id(1498529836, 6)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值