本文整理汇总了Python中sql.Table.insert方法的典型用法代码示例。如果您正苦于以下问题:Python Table.insert方法的具体用法?Python Table.insert怎么用?Python Table.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sql.Table
的用法示例。
在下文中一共展示了Table.insert方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init
# 需要导入模块: from sql import Table [as 别名]
# 或者: from sql.Table import insert [as 别名]
def init(self):
from trytond.modules import get_module_info
with self.get_connection() as conn:
cursor = conn.cursor()
sql_file = os.path.join(os.path.dirname(__file__), 'init.sql')
with open(sql_file) as fp:
for line in fp.read().split(';'):
if (len(line) > 0) and (not line.isspace()):
cursor.execute(line)
ir_module = Table('ir_module')
ir_module_dependency = Table('ir_module_dependency')
for module in ('ir', 'res'):
state = 'uninstalled'
if module in ('ir', 'res'):
state = 'to install'
info = get_module_info(module)
insert = ir_module.insert(
[ir_module.create_uid, ir_module.create_date,
ir_module.name, ir_module.state],
[[0, CurrentTimestamp(), module, state]])
cursor.execute(*insert)
cursor.execute('SELECT last_insert_rowid()')
module_id, = cursor.fetchone()
for dependency in info.get('depends', []):
insert = ir_module_dependency.insert(
[ir_module_dependency.create_uid,
ir_module_dependency.create_date,
ir_module_dependency.module,
ir_module_dependency.name,
],
[[0, CurrentTimestamp(), module_id, dependency]])
cursor.execute(*insert)
conn.commit()
示例2: migrate
# 需要导入模块: from sql import Table [as 别名]
# 或者: from sql.Table import insert [as 别名]
def migrate(self):
obj = objectify.fromstring(self.content)
t = Table('ir_model_data')
for xml_record in obj.iter(tag='record'):
record = self._record(xml_record)
self.records.append(record)
sp = []
for field in self.search_params.get(record.model, record.vals.keys()):
sp.append((field, '=', record.vals[field]))
logger.info('Trying to find existing record with query: {}'.format(
sp
))
table = record.model.replace('.', '_')
q = OOQuery(table)
sql = q.select(['id']).where(sp)
logger.debug(tuple(sql))
self.cursor.execute(*sql)
res_id = self.cursor.fetchone()
if res_id:
res_id = res_id[0]
logger.info('Record {}.{} found! ({} id:{})'.format(
self.module, record.id, record.model, res_id
))
else:
logger.info('Record {}.{} not found!'.format(
self.module, record.id
))
# We have to create the model
table_model = Table(record.model.replace('.', '_'))
columns = []
values = []
for col, value in record.vals.items():
columns.append(getattr(table_model, col))
values.append(value)
sql = table_model.insert(
columns=columns, values=[values], returning=[table_model.id]
)
logger.debug(tuple(sql))
self.cursor.execute(*sql)
res_id = self.cursor.fetchone()[0]
logger.info('Creating record {}.{} ({} id:{})'.format(
self.module, record.id, record.model, res_id
))
sql = t.insert(
columns=[t.name, t.model, t.noupdate, t.res_id, t.module],
values=[(record.id, record.model, record.noupdate, res_id,
self.module)]
)
logger.debug(tuple(sql))
logger.info('Linking model data {}.{} -> record {} id:{}'.format(
self.module, record.id, record.model, res_id
))
self.cursor.execute(*sql)
示例3: test_insert_subselect
# 需要导入模块: from sql import Table [as 别名]
# 或者: from sql.Table import insert [as 别名]
def test_insert_subselect(self):
t1 = Table("t1")
t2 = Table("t2")
subquery = t2.select(t2.c1, t2.c2)
query = t1.insert([t1.c1, t1.c2], subquery)
self.assertEqual(str(query), 'INSERT INTO "t1" ("c1", "c2") ' 'SELECT "a"."c1", "a"."c2" FROM "t2" AS "a"')
self.assertEqual(query.params, ())
示例4: resets
# 需要导入模块: from sql import Table [as 别名]
# 或者: from sql.Table import insert [as 别名]
def resets(cls, dbname):
table = Table('ir_cache')
with Transaction().new_transaction() as transaction,\
transaction.connection.cursor() as cursor,\
cls._resets_lock:
cls._resets.setdefault(dbname, set())
for name in cls._resets[dbname]:
cursor.execute(*table.select(table.name,
where=table.name == name))
if cursor.fetchone():
# It would be better to insert only
cursor.execute(*table.update([table.timestamp],
[CurrentTimestamp()],
where=table.name == name))
else:
cursor.execute(*table.insert(
[table.timestamp, table.name],
[[CurrentTimestamp(), name]]))
cls._resets[dbname].clear()