Python 重复错误
我正在使用下面的模块生成一个唯一的id。但是每天它都会抛出Python 重复错误,python,mysql,Python,Mysql,我正在使用下面的模块生成一个唯一的id。但是每天它都会抛出IntegrityError:(1062,“对键'PRIMARY'重复输入'122830001'),用于第一个loanid 我找不到问题的原因。请帮忙 def generate_loanid(leadid='0', counter=0): """ Locks the customer_loanid table retrieves maximum loanid from it. Generates a new loa
IntegrityError:(1062,“对键'PRIMARY'重复输入'122830001')
,用于第一个loanid
我找不到问题的原因。请帮忙
def generate_loanid(leadid='0', counter=0):
""" Locks the customer_loanid table retrieves maximum loanid from it.
Generates a new loanid and puts it back in the table.
Returns the new loanid.
"""
def gen_id(max_loanid=0, count=0):
"""
Increments the counter if an id exists for the day.
Else resets the counter and creates a fresh one.
"""
timestamp_now = time.localtime()
if max_loanid:
logger.debug("""Current Maximum Loan id :
(Year(YY)+Julian Day(DDD)+Counter(CCCC) %d,
Current timestamp : %s""" %(max_loanid, timestamp_now))
julian_day = (max_loanid/10000) % 1000
if julian_day == timestamp_now[7]:
count = max_loanid % 10000
return (str(timestamp_now[0])[2:] +
str(timestamp_now[7]).rjust(3, '0') +
str(count+1).rjust(4, '0')
)
logger.debug("Leadid:%s - Counter:%s"%(leadid, counter))
db_obj = dbLis(pooling=False)
try:
try:
db_obj.query("lock tables customer_loanid write")
max_loanid = db_obj.query("select max(loanid) as loanid from customer_loanid")
curr_loanid = gen_id(max_loanid = max_loanid.__len__() and max_loanid[0].loanid)
db_obj.insert('customer_loanid', loanid=curr_loanid)
except (MySQLdb.IntegrityError,MySQLdb.OperationalError):
logger.warning(traceback.format_exc())
#There is no logical backing for this piece of code.
if counter < 3:
db_obj.query("unlock tables")
return generate_loanid(counter=counter+1)
else:
raise
finally:
try:
db_obj.query("unlock tables")
logger.debug("Unlocked All Tables")
db_obj.ctx.db.close()
except MySQLdb.OperationalError:
logger.info("Table unlocked already")
logger.debug(traceback.format_exc())
logger.info("Generated Loanid : %s"%(curr_loanid))
return curr_loanid
您可以在一个查询中完成这一切:
timestamp_now = time.localtime()
day_part_of_id = str(timestamp_now[0])[2:] + str(timestamp_now[7]).rjust(3, '0')
min_id_of_day = day_part_of_id + '0001'
sql = "INSERT INTO customer_loanid (SELECT "+min_id_of_day+" + count(*) FROM customer_loanid WHERE loanid LIKE '"+day_part_of_id+"%'"
为什么要使用
max\u loanid.\uu len\uunid()
而不是len(max\u loanid)
?@shiva:我怀疑,但直接调用特殊方法通常不是一个好主意。@MartijnPieters谢谢。我将它改为len()。你为什么需要如此复杂的方法来计算loanid呢?带有AUTO_INCREMENT
的id不会起作用吗?@shiva似乎问题在于,计数器以“下一个可用id”的方式工作会产生“溢出”(在代码中由count=max_loanid%10000处理)。这意味着,如果给定日期的ID数超过10000,则返回的ID将被剥离(计数10001将给出计数1),因此会生成冲突。min_ID_of_day=day_part_of_ID+“0001”将抛出重复错误,但在查询中,ID将添加到该前缀已被删除的ID数(视为一个数字). 当表中已有三个ID具有相同前缀时,ID将为XXXXX 0004。
timestamp_now = time.localtime()
day_part_of_id = str(timestamp_now[0])[2:] + str(timestamp_now[7]).rjust(3, '0')
min_id_of_day = day_part_of_id + '0001'
sql = "INSERT INTO customer_loanid (SELECT "+min_id_of_day+" + count(*) FROM customer_loanid WHERE loanid LIKE '"+day_part_of_id+"%'"