def create_db(): if sqlite.version_info > (2, 0): if use_custom_types: con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES) sqlite.register_converter("text", lambda x: "<%s>" % x) else: con = sqlite.connect(":memory:") if use_dictcursor: cur = con.cursor(factory=DictCursor) elif use_rowcursor: cur = con.cursor(factory=RowCursor) else: cur = con.cursor() else: if use_tuple: con = sqlite.connect(":memory:") con.rowclass = tuple cur = con.cursor() else: con = sqlite.connect(":memory:") cur = con.cursor() cur.execute(""" create table test(v text, f float, i integer) """) return (con, cur)
def __init__(self, sequence=None): # Note that as immutable objects, tuples are created with __new__, # so we must not pass the argument to the __init__ method of tuple. super(dDataSet, self).__init__() self._connection = None self._cursor = None self._bizobj = None self._populated = False self._typeStructure = {} # We may need to encode fields that are not legal names. self.fieldAliases = {} # Keep a hash value to tell if we need to re-populate self._dataHash = "" sqlite.register_adapter(Decimal, self._adapt_decimal) # When filtering datasets, we need a reference to the dataset # this dataset was derived from. self._sourceDataSet = None # Register the converters sqlite.register_converter("decimal", self._convert_decimal) self._typeDict = {int: "integer", int: "integer", str: "text", str: "text", float: "real", datetime.date: "date", datetime.datetime: "timestamp", Decimal: "decimal"}
def __init__(self, db): """Load or create the database file. @type db: C{string} @param db: the database file to use """ self.db = db try: os.stat(db) except OSError: self._createNewDB(db) else: self._loadDB(db) if sqlite.version_info < (2, 1): sqlite.register_converter("TEXT", str) sqlite.register_converter("text", str) else: self.conn.text_factory = str
def __init__(self): self.needinit = 0 self.dbpath = "%s/.pimpdb" % os.getenv("HOME") self.streamlist = {} sqlite.register_adapter(str, adapt_string) sqlite.register_converter('VARCHAR', decode_string) if not os.path.isfile(self.dbpath): print "Need to create db" self.needinit = 1 # Initialize the db if we need to. if self.needinit: self.db = sqlite.connect(self.dbpath, detect_types=sqlite.PARSE_DECLTYPES) self.initdb() self.findmusic() self.db.close() self.queue = Queue.Queue() MusicDB.instance = self Thread.__init__(self,name="MusicDB")
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 # 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) else: opts["autocommit"] = 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")) # use only one connection for sqlite - supports multiple) # cursors per connection self._conn = sqlite.connect(self.filename, **opts) DBAPI.__init__(self, **kw)
def _getConnectParams_ (self, connectData): from pysqlite2 import dbapi2 # Register the missing converter and adapater for time values dbapi2.register_adapter (datetime.time, adapt_time) dbapi2.register_converter ('time', convert_time) # Register the missing converter and adapter for boolean values dbapi2.register_adapter (bool, adapt_boolean) dbapi2.register_converter ('boolean', convert_boolean) # NOTE: gnue-forms allways creates datetime values, even for dates. This is # why we have to define our own converter. Please remove as soon as # gnue-forms is fixed. dbapi2.register_converter ('date', convert_date) # mandatory parameters kwargs = {'database' : connectData ['dbname'], 'detect_types': dbapi2.PARSE_DECLTYPES} if 'timeout' in connectData: kwargs ['timeout'] = connectData ['timeout'] return ([], kwargs)
def adapt_point(point): return "%f;%f" % (point.x, point.y) def convert_point(s): x, y = map(float, s.split(";")) return Point(x, y) # Register the adapter sqlite3.register_adapter(Point, adapt_point) # Register the converter sqlite3.register_converter("point", convert_point) p = Point(4.0, -3.2) ######################### # 1) Using declared types con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) cur = con.cursor() cur.execute("create table test(p point)") cur.execute("insert into test(p) values (?)", (p, )) cur.execute("select p from test") print "with declared types:", cur.fetchone()[0] cur.close() con.close()
except ImportError, e1: from sqlite3 import dbapi2 as Database except ImportError, exc: import sys from django.core.exceptions import ImproperlyConfigured if sys.version_info < (2, 5, 0): module = 'pysqlite2 module' exc = e1 else: module = 'either pysqlite2 or sqlite3 modules (tried in that order)' raise ImproperlyConfigured("Error loading %s: %s" % (module, exc)) DatabaseError = Database.DatabaseError IntegrityError = Database.IntegrityError Database.register_converter("bool", lambda s: str(s) == '1') Database.register_converter("time", util.typecast_time) Database.register_converter("date", util.typecast_date) Database.register_converter("datetime", util.typecast_timestamp) Database.register_converter("timestamp", util.typecast_timestamp) Database.register_converter("TIMESTAMP", util.typecast_timestamp) Database.register_converter("decimal", util.typecast_decimal) Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) if Database.version_info >= (2, 4, 1): # Starting in 2.4.1, the str type is not accepted anymore, therefore, # we convert all str objects to Unicode # As registering a adapter for a primitive type causes a small # slow-down, this adapter is only registered for sqlite3 versions # needing it. Database.register_adapter(str, lambda s: s.decode('utf-8')) Database.register_adapter(SafeString, lambda s: s.decode('utf-8'))
# ---*< Standard imports >*--------------------------------------------------- import json try: # First try pysqlie2, assuming if it exists, it is newer from pysqlite2 import dbapi2 as sqlite3 except: import sqlite3 # ---*< Third-party imports >*------------------------------------------------ # ---*< Local imports >*------------------------------------------------------ from models import BitlyUrl # ---*< Initialization >*----------------------------------------------------- sqlite3.register_converter("json", json.loads) # ---*< Code >*--------------------------------------------------------------- def init_db_conn(**kwargs): db_conn = sqlite3.connect("bitly-grinder.db", detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) db_conn.row_factory = sqlite3.Row # fields by names setup_db(db_conn) return db_conn def setup_db(db): """Creates SQLite tables if needed """
reformatparamstyle = re.compile(u'%\((?P<name>[^)]+)\)s') #~ #bots engine uses: #~ ''' SELECT * #~ FROM ta #~ WHERE idta=%(idta)s ''', #~ {'idta':12345}) #~ #SQLite wants: #~ ''' SELECT * #~ FROM ta #~ WHERE idta=:idta ''', #~ {'idta': 12345} sqlite.register_adapter(bool, lambda s: '1' if s else '0') sqlite.register_converter('BOOLEAN', lambda s: s == '1') def connect(database): con = sqlite.connect(database, factory=BotsConnection,detect_types=sqlite.PARSE_DECLTYPES, timeout=99.0, isolation_level='EXCLUSIVE') con.row_factory = sqlite.Row con.execute('''PRAGMA synchronous=OFF''') return con class BotsConnection(sqlite.Connection): def cursor(self): return sqlite.Connection.cursor(self, factory=BotsCursor) class BotsCursor(sqlite.Cursor): def execute(self,string,parameters=None): if parameters is None: sqlite.Cursor.execute(self,string)
dt = dt.replace(tzinfo=timezone.utc) return dt def adapt_datetime_with_timezone_support(value): # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL. if settings.USE_TZ: if timezone.is_naive(value): warnings.warn(u"SQLite received a naive datetime (%s)" u" while time zone support is active." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) value = value.astimezone(timezone.utc).replace(tzinfo=None) return value.isoformat(" ") Database.register_converter("bool", lambda s: str(s) == '1') Database.register_converter("time", parse_time) Database.register_converter("date", parse_date) Database.register_converter("datetime", parse_datetime_with_timezone_support) Database.register_converter("timestamp", parse_datetime_with_timezone_support) Database.register_converter("TIMESTAMP", parse_datetime_with_timezone_support) Database.register_converter("decimal", util.typecast_decimal) Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support) Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) if Database.version_info >= (2, 4, 1): # Starting in 2.4.1, the str type is not accepted anymore, therefore, # we convert all str objects to Unicode # As registering a adapter for a primitive type causes a small # slow-down, this adapter is only registered for sqlite3 versions # needing it (Python 2.6 and up). Database.register_adapter(str, lambda s: s.decode('utf-8'))
from sqlite3 import dbapi2 as Database except ImportError, exc: import sys from django.core.exceptions import ImproperlyConfigured if sys.version_info < (2, 5, 0): module = 'pysqlite2 module' exc = e1 else: module = 'either pysqlite2 or sqlite3 modules (tried in that order)' raise ImproperlyConfigured("Error loading %s: %s" % (module, exc)) DatabaseError = Database.DatabaseError IntegrityError = Database.IntegrityError Database.register_converter("bool", lambda s: str(s) == '1') Database.register_converter("time", util.typecast_time) Database.register_converter("date", util.typecast_date) Database.register_converter("datetime", util.typecast_timestamp) Database.register_converter("timestamp", util.typecast_timestamp) Database.register_converter("TIMESTAMP", util.typecast_timestamp) Database.register_converter("decimal", util.typecast_decimal) Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) if Database.version_info >= (2,4,1): # Starting in 2.4.1, the str type is not accepted anymore, therefore, # we convert all str objects to Unicode # As registering a adapter for a primitive type causes a small # slow-down, this adapter is only registered for sqlite3 versions # needing it. Database.register_adapter(str, lambda s:s.decode('utf-8')) Database.register_adapter(SafeString, lambda s:s.decode('utf-8'))
dt = dt.replace(tzinfo=timezone.utc) return dt def adapt_datetime_with_timezone_support(value): # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL. if settings.USE_TZ: if timezone.is_naive(value): warnings.warn(u"SQLite received a naive datetime (%s)" u" while time zone support is active." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) value = value.astimezone(timezone.utc).replace(tzinfo=None) return value.isoformat(b" ") Database.register_converter(b"bool", lambda s: str(s) == '1') Database.register_converter(b"time", parse_time) Database.register_converter(b"date", parse_date) Database.register_converter(b"datetime", parse_datetime_with_timezone_support) Database.register_converter(b"timestamp", parse_datetime_with_timezone_support) Database.register_converter(b"TIMESTAMP", parse_datetime_with_timezone_support) Database.register_converter(b"decimal", util.typecast_decimal) Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support) Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) if Database.version_info >= (2, 4, 1): # Starting in 2.4.1, the str type is not accepted anymore, therefore, # we convert all str objects to Unicode # As registering a adapter for a primitive type causes a small # slow-down, this adapter is only registered for sqlite3 versions # needing it (Python 2.6 and up). Database.register_adapter(str, lambda s: s.decode('utf-8'))
def adapter4bool(boolfrompython): #SQLite expects a string if boolfrompython: return '1' else: return '0' def converter4bool(strfromdb): #SQLite returns a string if strfromdb == '1': return True else: return False sqlite.register_adapter(bool,adapter4bool) sqlite.register_converter('BOOLEAN',converter4bool) def connect(database): con = sqlite.connect(database, factory=BotsConnection,detect_types=sqlite.PARSE_DECLTYPES, timeout=99.0, isolation_level='IMMEDIATE') con.row_factory = sqlite.Row con.execute('''PRAGMA synchronous=OFF''') return con class BotsConnection(sqlite.Connection): def cursor(self): return sqlite.Connection.cursor(self, factory=BotsCursor) class BotsCursor(sqlite.Cursor): def execute(self,string,parameters=None): if parameters is None: sqlite.Cursor.execute(self,string)
from .creation import DatabaseCreation # isort:skip from .features import DatabaseFeatures # isort:skip from .introspection import DatabaseIntrospection # isort:skip from .operations import DatabaseOperations # isort:skip from .schema import DatabaseSchemaEditor # isort:skip def decoder(conv_func): """ The Python sqlite3 interface returns always byte strings. This function converts the received value to a regular string before passing it to the receiver function. """ return lambda s: conv_func(s.decode('utf-8')) Database.register_converter("bool", decoder(lambda s: s == '1')) Database.register_converter("time", decoder(parse_time)) Database.register_converter("date", decoder(parse_date)) Database.register_converter("datetime", decoder(parse_datetime)) Database.register_converter("timestamp", decoder(parse_datetime)) Database.register_converter("TIMESTAMP", decoder(parse_datetime)) Database.register_converter("decimal", decoder(backend_utils.typecast_decimal)) Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal) class DatabaseWrapper(BaseDatabaseWrapper): vendor = 'sqlite' # SQLite doesn't actually support most of these types, but it "does the right # thing" given more verbose field definitions, so leave them as is so that # schema inspection is more useful.
def adapt_datetime_with_timezone_support(value): # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL. if settings.USE_TZ: if timezone.is_naive(value): warnings.warn( "SQLite received a naive datetime (%s)" " while time zone support is active." % value, RuntimeWarning ) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) value = value.astimezone(timezone.utc).replace(tzinfo=None) return value.isoformat(str(" ")) Database.register_converter(str("bool"), lambda s: str(s) == "1") Database.register_converter(str("time"), parse_time) Database.register_converter(str("date"), parse_date) Database.register_converter(str("datetime"), parse_datetime_with_timezone_support) Database.register_converter(str("timestamp"), parse_datetime_with_timezone_support) Database.register_converter(str("TIMESTAMP"), parse_datetime_with_timezone_support) Database.register_converter(str("decimal"), util.typecast_decimal) Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support) Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) if Database.version_info >= (2, 4, 1): # Starting in 2.4.1, the str type is not accepted anymore, therefore, # we convert all str objects to Unicode # As registering a adapter for a primitive type causes a small # slow-down, this adapter is only registered for sqlite3 versions # needing it (Python 2.6 and up). Database.register_adapter(str, lambda s: s.decode("utf-8"))
""" SQLite3 backend for django. Requires pysqlite2 (http://pysqlite.org/). """ from django.core.db import base, typecasts from django.core.db.dicthelpers import * from pysqlite2 import dbapi2 as Database DatabaseError = Database.DatabaseError # Register adaptors ########################################################### Database.register_converter("bool", lambda s: str(s) == '1') Database.register_converter("time", typecasts.typecast_time) Database.register_converter("date", typecasts.typecast_date) Database.register_converter("datetime", typecasts.typecast_timestamp) # Database wrapper ############################################################ def utf8rowFactory(cursor, row): def utf8(s): if type(s) == unicode: return s.encode("utf-8") else: return s return [utf8(r) for r in row] class DatabaseWrapper: def __init__(self): self.connection = None self.queries = []
assert sqlite.version_info >= (2, 1) class DBExcept(Exception): """An error occurred in accessing the database.""" pass class khash(str): """Dummy class to convert all hashes to base64 for storing in the DB.""" # Initialize the database to work with 'khash' objects (binary strings) sqlite.register_adapter(khash, b2a_base64) sqlite.register_converter("KHASH", a2b_base64) sqlite.register_converter("khash", a2b_base64) sqlite.enable_callback_tracebacks(True) class DB: """An sqlite database for storing persistent files and hashes. @type db: L{twisted.python.filepath.FilePath} @ivar db: the database file to use @type conn: L{pysqlite2.dbapi2.Connection} @ivar conn: an open connection to the sqlite database """ def __init__(self, db): """Load or create the database file.
class DBExcept(Exception): pass class khash(str): """Dummy class to convert all hashes to base64 for storing in the DB.""" class dht_value(str): """Dummy class to convert all DHT values to base64 for storing in the DB.""" # Initialize the database to work with 'khash' objects (binary strings) sqlite.register_adapter(khash, b2a_base64) sqlite.register_converter("KHASH", a2b_base64) sqlite.register_converter("khash", a2b_base64) # Initialize the database to work with DHT values (binary strings) sqlite.register_adapter(dht_value, b2a_base64) sqlite.register_converter("DHT_VALUE", a2b_base64) sqlite.register_converter("dht_value", a2b_base64) class DB: """An sqlite database for storing persistent node info and key/value pairs. @type db: C{string} @ivar db: the database file to use @type conn: L{pysqlite2.dbapi2.Connection} @ivar conn: an open connection to the sqlite database
import os from twisted.trial import unittest class DBExcept(Exception): pass class khash(str): """Dummy class to convert all hashes to base64 for storing in the DB.""" class dht_value(str): """Dummy class to convert all DHT values to base64 for storing in the DB.""" # Initialize the database to work with 'khash' objects (binary strings) sqlite.register_adapter(khash, b2a_base64) sqlite.register_converter("KHASH", a2b_base64) sqlite.register_converter("khash", a2b_base64) # Initialize the database to work with DHT values (binary strings) sqlite.register_adapter(dht_value, b2a_base64) sqlite.register_converter("DHT_VALUE", a2b_base64) sqlite.register_converter("dht_value", a2b_base64) class DB: """An sqlite database for storing persistent node info and key/value pairs. @type db: C{string} @ivar db: the database file to use @type conn: L{pysqlite2.dbapi2.Connection} @ivar conn: an open connection to the sqlite database """
try: from pysqlite2 import dbapi2 as sqlite # ... all development was with pysqlite2 except ImportError: from sqlite3 import dbapi2 as sqlite # I hope we are compatible with sqlite3 import numpy from sqlutil import adapt_numpyarray, convert_numpyarray,\ adapt_object, convert_object from rest_table import Table2array from convert import irecarray_to_py sqlite.register_adapter(numpy.ndarray, adapt_numpyarray) sqlite.register_adapter(numpy.recarray, adapt_numpyarray) sqlite.register_adapter(numpy.core.records.recarray, adapt_numpyarray) sqlite.register_adapter(tuple, adapt_object) sqlite.register_adapter(list, adapt_object) sqlite.register_converter("NumpyArray", convert_numpyarray) sqlite.register_converter("Object", convert_object) class SQLarray(object): """A SQL table that returns (mostly) rec arrays. .. method:: SQLarray([name[,records[,columns[,cachesize=5,connection=None,dbfile=":memory:"]]]]) :Arguments: *name* table name (can be referred to as '__self__' in SQL queries) *records* numpy record array that describes the layout and initializes the table OR any iterable (and then columns must be set, too) OR a string that contains a single, *simple reStructured text table* (and the table name is
from twisted.python.filepath import FilePath from twisted.trial import unittest assert sqlite.version_info >= (2, 1) class DBExcept(Exception): """An error occurred in accessing the database.""" pass class khash(str): """Dummy class to convert all hashes to base64 for storing in the DB.""" # Initialize the database to work with 'khash' objects (binary strings) sqlite.register_adapter(khash, b2a_base64) sqlite.register_converter("KHASH", a2b_base64) sqlite.register_converter("khash", a2b_base64) sqlite.enable_callback_tracebacks(True) class DB: """An sqlite database for storing persistent files and hashes. @type db: L{twisted.python.filepath.FilePath} @ivar db: the database file to use @type conn: L{pysqlite2.dbapi2.Connection} @ivar conn: an open connection to the sqlite database """ def __init__(self, db): """Load or create the database file.
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)
try: from pysqlite2 import dbapi2 as sqlite # ... all development was with pysqlite2 except ImportError: from sqlite3 import dbapi2 as sqlite # I hope we are compatible with sqlite3 import numpy from sqlutil import adapt_numpyarray, convert_numpyarray,\ adapt_object, convert_object from rest_table import Table2array from convert import irecarray_to_py sqlite.register_adapter(numpy.ndarray,adapt_numpyarray) sqlite.register_adapter(numpy.recarray,adapt_numpyarray) sqlite.register_adapter(numpy.core.records.recarray,adapt_numpyarray) sqlite.register_adapter(tuple,adapt_object) sqlite.register_adapter(list,adapt_object) sqlite.register_converter("NumpyArray", convert_numpyarray) sqlite.register_converter("Object", convert_object) class SQLarray(object): """A SQL table that returns (mostly) rec arrays. .. method:: SQLarray([name[,records[,columns[,cachesize=5,connection=None,dbfile=":memory:"]]]]) :Arguments: *name* table name (can be referred to as '__self__' in SQL queries) *records* numpy record array that describes the layout and initializes the table OR any iterable (and then columns must be set, too) OR a string that contains a single, *simple reStructured text table* (and the table name is
return len(cursor.fetchall()) != 0 def lastid(self, cursor): # This call is not thread safe return cursor.lastrowid def lock(self, connection, table): pass def has_constraint(self): return False def has_multirow_insert(self): return True sqlite.register_converter('NUMERIC', lambda val: Decimal(val.decode('utf-8'))) if sys.version_info[0] == 2: sqlite.register_adapter(Decimal, lambda val: buffer(str(val))) sqlite.register_adapter(bytearray, lambda val: buffer(val)) else: sqlite.register_adapter(Decimal, lambda val: str(val).encode('utf-8')) def adapt_datetime(val): return val.replace(tzinfo=None).isoformat(" ") sqlite.register_adapter(datetime.datetime, adapt_datetime) sqlite.register_adapter(datetime.time, lambda val: val.isoformat()) sqlite.register_converter('TIME', lambda val: datetime.time(*map(int, val.decode('utf-8').split(':')))) sqlite.register_adapter(datetime.timedelta, lambda val: val.total_seconds())
return self.cursor.fetchone()[0] def lock(self, table): pass def has_constraint(self): return False def limit_clause(self, select, limit=None, offset=None): if limit is not None: select += ' LIMIT %d' % limit if offset is not None: if limit is None: select += ' LIMIT -1' select += ' OFFSET %d' % offset return select sqlite.register_converter('NUMERIC', lambda val: Decimal(val)) if sys.version_info[0] == 2: sqlite.register_adapter(Decimal, lambda val: buffer(str(val))) else: sqlite.register_adapter(Decimal, lambda val: bytes(str(val))) def adapt_datetime(val): return val.replace(tzinfo=None).isoformat(" ") sqlite.register_adapter(datetime.datetime, adapt_datetime) sqlite.register_adapter(datetime.time, lambda val: val.isoformat()) sqlite.register_converter('TIME', lambda val: datetime.time(*map(int, val.split(':'))))
" while time zone support is active." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) value = value.astimezone(timezone.utc).replace(tzinfo=None) return value.isoformat(str(" ")) def decoder(conv_func): """ The Python sqlite3 interface returns always byte strings. This function converts the received value to a regular string before passing it to the receiver function. """ return lambda s: conv_func(s.decode('utf-8')) Database.register_converter(str("bool"), decoder(lambda s: s == '1')) Database.register_converter(str("time"), decoder(parse_time)) Database.register_converter(str("date"), decoder(parse_date)) Database.register_converter(str("datetime"), decoder(parse_datetime_with_timezone_support)) Database.register_converter(str("timestamp"), decoder(parse_datetime_with_timezone_support)) Database.register_converter(str("TIMESTAMP"), decoder(parse_datetime_with_timezone_support)) Database.register_converter(str("decimal"), decoder(backend_utils.typecast_decimal)) Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support) Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal) if six.PY2:
def __repr__(self): return "(%f;%f)" % (self.x, self.y) def adapt_point(point): return "%f;%f" % (point.x, point.y) def convert_point(s): x, y = map(float, s.split(";")) return Point(x, y) # Register the adapter sqlite.register_adapter(Point, adapt_point) # Register the converter sqlite.register_converter("point", convert_point) p = Point(4.0, -3.2) ######################### # 1) Using declared types con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES) cur = con.cursor() cur.execute("create table test(p point)") cur.execute("insert into test(p) values (?)", (p,)) cur.execute("select p from test") print "with declared types:", cur.fetchone()[0] cur.close() con.close()
def setUp(self): self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES) sqlite.register_converter("bin", BinaryConverterTests.convert)
def sql_type(self, type_): if type_ in self.TYPES_MAPPING: return self.TYPES_MAPPING[type_] if type_.startswith('VARCHAR'): return SQLType('VARCHAR', 'VARCHAR') return SQLType(type_, type_) def sql_format(self, type_, value): if type_ in ('INTEGER', 'BIGINT'): if (value is not None and not isinstance(value, (Query, Expression))): value = int(value) return value sqlite.register_converter('NUMERIC', lambda val: Decimal(val.decode('utf-8'))) sqlite.register_adapter(Decimal, lambda val: str(val).encode('utf-8')) def adapt_datetime(val): return val.replace(tzinfo=None).isoformat(" ") sqlite.register_adapter(datetime.datetime, adapt_datetime) sqlite.register_adapter(datetime.time, lambda val: val.isoformat()) sqlite.register_converter( 'TIME', lambda val: datetime.time(*map(int, val.decode('utf-8').split(':')))) sqlite.register_adapter(datetime.timedelta, lambda val: val.total_seconds())
" while time zone support is active." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) value = value.astimezone(timezone.utc).replace(tzinfo=None) return value.isoformat(str(" ")) def decoder(conv_func): """ The Python sqlite3 interface returns always byte strings. This function converts the received value to a regular string before passing it to the receiver function. """ return lambda s: conv_func(s.decode('utf-8')) Database.register_converter(str("bool"), decoder(lambda s: s == '1')) Database.register_converter(str("time"), decoder(parse_time)) Database.register_converter(str("date"), decoder(parse_date)) Database.register_converter(str("datetime"), decoder(parse_datetime_with_timezone_support)) Database.register_converter(str("timestamp"), decoder(parse_datetime_with_timezone_support)) Database.register_converter(str("TIMESTAMP"), decoder(parse_datetime_with_timezone_support)) Database.register_converter(str("decimal"), decoder(backend_utils.typecast_decimal)) Database.register_adapter(datetime.datetime, adapt_datetime_with_timezone_support) Database.register_adapter(decimal.Decimal, backend_utils.rev_typecast_decimal) if six.PY2: Database.register_adapter(str, lambda s: s.decode('utf-8')) Database.register_adapter(SafeBytes, lambda s: s.decode('utf-8')) class DatabaseFeatures(BaseDatabaseFeatures):
import re reformatparamstyle = re.compile(u'%\((?P<name>[^)]+)\)s') #~ #bots engine uses: #~ ''' SELECT * #~ FROM ta #~ WHERE idta=%(idta)s ''', #~ {'idta':12345}) #~ #SQLite wants: #~ ''' SELECT * #~ FROM ta #~ WHERE idta=:idta ''', #~ {'idta': 12345} sqlite.register_adapter(bool, lambda s: '1' if s else '0') sqlite.register_converter('BOOLEAN', lambda s: s == '1') def connect(database): con = sqlite.connect(database, factory=BotsConnection, detect_types=sqlite.PARSE_DECLTYPES, timeout=99.0, isolation_level='EXCLUSIVE') con.row_factory = sqlite.Row con.execute('''PRAGMA synchronous=OFF''') return con class BotsConnection(sqlite.Connection): def cursor(self):