• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python table.insert函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sqlalchemy.table.insert函数的典型用法代码示例。如果您正苦于以下问题:Python insert函数的具体用法?Python insert怎么用?Python insert使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了insert函数的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_inline_default

    def test_inline_default(self):
        metadata = MetaData()
        table = Table('sometable', metadata,
                      Column('id', Integer, primary_key=True),
                      Column('foo', Integer, default=func.foobar()))

        self.assert_compile(table.insert(values={}, inline=True),
                            'INSERT INTO sometable (foo) VALUES (foobar())')

        self.assert_compile(
            table.insert(
                inline=True),
            'INSERT INTO sometable (foo) VALUES (foobar())',
            params={})
开发者ID:robin900,项目名称:sqlalchemy,代码行数:14,代码来源:test_insert.py


示例2: test_python_fn_default

    def test_python_fn_default(self):
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", String),
            Column("foo", Integer, default=lambda: 10),
        )

        values = [
            {"id": 1, "data": "data1"},
            {"id": 2, "data": "data2", "foo": 15},
            {"id": 3, "data": "data3"},
        ]

        checkparams = {
            "id_m0": 1,
            "id_m1": 2,
            "id_m2": 3,
            "data_m0": "data1",
            "data_m1": "data2",
            "data_m2": "data3",
            "foo": None,  # evaluated later
            "foo_m1": 15,
            "foo_m2": None,  # evaluated later
        }

        stmt = table.insert().values(values)
        eq_(
            dict(
                [
                    (k, v.type._type_affinity)
                    for (k, v) in stmt.compile(
                        dialect=postgresql.dialect()
                    ).binds.items()
                ]
            ),
            {
                "foo": Integer,
                "data_m2": String,
                "id_m0": Integer,
                "id_m2": Integer,
                "foo_m1": Integer,
                "data_m1": String,
                "id_m1": Integer,
                "foo_m2": Integer,
                "data_m0": String,
            },
        )

        self.assert_compile(
            stmt,
            "INSERT INTO sometable (id, data, foo) VALUES "
            "(%(id_m0)s, %(data_m0)s, %(foo)s), "
            "(%(id_m1)s, %(data_m1)s, %(foo_m1)s), "
            "(%(id_m2)s, %(data_m2)s, %(foo_m2)s)",
            checkparams=checkparams,
            dialect=postgresql.dialect(),
        )
开发者ID:vrajmohan,项目名称:sqlalchemy,代码行数:60,代码来源:test_insert.py


示例3: test_sql_expression_pk_noautoinc_returning

    def test_sql_expression_pk_noautoinc_returning(self):
        # test that return_defaults() works with a primary key where we are
        # sending a SQL expression, and we need to get the server-calculated
        # value back.  [ticket:3133]
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, autoincrement=False, primary_key=True),
            Column("data", String),
        )

        stmt = table.insert().return_defaults().values(id=func.foobar())
        returning_dialect = postgresql.dialect()
        returning_dialect.implicit_returning = True
        compiled = stmt.compile(
            dialect=returning_dialect, column_keys=["data"]
        )
        eq_(compiled.postfetch, [])
        eq_(compiled.returning, [table.c.id])

        self.assert_compile(
            stmt,
            "INSERT INTO sometable (id, data) VALUES "
            "(foobar(), %(data)s) RETURNING sometable.id",
            checkparams={"data": "foo"},
            params={"data": "foo"},
            dialect=returning_dialect,
        )
开发者ID:BY-jk,项目名称:sqlalchemy,代码行数:29,代码来源:test_insert.py


示例4: test_server_default

    def test_server_default(self):
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", String),
            Column("foo", Integer, server_default=func.foobar()),
        )

        values = [
            {"id": 1, "data": "data1"},
            {"id": 2, "data": "data2", "foo": "plainfoo"},
            {"id": 3, "data": "data3"},
        ]

        checkparams = {
            "id_m0": 1,
            "id_m1": 2,
            "id_m2": 3,
            "data_m0": "data1",
            "data_m1": "data2",
            "data_m2": "data3",
        }

        self.assert_compile(
            table.insert().values(values),
            "INSERT INTO sometable (id, data) VALUES "
            "(%(id_m0)s, %(data_m0)s), "
            "(%(id_m1)s, %(data_m1)s), "
            "(%(id_m2)s, %(data_m2)s)",
            checkparams=checkparams,
            dialect=postgresql.dialect(),
        )
