def __init__(self, filename, autoCommit=1, **kw):
     drivers = kw.pop('driver', None) or 'pysqlite2,sqlite3,sqlite'
     for driver in drivers.split(','):
         driver = driver.strip()
         if not driver:
             continue
         try:
             if driver in ('sqlite2', 'pysqlite2'):
                     from pysqlite2 import dbapi2 as sqlite
                     self.using_sqlite2 = True
             elif driver == 'sqlite3':
                     import sqlite3 as sqlite
                     self.using_sqlite2 = True
             elif driver in ('sqlite', 'sqlite1'):
                     import sqlite
                     self.using_sqlite2 = False
             else:
                 raise ValueError('Unknown SQLite driver "%s", expected pysqlite2, sqlite3 or sqlite' % driver)
         except ImportError:
             pass
         else:
             break
     else:
         raise ImportError('Cannot find an SQLite driver, tried %s' % drivers)
     if self.using_sqlite2:
         sqlite.encode = base64.encodestring
         sqlite.decode = base64.decodestring
     self.module = sqlite
     self.filename = filename  # full path to sqlite-db-file
     self._memory = filename == ':memory:'
     if self._memory and not self.using_sqlite2:
         raise ValueError("You must use sqlite2 to use in-memory databases")
     # connection options
     opts = {}
     if self.using_sqlite2:
         if autoCommit:
             opts["isolation_level"] = None
         global sqlite2_Binary
         if sqlite2_Binary is None:
             sqlite2_Binary = sqlite.Binary
             sqlite.Binary = lambda s: sqlite2_Binary(sqlite.encode(s))
         if 'factory' in kw:
             factory = kw.pop('factory')
             if isinstance(factory, str):
                 factory = globals()[factory]
             opts['factory'] = factory(sqlite)
     else:
         opts['autocommit'] = Boolean(autoCommit)
         if 'encoding' in kw:
             opts['encoding'] = kw.pop('encoding')
         if 'mode' in kw:
             opts['mode'] = int(kw.pop('mode'), 0)
     if 'timeout' in kw:
         if self.using_sqlite2:
             opts['timeout'] = float(kw.pop('timeout'))
         else:
             opts['timeout'] = int(float(kw.pop('timeout')) * 1000)
     if 'check_same_thread' in kw:
         opts["check_same_thread"] = Boolean(kw.pop('check_same_thread'))
     # use only one connection for sqlite - supports multiple)
     # cursors per connection
     self._connOptions = opts
     self.use_table_info = Boolean(kw.pop("use_table_info", True))
     DBAPI.__init__(self, **kw)
     self._threadPool = {}
     self._threadOrigination = {}
     if self._memory:
         self.makeMemoryConnection()
 def __init__(self, filename, autoCommit=1, **kw):
     drivers = kw.pop('driver', None) or 'pysqlite2,sqlite3,sqlite'
     for driver in drivers.split(','):
         driver = driver.strip()
         if not driver:
             continue
         try:
             if driver in ('sqlite2', 'pysqlite2'):
                 from pysqlite2 import dbapi2 as sqlite
                 self.using_sqlite2 = True
             elif driver == 'sqlite3':
                 import sqlite3 as sqlite
                 self.using_sqlite2 = True
             elif driver in ('sqlite', 'sqlite1'):
                 import sqlite
                 self.using_sqlite2 = False
             else:
                 raise ValueError(
                     'Unknown SQLite driver "%s", expected pysqlite2, sqlite3 or sqlite'
                     % driver)
         except ImportError:
             pass
         else:
             break
     else:
         raise ImportError('Cannot find an SQLite driver, tried %s' %
                           drivers)
     if self.using_sqlite2:
         sqlite.encode = base64.encodestring
         sqlite.decode = base64.decodestring
     self.module = sqlite
     self.filename = filename  # full path to sqlite-db-file
     self._memory = filename == ':memory:'
     if self._memory and not self.using_sqlite2:
         raise ValueError("You must use sqlite2 to use in-memory databases")
     # connection options
     opts = {}
     if self.using_sqlite2:
         if autoCommit:
             opts["isolation_level"] = None
         global sqlite2_Binary
         if sqlite2_Binary is None:
             sqlite2_Binary = sqlite.Binary
             sqlite.Binary = lambda s: sqlite2_Binary(sqlite.encode(s))
         if 'factory' in kw:
             factory = kw.pop('factory')
             if isinstance(factory, str):
                 factory = globals()[factory]
             opts['factory'] = factory(sqlite)
     else:
         opts['autocommit'] = Boolean(autoCommit)
         if 'encoding' in kw:
             opts['encoding'] = kw.pop('encoding')
         if 'mode' in kw:
             opts['mode'] = int(kw.pop('mode'), 0)
     if 'timeout' in kw:
         if self.using_sqlite2:
             opts['timeout'] = float(kw.pop('timeout'))
         else:
             opts['timeout'] = int(float(kw.pop('timeout')) * 1000)
     if 'check_same_thread' in kw:
         opts["check_same_thread"] = Boolean(kw.pop('check_same_thread'))
     # use only one connection for sqlite - supports multiple)
     # cursors per connection
     self._connOptions = opts
     self.use_table_info = Boolean(kw.pop("use_table_info", True))
     DBAPI.__init__(self, **kw)
     self._threadPool = {}
     self._threadOrigination = {}
     if self._memory:
         self._memoryConn = sqlite.connect(self.filename,
                                           **self._connOptions)
         # Convert text data from SQLite to str, not unicode -
         # SQLObject converts it to unicode itself.
         self._memoryConn.text_factory = str
