本文整理汇总了Python中pysqlite2.dbapi2.register_adapter函数的典型用法代码示例。如果您正苦于以下问题:Python register_adapter函数的具体用法?Python register_adapter怎么用?Python register_adapter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了register_adapter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
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"}
示例2: setUp
def setUp(self):
self.con = sqlite.connect(":memory:")
try:
del sqlite.adapters[int]
except:
pass
sqlite.register_adapter(int, ObjectAdaptationTests.cast)
self.cur = self.con.cursor()
示例3: __init__
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")
示例4: getConn
def getConn(self):
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def boolAdapter(b):
if b:
return 'True'
else:
return 'False'
# Return connection for current thread
id = threading.currentThread().getName()
if not id in self.conns:
sqlite.register_adapter(bool, boolAdapter)
conn = sqlite.connect(self.name, timeout=30, isolation_level=None)
conn.row_factory = dict_factory
self.connParams[id] = "?"
self.conns[id] = conn
return self.conns[id]
示例5: _getConnectParams_
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)
示例6: decoder
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))
Database.register_converter(str("timestamp"), decoder(parse_datetime))
Database.register_converter(str("TIMESTAMP"), decoder(parse_datetime))
Database.register_converter(str("decimal"), decoder(backend_utils.typecast_decimal))
Database.register_adapter(datetime.datetime, adapt_datetime_warn_on_aware_datetime)
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 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.
data_types = {
'AutoField': 'integer',
'BinaryField': 'BLOB',
'BooleanField': 'bool',
示例7: modules
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'))
class DatabaseFeatures(BaseDatabaseFeatures):
# SQLite cannot handle us only partially reading from a cursor's result set
# and then writing the same rows to the database in another cursor. This
# setting ensures we always read result sets fully into memory all in one
# go.
can_use_chunked_reads = False
示例8: lock
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(':'))))
示例9: Point
from pysqlite2 import dbapi2 as sqlite3
class Point(object):
def __init__(self, x, y):
self.x, self.y = x, y
def adapt_point(point):
return "%f;%f" % (point.x, point.y)
sqlite3.register_adapter(Point, adapt_point)
con = sqlite3.connect(":memory:")
cur = con.cursor()
p = Point(4.0, -3.2)
cur.execute("select ?", (p,))
print cur.fetchone()[0]
示例10: BerkeleyDatabase
import sys
from peewee import ImproperlyConfigured
from peewee import sqlite3
from playhouse.sqlite_ext import *
sqlite3_lib_version = sqlite3.sqlite_version_info
# Peewee assumes that the `pysqlite2` module was compiled against the
# BerkeleyDB SQLite libraries.
try:
from pysqlite2 import dbapi2 as berkeleydb
except ImportError:
import sqlite3 as berkeleydb
berkeleydb.register_adapter(decimal.Decimal, str)
berkeleydb.register_adapter(datetime.date, str)
berkeleydb.register_adapter(datetime.time, str)
class BerkeleyDatabase(SqliteExtDatabase):
def __init__(self, database, pragmas=None, cache_size=None, page_size=None,
multiversion=None, *args, **kwargs):
super(BerkeleyDatabase, self).__init__(
database, pragmas=pragmas, *args, **kwargs)
if multiversion:
self._pragmas.append(('multiversion', 'on'))
if page_size:
self._pragmas.append(('page_size', page_size))
if cache_size:
self._pragmas.append(('cache_size', cache_size))
示例11: SQLarray
import re
try:
from hashlib import md5
except ImportError:
from md5 import md5
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*
示例12: adapt_datetime
from pysqlite2 import dbapi2 as sqlite
import datetime, time
def adapt_datetime(ts):
return time.mktime(ts.timetuple())
sqlite.register_adapter(datetime.datetime, adapt_datetime)
con = sqlite.connect(":memory:")
cur = con.cursor()
now = datetime.datetime.now()
cur.execute("select ?", (now,))
print cur.fetchone()[0]
示例13: DBExcept
from time import sleep
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
示例14: lastid
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())
def convert_interval(value):
示例15: datetime
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'))
Database.register_adapter(SafeString, lambda s: s.decode('utf-8'))
class DatabaseFeatures(BaseDatabaseFeatures):
# SQLite cannot handle us only partially reading from a cursor's result set
# and then writing the same rows to the database in another cursor. This
# setting ensures we always read result sets fully into memory all in one
# go.