开发者ID:vrajmohan,项目名称:sqlalchemy,代码行数:34,代码来源:test_insert.py


示例5: test_inline_default

    def test_inline_default(self):
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("foo", Integer, default=func.foobar()),
        )

        self.assert_compile(
            table.insert(values={}, inline=True),
            "INSERT INTO sometable (foo) VALUES (foobar())",
        )

        self.assert_compile(
            table.insert(inline=True),
            "INSERT INTO sometable (foo) VALUES (foobar())",
            params={},
        )
开发者ID:vrajmohan,项目名称:sqlalchemy,代码行数:19,代码来源:test_insert.py


示例6: insert

    def insert(self, table, **kw):
        """
        Convenience insert of a single row to the specified table.

        Executes as an operation, returning no value.

        :param table: The table to insert into.
        :param kw: The columns to insert and their values.

        :rtype: twisted.internet.defer.Deferred
        """
        return self.operation(table.insert().values(**kw))
开发者ID:joshbenner,项目名称:mudsling,代码行数:12,代码来源:db.py


示例7: test_insert_literal_binds_sequence_notimplemented

    def test_insert_literal_binds_sequence_notimplemented(self):
        table = Table("x", MetaData(), Column("y", Integer, Sequence("y_seq")))
        dialect = default.DefaultDialect()
        dialect.supports_sequences = True

        stmt = table.insert().values(myid=3, name="jack")

        assert_raises(
            NotImplementedError,
            stmt.compile,
            compile_kwargs=dict(literal_binds=True),
            dialect=dialect,
        )
开发者ID:vrajmohan,项目名称:sqlalchemy,代码行数:13,代码来源:test_insert.py


示例8: test_insert_from_select_no_defaults

 def test_insert_from_select_no_defaults(self):
     metadata = MetaData()
     table = Table('sometable', metadata,
                   Column('id', Integer, primary_key=True),
                   Column('foo', Integer, default=func.foobar()))
     table1 = self.tables.mytable
     sel = select([table1.c.myid]).where(table1.c.name == 'foo')
     ins = table.insert().\
         from_select(["id"], sel, include_defaults=False)
     self.assert_compile(
         ins,
         "INSERT INTO sometable (id) SELECT mytable.myid "
         "FROM mytable WHERE mytable.name = :name_1",
         checkparams={"name_1": "foo"}
     )
开发者ID:robin900,项目名称:sqlalchemy,代码行数:15,代码来源:test_insert.py


示例9: test_python_scalar_default

    def test_python_scalar_default(self):
        metadata = MetaData()
        table = Table('sometable', metadata,
                      Column('id', Integer, primary_key=True),
                      Column('data', String),
                      Column('foo', Integer, default=10))

        values = [
            {'id': 1, 'data': 'data1'},
            {'id': 2, 'data': 'data2', 'foo': 15},
            {'id': 3, 'data': 'data3'},
        ]

        checkparams = {
            'id_m0': 1,
            'id_m1': 2,
            'id_m2': 3,
            'data_m0': 'data1',
            'data_m1': 'data2',
            'data_m2': 'data3',
            'foo': None,  # evaluated later
            'foo_m1': 15,
            'foo_m2': None  # evaluated later
        }

        stmt = table.insert().values(values)

        eq_(
            dict([
                (k, v.type._type_affinity)
                for (k, v) in
                stmt.compile(dialect=postgresql.dialect()).binds.items()]),
            {
                'foo': Integer, 'data_m2': String, 'id_m0': Integer,
                'id_m2': Integer, 'foo_m1': Integer, 'data_m1': String,
                'id_m1': Integer, 'foo_m2': Integer, 'data_m0': String}
        )

        self.assert_compile(
            stmt,
            'INSERT INTO sometable (id, data, foo) VALUES '
            '(%(id_m0)s, %(data_m0)s, %(foo)s), '
            '(%(id_m1)s, %(data_m1)s, %(foo_m1)s), '
            '(%(id_m2)s, %(data_m2)s, %(foo_m2)s)',
            checkparams=checkparams,
            dialect=postgresql.dialect())
开发者ID:robin900,项目名称:sqlalchemy,代码行数:46,代码来源:test_insert.py