示例#3
0
 def __init__(self, filename, autoCommit=1, **kw):
     global sqlite
     global using_sqlite2
     if sqlite is None:
         try:
             from pysqlite2 import dbapi2 as sqlite
             using_sqlite2 = True
         except ImportError:
             import sqlite
             using_sqlite2 = False
     self.module = sqlite
     self.filename = filename  # full path to sqlite-db-file
     self._memory = filename == ':memory:'
     if self._memory:
         if not using_sqlite2:
             raise ValueError(
                 "You must use sqlite2 to use in-memory databases")
     # connection options
     opts = {}
     if using_sqlite2:
         if autoCommit:
             opts["isolation_level"] = None
         if 'encoding' in kw:
             import warnings
             warnings.warn(DeprecationWarning("pysqlite2 does not support the encoding option"))
         opts["detect_types"] = sqlite.PARSE_DECLTYPES
         for col_type in "text", "char", "varchar":
             sqlite.register_converter(col_type, stop_pysqlite2_converting_strings_to_unicode)
             sqlite.register_converter(col_type.upper(), stop_pysqlite2_converting_strings_to_unicode)
         try:
             from sqlite import encode, decode
         except ImportError:
             import base64
             sqlite.encode = base64.encodestring
             sqlite.decode = base64.decodestring
         else:
             sqlite.encode = encode
             sqlite.decode = decode
         global sqlite2_Binary
         if sqlite2_Binary is None:
             sqlite2_Binary = sqlite.Binary
             sqlite.Binary = lambda s: sqlite2_Binary(sqlite.encode(s))
     else:
         opts['autocommit'] = bool(autoCommit)
         if 'encoding' in kw:
             opts['encoding'] = popKey(kw, 'encoding')
         if 'mode' in kw:
             opts['mode'] = int(popKey(kw, 'mode'), 0)
     if 'timeout' in kw:
         opts['timeout'] = float(popKey(kw, 'timeout'))
     if 'check_same_thread' in kw:
         opts["check_same_thread"] = bool(popKey(kw, 'check_same_thread'))
     # use only one connection for sqlite - supports multiple)
     # cursors per connection
     self._connOptions = opts
     DBAPI.__init__(self, **kw)
     self._threadPool = {}
     self._threadOrigination = {}
     if self._memory:
         self._memoryConn = sqlite.connect(
             self.filename, **self._connOptions)