def insert_row(table, insert):
"""
Execute an ``INSERT`` on a table.
:param basestring table: table name
:param dict insert: inserted data, with keys as the column name and
values as the corresponding values
:returns: the id of the inserted row
:rtype: int or long
:raises AlreadyExists: if the row already exists and insertion would
result in duplicate keys
"""
try:
keys = _encode_name(insert.keys())
values = ','.join([_encode(value) for value in insert.values()])
return _execute("INSERT INTO %s (%s) VALUES (%s);"
% (_encode_name(table), keys, values),
write=True, ret='lastrowid')
except MySQLdb.IntegrityError as e:
if e[0] == 1062:
raise AlreadyExists
raise
评论列表
文章目录