示例10: test_insert_from_select_with_python_defaults

 def test_insert_from_select_with_python_defaults(self):
     metadata = MetaData()
     table = Table('sometable', metadata,
                   Column('id', Integer, primary_key=True),
                   Column('foo', Integer, default=12))
     table1 = self.tables.mytable
     sel = select([table1.c.myid]).where(table1.c.name == 'foo')
     ins = table.insert().\
         from_select(["id"], sel)
     self.assert_compile(
         ins,
         "INSERT INTO sometable (id, foo) SELECT "
         "mytable.myid, :foo AS anon_1 "
         "FROM mytable WHERE mytable.name = :name_1",
         # value filled in at execution time
         checkparams={"name_1": "foo", "foo": None}
     )
开发者ID:robin900,项目名称:sqlalchemy,代码行数:17,代码来源:test_insert.py


示例11: test_insert_from_select_with_sql_defaults

 def test_insert_from_select_with_sql_defaults(self):
     metadata = MetaData()
     table = Table(
         "sometable",
         metadata,
         Column("id", Integer, primary_key=True),
         Column("foo", Integer, default=func.foobar()),
     )
     table1 = self.tables.mytable
     sel = select([table1.c.myid]).where(table1.c.name == "foo")
     ins = table.insert().from_select(["id"], sel)
     self.assert_compile(
         ins,
         "INSERT INTO sometable (id, foo) SELECT "
         "mytable.myid, foobar() AS foobar_1 "
         "FROM mytable WHERE mytable.name = :name_1",
         checkparams={"name_1": "foo"},
     )
开发者ID:vrajmohan,项目名称:sqlalchemy,代码行数:18,代码来源:test_insert.py


示例12: test_insert_from_select_override_defaults

 def test_insert_from_select_override_defaults(self):
     metadata = MetaData()
     table = Table('sometable', metadata,
                   Column('id', Integer, primary_key=True),
                   Column('foo', Integer, default=12))
     table1 = self.tables.mytable
     sel = select(
         [table1.c.myid, table1.c.myid.label('q')]).where(
         table1.c.name == 'foo')
     ins = table.insert().\
         from_select(["id", "foo"], sel)
     self.assert_compile(
         ins,
         "INSERT INTO sometable (id, foo) SELECT "
         "mytable.myid, mytable.myid AS q "
         "FROM mytable WHERE mytable.name = :name_1",
         checkparams={"name_1": "foo"}
     )
开发者ID:robin900,项目名称:sqlalchemy,代码行数:18,代码来源:test_insert.py


示例13: test_sql_functions

    def test_sql_functions(self):
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", String),
            Column("foo", Integer),
        )

        values = [
            {"id": 1, "data": "foo", "foo": func.foob()},
            {"id": 2, "data": "bar", "foo": func.foob()},
            {"id": 3, "data": "bar", "foo": func.bar()},
            {"id": 4, "data": "bar", "foo": 15},
            {"id": 5, "data": "bar", "foo": func.foob()},
        ]
        checkparams = {
            "id_m0": 1,
            "data_m0": "foo",
            "id_m1": 2,
            "data_m1": "bar",
            "id_m2": 3,
            "data_m2": "bar",
            "id_m3": 4,
            "data_m3": "bar",
            "foo_m3": 15,
            "id_m4": 5,
            "data_m4": "bar",
        }

        self.assert_compile(
            table.insert().values(values),
            "INSERT INTO sometable (id, data, foo) VALUES "
            "(%(id_m0)s, %(data_m0)s, foob()), "
            "(%(id_m1)s, %(data_m1)s, foob()), "
            "(%(id_m2)s, %(data_m2)s, bar()), "
            "(%(id_m3)s, %(data_m3)s, %(foo_m3)s), "
            "(%(id_m4)s, %(data_m4)s, foob())",
            checkparams=checkparams,
            dialect=postgresql.dialect(),
        )
开发者ID:vrajmohan,项目名称:sqlalchemy,代码行数:42,代码来源:test_insert.py


