首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

python sqlite种

2012-10-31 
python sqlite类#!/usr/bin/env python# -*- coding: utf-8 -*-import sqlite3 as sqliteimport os.path a

python sqlite类

#!/usr/bin/env python# -*- coding: utf-8 -*-import sqlite3 as sqliteimport os.path as ospimport sysclass Sqli(object):    conn = ''    cursor = ''    def __init__(self, dbname):                try:            self.conn = sqlite.connect(osp.abspath(dbname))        except Exception, what:            print what            sys.exit()        self.conn.row_factory = sqlite.Row                 self.cursor = self.conn.cursor()            def createTable(self):                self.cursor.execute('''    CREATE TABLE IF NOT EXISTS [com](      [id] INTEGER PRIMARY KEY,      [title] TEXT,      [city] TEXT,      [url] TEXT,      UNIQUE([url]));            ''')        print "create table com "        self.cursor.execute('''    CREATE INDEX IF NOT EXISTS [website_idx_url] ON [com]([url]);            ''')        print 'create map index'        self.conn.commit()        def query(self, sql):        try:            self.cursor.execute(sql)            self.conn.commit()        except Exception, what:            print what       def show(self):        r = self.cursor.fetchall()        return r    def showone(self):        return self.cursor.fetchone()        def __del__(self):        self.cursor.close()        self.conn.close()

?

热点排行