本文整理汇总了Python中sqlalchemy.testing.schema.Table.insert方法的典型用法代码示例。如果您正苦于以下问题:Python Table.insert方法的具体用法?Python Table.insert怎么用?Python Table.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.testing.schema.Table
的用法示例。
在下文中一共展示了Table.insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_numeric_nan_float
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_numeric_nan_float(self):
m = self.metadata
t1 = Table(
"t1",
m,
Column("intcol", Integer),
Column("numericcol", oracle.BINARY_DOUBLE(asdecimal=False)),
)
t1.create()
t1.insert().execute(
[
dict(intcol=1, numericcol=float("nan")),
dict(intcol=2, numericcol=float("-nan")),
]
)
eq_(
[
tuple(str(col) for col in row)
for row in select([t1.c.numericcol])
.order_by(t1.c.intcol)
.execute()
],
[("nan",), ("nan",)],
)
eq_(
[
tuple(str(col) for col in row)
for row in testing.db.execute(
"select numericcol from t1 order by intcol"
)
],
[("nan",), ("nan",)],
)
示例2: test_empty_insert
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_empty_insert(self):
t1 = Table('t1', self.metadata,
Column('is_true', Boolean, server_default=('1')))
self.metadata.create_all()
t1.insert().execute()
eq_(1, select([func.count(text('*'))], from_obj=t1).scalar())
eq_(True, t1.select().scalar())
示例3: test_misordered_lastrow
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_misordered_lastrow(self):
metadata = self.metadata
related = Table(
'related', metadata,
Column('id', Integer, primary_key=True),
mysql_engine='MyISAM'
)
t6 = Table(
"t6", metadata,
Column(
'manual_id', Integer, ForeignKey('related.id'),
primary_key=True),
Column(
'auto_id', Integer, primary_key=True,
test_needs_autoincrement=True),
mysql_engine='MyISAM'
)
metadata.create_all()
r = related.insert().values(id=12).execute()
id_ = r.inserted_primary_key[0]
eq_(id_, 12)
r = t6.insert().values(manual_id=id_).execute()
eq_(r.inserted_primary_key, [12, 1])
示例4: test_fixed_char
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_fixed_char(self):
m = MetaData(testing.db)
t = Table('t1', m,
Column('id', Integer, primary_key=True),
Column('data', CHAR(30), nullable=False))
t.create()
try:
t.insert().execute(
dict(id=1, data="value 1"),
dict(id=2, data="value 2"),
dict(id=3, data="value 3")
)
eq_(
t.select().where(t.c.data == 'value 2').execute().fetchall(),
[(2, 'value 2 ')]
)
m2 = MetaData(testing.db)
t2 = Table('t1', m2, autoload=True)
assert type(t2.c.data.type) is CHAR
eq_(
t2.select().where(t2.c.data == 'value 2').execute().fetchall(),
[(2, 'value 2 ')]
)
finally:
t.drop()
示例5: test_numeric_infinity_float
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_numeric_infinity_float(self):
m = self.metadata
t1 = Table('t1', m,
Column("intcol", Integer),
Column("numericcol", oracle.BINARY_DOUBLE(asdecimal=False)))
t1.create()
t1.insert().execute([
dict(
intcol=1,
numericcol=float("inf")
),
dict(
intcol=2,
numericcol=float("-inf")
),
])
eq_(
select([t1.c.numericcol]).
order_by(t1.c.intcol).execute().fetchall(),
[(float('inf'), ), (float('-inf'), )]
)
eq_(
testing.db.execute(
"select numericcol from t1 order by intcol").fetchall(),
[(float('inf'), ), (float('-inf'), )]
)
示例6: test_column_accessor_shadow
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_column_accessor_shadow(self):
shadowed = Table(
'test_shadowed', self.metadata,
Column('shadow_id', INT, primary_key=True),
Column('shadow_name', VARCHAR(20)),
Column('parent', VARCHAR(20)),
Column('row', VARCHAR(40)),
Column('_parent', VARCHAR(20)),
Column('_row', VARCHAR(20)),
)
self.metadata.create_all()
shadowed.insert().execute(
shadow_id=1, shadow_name='The Shadow', parent='The Light',
row='Without light there is no shadow',
_parent='Hidden parent', _row='Hidden row')
r = shadowed.select(shadowed.c.shadow_id == 1).execute().first()
eq_(r.shadow_id, 1)
eq_(r['shadow_id'], 1)
eq_(r[shadowed.c.shadow_id], 1)
eq_(r.shadow_name, 'The Shadow')
eq_(r['shadow_name'], 'The Shadow')
eq_(r[shadowed.c.shadow_name], 'The Shadow')
eq_(r.parent, 'The Light')
eq_(r['parent'], 'The Light')
eq_(r[shadowed.c.parent], 'The Light')
eq_(r.row, 'Without light there is no shadow')
eq_(r['row'], 'Without light there is no shadow')
eq_(r[shadowed.c.row], 'Without light there is no shadow')
eq_(r['_parent'], 'Hidden parent')
eq_(r['_row'], 'Hidden row')
示例7: _dont_test_numeric_nan_decimal
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def _dont_test_numeric_nan_decimal(self):
m = self.metadata
t1 = Table('t1', m,
Column("intcol", Integer),
Column("numericcol", oracle.BINARY_DOUBLE(asdecimal=True)))
t1.create()
t1.insert().execute([
dict(
intcol=1,
numericcol=decimal.Decimal("NaN")
),
dict(
intcol=2,
numericcol=decimal.Decimal("-NaN")
),
])
eq_(
select([t1.c.numericcol]).
order_by(t1.c.intcol).execute().fetchall(),
[(decimal.Decimal("NaN"), ), (decimal.Decimal("NaN"), )]
)
eq_(
testing.db.execute(
"select numericcol from t1 order by intcol").fetchall(),
[(decimal.Decimal("NaN"), ), (decimal.Decimal("NaN"), )]
)
示例8: test_default_exec
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_default_exec(self):
metadata = MetaData(testing.db)
t1 = Table(
"t1",
metadata,
Column("special_col", Integer, Sequence("special_col"), primary_key=True),
Column("data", String(50)), # to appease SQLite without DEFAULT VALUES
)
metadata.create_all()
try:
engine = metadata.bind
# reset the identifier preparer, so that we can force it to cache
# a unicode identifier
engine.dialect.identifier_preparer = engine.dialect.preparer(engine.dialect)
select([column("special_col")]).select_from(t1).execute().close()
assert isinstance(engine.dialect.identifier_preparer.format_sequence(Sequence("special_col")), str)
# now execute, run the sequence. it should run in u"Special_col.nextid" or similar as
# a unicode object; cx_oracle asserts that this is None or a String (postgresql lets it pass thru).
# ensure that executioncontext._exec_default() is encoding.
t1.insert().execute(data="foo")
finally:
metadata.drop_all()
示例9: test_select_doesnt_pollute_result
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_select_doesnt_pollute_result(self):
class MyType(TypeDecorator):
impl = Integer
def process_result_value(self, value, dialect):
raise Exception("I have not been selected")
t1 = Table(
't1', self.metadata,
Column('x', MyType())
)
t2 = Table(
't2', self.metadata,
Column('x', Integer)
)
self.metadata.create_all(testing.db)
with testing.db.connect() as conn:
conn.execute(t1.insert().values(x=5))
stmt = t2.insert().values(
x=select([t1.c.x]).as_scalar()).returning(t2.c.x)
result = conn.execute(stmt)
eq_(result.scalar(), 5)
示例10: test_limit_offset_for_update
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_limit_offset_for_update(self):
metadata = self.metadata
# oracle can't actually do the ROWNUM thing with FOR UPDATE
# very well.
t = Table(
"t1",
metadata,
Column("id", Integer, primary_key=True),
Column("data", Integer),
)
metadata.create_all()
t.insert().execute(
{"id": 1, "data": 1},
{"id": 2, "data": 7},
{"id": 3, "data": 12},
{"id": 4, "data": 15},
{"id": 5, "data": 32},
)
# here, we can't use ORDER BY.
eq_(
t.select().with_for_update().limit(2).execute().fetchall(),
[(1, 1), (2, 7)],
)
# here, its impossible. But we'd prefer it to raise ORA-02014
# instead of issuing a syntax error.
assert_raises_message(
exc.DatabaseError,
"ORA-02014",
t.select().with_for_update().limit(2).offset(3).execute,
)
示例11: test_implicit_execution
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_implicit_execution(self):
metadata = MetaData()
table = Table(
"test_table",
metadata,
Column("foo", Integer),
test_needs_acid=True,
)
conn = testing.db.connect()
metadata.create_all(bind=conn)
try:
trans = conn.begin()
metadata.bind = conn
t = table.insert()
assert t.bind is conn
table.insert().execute(foo=5)
table.insert().execute(foo=6)
table.insert().execute(foo=7)
trans.rollback()
metadata.bind = None
assert (
conn.execute("select count(*) from test_table").scalar() == 0
)
finally:
metadata.drop_all(bind=conn)
示例12: setup
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def setup(self):
self.engine = engines.reconnecting_engine()
self.meta = MetaData(self.engine)
table = Table('sometable', self.meta,
Column('id', Integer, primary_key=True),
Column('name', String(50)))
self.meta.create_all()
table.insert().execute(
[{'id': i, 'name': 'row %d' % i} for i in range(1, 100)]
)
示例13: test_autoincrement_fk
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_autoincrement_fk(self):
nodes = Table('nodes', self.metadata,
Column('id', Integer, primary_key=True),
Column('parent_id', Integer, ForeignKey('nodes.id')),
Column('data', String(30)))
nodes.create()
r = nodes.insert().execute(data='foo')
id_ = r.inserted_primary_key[0]
nodes.insert().execute(data='bar', parent_id=id_)
示例14: test_create_same_names_implicit_schema
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_create_same_names_implicit_schema(self):
meta = self.metadata
parent = Table('parent',
meta,
Column('pid', Integer, primary_key=True))
child = Table('child', meta,
Column('cid', Integer, primary_key=True),
Column('pid', Integer, ForeignKey('parent.pid')))
meta.create_all()
parent.insert().execute({'pid': 1})
child.insert().execute({'cid': 1, 'pid': 1})
eq_(child.select().execute().fetchall(), [(1, 1)])
示例15: test_interval
# 需要导入模块: from sqlalchemy.testing.schema import Table [as 别名]
# 或者: from sqlalchemy.testing.schema.Table import insert [as 别名]
def test_interval(self):
metadata = self.metadata
interval_table = Table('intervaltable', metadata, Column('id',
Integer, primary_key=True,
test_needs_autoincrement=True),
Column('day_interval',
oracle.INTERVAL(day_precision=3)))
metadata.create_all()
interval_table.insert().\
execute(day_interval=datetime.timedelta(days=35, seconds=5743))
row = interval_table.select().execute().first()
eq_(row['day_interval'], datetime.timedelta(days=35,
seconds=5743))