示例14: test_sql_functions

    def test_sql_functions(self):
        metadata = MetaData()
        table = Table('sometable', metadata,
                      Column('id', Integer, primary_key=True),
                      Column('data', String),
                      Column('foo', Integer))

        values = [
            {"id": 1, "data": "foo", "foo": func.foob()},
            {"id": 2, "data": "bar", "foo": func.foob()},
            {"id": 3, "data": "bar", "foo": func.bar()},
            {"id": 4, "data": "bar", "foo": 15},
            {"id": 5, "data": "bar", "foo": func.foob()},
        ]
        checkparams = {
            'id_m0': 1,
            'data_m0': 'foo',

            'id_m1': 2,
            'data_m1': 'bar',

            'id_m2': 3,
            'data_m2': 'bar',

            'id_m3': 4,
            'data_m3': 'bar',
            'foo_m3': 15,

            'id_m4': 5,
            'data_m4': 'bar'
        }

        self.assert_compile(
            table.insert().values(values),
            "INSERT INTO sometable (id, data, foo) VALUES "
            "(%(id_m0)s, %(data_m0)s, foob()), "
            "(%(id_m1)s, %(data_m1)s, foob()), "
            "(%(id_m2)s, %(data_m2)s, bar()), "
            "(%(id_m3)s, %(data_m3)s, %(foo_m3)s), "
            "(%(id_m4)s, %(data_m4)s, foob())",
            checkparams=checkparams,
            dialect=postgresql.dialect())
开发者ID:robin900,项目名称:sqlalchemy,代码行数:42,代码来源:test_insert.py


示例15: test_server_default_absent_value

    def test_server_default_absent_value(self):
        metadata = MetaData()
        table = Table('sometable', metadata,
                      Column('id', Integer, primary_key=True),
                      Column('data', String),
                      Column('foo', Integer, server_default=func.foobar()))

        values = [
            {'id': 1, 'data': 'data1', 'foo': 'plainfoo'},
            {'id': 2, 'data': 'data2'},
            {'id': 3, 'data': 'data3', 'foo': 'otherfoo'},
        ]

        assert_raises_message(
            exc.CompileError,
            "INSERT value for column sometable.foo is explicitly rendered "
            "as a boundparameter in the VALUES clause; a Python-side value or "
            "SQL expression is required",
            table.insert().values(values).compile
        )
开发者ID:robin900,项目名称:sqlalchemy,代码行数:20,代码来源:test_insert.py


示例16: test_server_default_absent_value

    def test_server_default_absent_value(self):
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", String),
            Column("foo", Integer, server_default=func.foobar()),
        )

        values = [
            {"id": 1, "data": "data1", "foo": "plainfoo"},
            {"id": 2, "data": "data2"},
            {"id": 3, "data": "data3", "foo": "otherfoo"},
        ]

        assert_raises_message(
            exc.CompileError,
            "INSERT value for column sometable.foo is explicitly rendered "
            "as a boundparameter in the VALUES clause; a Python-side value or "
            "SQL expression is required",
            table.insert().values(values).compile,
        )
开发者ID:vrajmohan,项目名称:sqlalchemy,代码行数:23,代码来源:test_insert.py


示例17: test_insert_from_select_fn_defaults

    def test_insert_from_select_fn_defaults(self):
        metadata = MetaData()

        def foo(ctx):
            return 12

        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("foo", Integer, default=foo),
        )
        table1 = self.tables.mytable
        sel = select([table1.c.myid]).where(table1.c.name == "foo")
        ins = table.insert().from_select(["id"], sel)
        self.assert_compile(
            ins,
            "INSERT INTO sometable (id, foo) SELECT "
            "mytable.myid, :foo AS anon_1 "
            "FROM mytable WHERE mytable.name = :name_1",
            # value filled in at execution time
            checkparams={"name_1": "foo", "foo": None},
        )
开发者ID:vrajmohan,项目名称:sqlalchemy,代码行数:23,代码来源:test_insert.py


示例18: test_sql_expression_pk_autoinc_lastinserted

    def test_sql_expression_pk_autoinc_lastinserted(self):
        # test that postfetch isn't invoked for a SQL expression
        # in a primary key column.  the DB either needs to support a lastrowid
        # that can return it, or RETURNING.  [ticket:3133]
        metadata = MetaData()
        table = Table(
            "sometable",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", String),
        )

        stmt = table.insert().return_defaults().values(id=func.foobar())
        compiled = stmt.compile(dialect=sqlite.dialect(), column_keys=["data"])
        eq_(compiled.postfetch, [])
        eq_(compiled.returning, [])

        self.assert_compile(
            stmt,
            "INSERT INTO sometable (id, data) VALUES " "(foobar(), ?)",
            checkparams={"data": "foo"},
            params={"data": "foo"},
            dialect=sqlite.dialect(),
        )
开发者ID:BY-jk,项目名称:sqlalchemy,代码行数:24,代码来源:test_insert.py



注:本文中的sqlalchemy.table.insert函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python schema.Table类代码示例发布时间:2022-05-27
下一篇:
Python visitors.traverse函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap