本文整理匯總了Python中sqlalchemy.types.NVARCHAR屬性的典型用法代碼示例。如果您正苦於以下問題:Python types.NVARCHAR屬性的具體用法?Python types.NVARCHAR怎麽用?Python types.NVARCHAR使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類sqlalchemy.types
的用法示例。
在下文中一共展示了types.NVARCHAR屬性的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: insert_other_db
# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import NVARCHAR [as 別名]
def insert_other_db(to_db, data, table_name, write_index, primary_keys):
# 定義engine
engine_mysql = engine_to_db(to_db)
# 使用 http://docs.sqlalchemy.org/en/latest/core/reflection.html
# 使用檢查檢查數據庫表是否有主鍵。
insp = inspect(engine_mysql)
col_name_list = data.columns.tolist()
# 如果有索引,把索引增加到varchar上麵。
if write_index:
# 插入到第一個位置:
col_name_list.insert(0, data.index.name)
print(col_name_list)
data.to_sql(name=table_name, con=engine_mysql, schema=to_db, if_exists='append',
dtype={col_name: NVARCHAR(length=255) for col_name in col_name_list}, index=write_index)
# 判斷是否存在主鍵
if insp.get_primary_keys(table_name) == []:
with engine_mysql.connect() as con:
# 執行數據庫插入數據。
try:
con.execute('ALTER TABLE `%s` ADD PRIMARY KEY (%s);' % (table_name, primary_keys))
except Exception as e:
print("################## ADD PRIMARY KEY ERROR :", e)
# 插入數據。
示例2: test_char_length
# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import NVARCHAR [as 別名]
def test_char_length(self):
metadata = self.metadata
t1 = Table(
"t1",
metadata,
Column("c1", VARCHAR(50)),
Column("c2", NVARCHAR(250)),
Column("c3", CHAR(200)),
Column("c4", NCHAR(180)),
)
t1.create()
m2 = MetaData(testing.db)
t2 = Table("t1", m2, autoload=True)
eq_(t2.c.c1.type.length, 50)
eq_(t2.c.c2.type.length, 250)
eq_(t2.c.c3.type.length, 200)
eq_(t2.c.c4.type.length, 180)
示例3: test_sql_alchemy_subclass_column_types
# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import NVARCHAR [as 別名]
def test_sql_alchemy_subclass_column_types():
class F(FilterSet):
ALLOWED_FILTERS = {types.Integer: ['eq', 'gt']}
class Meta:
abstract = True
class MyNVARCHAR(types.NVARCHAR):
pass
class TestModel(Base):
__tablename__ = 'test'
id = Column(types.SmallInteger, primary_key=True, autoincrement=True)
text = Column(MyNVARCHAR)
class TestFilter(F):
EXTRA_ALLOWED_FILTERS = {types.String: ['eq']}
class Meta:
model = TestModel
fields = {'id': [...], 'text': [...]}
filter_fields = set(TestFilter._meta.fields)
ok = {'id', 'id_gt', 'text', 'text_is_null', 'and', 'not', 'or'}
assert filter_fields == ok
示例4: __init__
# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import NVARCHAR [as 別名]
def __init__(self, length=None, collation=None,
convert_unicode='force',
unicode_error=None):
super(NVARCHAR, self).__init__(
length,
collation=collation,
convert_unicode=convert_unicode,
unicode_error='ignore')
示例5: test_reflect_nvarchar
# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import NVARCHAR [as 別名]
def test_reflect_nvarchar(self):
metadata = self.metadata
Table(
"tnv",
metadata,
Column("nv_data", sqltypes.NVARCHAR(255)),
Column("c_data", sqltypes.NCHAR(20)),
)
metadata.create_all()
m2 = MetaData(testing.db)
t2 = Table("tnv", m2, autoload=True)
assert isinstance(t2.c.nv_data.type, sqltypes.NVARCHAR)
assert isinstance(t2.c.c_data.type, sqltypes.NCHAR)
if testing.against("oracle+cx_oracle"):
assert isinstance(
t2.c.nv_data.type.dialect_impl(testing.db.dialect),
cx_oracle._OracleUnicodeStringNCHAR,
)
assert isinstance(
t2.c.c_data.type.dialect_impl(testing.db.dialect),
cx_oracle._OracleNChar,
)
data = u("m’a réveillé.")
with testing.db.connect() as conn:
conn.execute(t2.insert(), dict(nv_data=data, c_data=data))
nv_data, c_data = conn.execute(t2.select()).first()
eq_(nv_data, data)
eq_(c_data, data + (" " * 7)) # char is space padded
assert isinstance(nv_data, util.text_type)
assert isinstance(c_data, util.text_type)
示例6: _get_column_info
# 需要導入模塊: from sqlalchemy import types [as 別名]
# 或者: from sqlalchemy.types import NVARCHAR [as 別名]
def _get_column_info(
self,
name,
type_,
nullable,
autoincrement,
default,
precision,
scale,
length,
):
coltype = self.ischema_names.get(type_, None)
kwargs = {}
if coltype in (NUMERIC, DECIMAL):
args = (precision, scale)
elif coltype == FLOAT:
args = (precision,)
elif coltype in (CHAR, VARCHAR, UNICHAR, UNIVARCHAR, NCHAR, NVARCHAR):
args = (length,)
else:
args = ()
if coltype:
coltype = coltype(*args, **kwargs)
# is this necessary
# if is_array:
# coltype = ARRAY(coltype)
else:
util.warn(
"Did not recognize type '%s' of column '%s'" % (type_, name)
)
coltype = sqltypes.NULLTYPE
if default:
default = default.replace("DEFAULT", "").strip()
default = re.sub("^'(.*)'$", lambda m: m.group(1), default)
else:
default = None
column_info = dict(
name=name,
type=coltype,
nullable=nullable,
default=default,
autoincrement=autoincrement,
)
return column_info