Here are the examples of the python api sqlalchemy.insert taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
10 Examples
3
Example 1
Project: zipline Source File: asset_writer.py
def write_version_info(conn, version_table, version_value):
"""
Inserts the version value in to the version table.
Parameters
----------
conn : sa.Connection
The connection to use to execute the insert.
version_table : sa.Table
The version table of the asset database
version_value : int
The version to write in to the database
"""
conn.execute(sa.insert(version_table, values={'version': version_value}))
3
Example 2
Project: sqlalchemy Source File: test_insert.py
def test_generic_insert_bind_params_all_columns(self):
table1 = self.tables.mytable
self.assert_compile(insert(table1),
'INSERT INTO mytable (myid, name, description) '
'VALUES (:myid, :name, :description)')
3
Example 3
Project: sqlalchemy Source File: test_insert.py
def test_insert_with_values_dict(self):
table1 = self.tables.mytable
checkparams = {
'myid': 3,
'name': 'jack'
}
self.assert_compile(
insert(
table1,
dict(
myid=3,
name='jack')),
'INSERT INTO mytable (myid, name) VALUES (:myid, :name)',
checkparams=checkparams)
3
Example 4
def test_unconsumed_names_values_dict(self):
table1 = self.tables.mytable
checkparams = {
'myid': 3,
'name': 'jack',
'unknowncol': 'oops'
}
stmt = insert(table1, values=checkparams)
assert_raises_message(
exc.CompileError,
'Unconsumed column names: unknowncol',
stmt.compile,
dialect=postgresql.dialect()
)
3
Example 5
Project: sqlalchemy Source File: test_insert.py
def test_insert_with_values_tuple(self):
table1 = self.tables.mytable
checkparams = {
'myid': 3,
'name': 'jack',
'description': 'mydescription'
}
self.assert_compile(insert(table1, (3, 'jack', 'mydescription')),
'INSERT INTO mytable (myid, name, description) '
'VALUES (:myid, :name, :description)',
checkparams=checkparams)
3
Example 6
Project: sqlalchemy Source File: test_insert.py
def test_insert_with_user_supplied_bind_params(self):
table1 = self.tables.mytable
values = {
table1.c.myid: bindparam('userid'),
table1.c.name: bindparam('username')
}
self.assert_compile(
insert(
table1,
values),
'INSERT INTO mytable (myid, name) VALUES (:userid, :username)')
3
Example 7
Project: sqlalchemy Source File: test_insert.py
def test_insert_values(self):
table1 = self.tables.mytable
values1 = {table1.c.myid: bindparam('userid')}
values2 = {table1.c.name: bindparam('username')}
self.assert_compile(
insert(
table1,
values=values1).values(values2),
'INSERT INTO mytable (myid, name) VALUES (:userid, :username)')
0
Example 8
Project: sqlalchemy Source File: test_insert.py
def test_unconsumed_names_multi_values_dict(self):
table1 = self.tables.mytable
checkparams = [{
'myid': 3,
'name': 'jack',
'unknowncol': 'oops'
}, {
'myid': 4,
'name': 'someone',
'unknowncol': 'oops'
}]
stmt = insert(table1, values=checkparams)
assert_raises_message(
exc.CompileError,
'Unconsumed column names: unknowncol',
stmt.compile,
dialect=postgresql.dialect()
)
0
Example 9
Project: sqlalchemy Source File: test_insert.py
def test_insert_with_values_func(self):
table1 = self.tables.mytable
self.assert_compile(insert(table1, values=dict(myid=func.lala())),
'INSERT INTO mytable (myid) VALUES (lala())')
0
Example 10
Project: bag Source File: mediovaigel.py
def _load_row(self, original_values):
from sqlalchemy import insert
values = copy(original_values)
table = resolve(values.pop(0)) # TODO Cache
cols = list(enumerate(table.c.keys())) # TODO Cache
for index, col in cols:
fk = foreign_key_from_col(table.c[col])
if fk:
# Replace the old FK value with the NEW id stored in mapp
old_fk_value = values[index]
if old_fk_value is None:
continue
try:
values[index] = self._get_new_id(fk, old_fk_value)
except KeyError as e:
print('Delaying {} row for lack of {}'.format(
table.name, e))
# Store this job so it will be retried later:
self._delay_creation(
e.args[0], self._load_row, original_values)
return False
self.sas.execute(insert(table, values=values))
return True