本文整理汇总了Python中pysqlite2.dbapi2.register_converter函数的典型用法代码示例。如果您正苦于以下问题:Python register_converter函数的具体用法?Python register_converter怎么用?Python register_converter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了register_converter函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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"}
开发者ID:CarlFK,项目名称:dabo,代码行数:25,代码来源:dDataSet.py
示例2: create_db
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)
开发者ID:42cc,项目名称:pysqlite,代码行数:25,代码来源:fetch.py
示例3: __init__
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
开发者ID:PyroSamurai,项目名称:apt-p2p,代码行数:18,代码来源:db.py
示例4: __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")
开发者ID:jordansissel,项目名称:python-pimp,代码行数:22,代码来源:MusicDB.py
示例5: __init__
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)
开发者ID:bieschke,项目名称:nuffle,代码行数:39,代码来源:sqliteconnection.py
示例6: _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)
开发者ID:HarmonyEnterpriseSolutions,项目名称:harmony-platform,代码行数:23,代码来源:pysqlite2drv.py
示例7: len
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())
开发者ID:coopengo,项目名称:trytond,代码行数:30,代码来源:database.py
示例8: pysqlite2
"""
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 = []
开发者ID:BackupTheBerlios,项目名称:gappy-svn,代码行数:30,代码来源:sqlite3.py
示例9: __init__
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)
开发者ID:fregaham,项目名称:DISP,代码行数:61,代码来源:sqliteconnection.py
示例10: adapt_datetime_with_timezone_support
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'))
开发者ID:AlfredFu,项目名称:django,代码行数:31,代码来源:base.py
示例11: modules
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'))
开发者ID:Belgabor,项目名称:django,代码行数:31,代码来源:base.py
示例12: DBExcept
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
"""
开发者ID:PyroSamurai,项目名称:apt-p2p,代码行数:31,代码来源:db.py
示例13: adapter4bool
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)
开发者ID:avkameth,项目名称:bots.v202,代码行数:31,代码来源:botssqlite.py
示例14: 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(':'))))
开发者ID:mediafactory,项目名称:tryton_core_daemon,代码行数:30,代码来源:database.py
示例15: SQLarray
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
开发者ID:peterhil,项目名称:RecSQL,代码行数:31,代码来源:sqlarray.py
示例16: __repr__
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()
开发者ID:Oize,项目名称:pspstacklesspython,代码行数:29,代码来源:converter_point.py
示例17: init_db_conn
# ---*< 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
"""
开发者ID:st0w,项目名称:bitly-grinder,代码行数:30,代码来源:db.py
示例18: setUp
def setUp(self):
self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES)
sqlite.register_converter("bin", BinaryConverterTests.convert)
开发者ID:enyst,项目名称:plexnet,代码行数:3,代码来源:types.py
示例19: decoder
" 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):
开发者ID:5977862,项目名称:django,代码行数:31,代码来源:base.py
示例20: connect
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)
开发者ID:alexproca,项目名称:bots,代码行数:31,代码来源:botssqlite.py
注:本文中的pysqlite2.dbapi2.register_converter函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论