gpt4 book ai didi

python - 重复错误

转载 作者:行者123 更新时间:2023-11-29 14:15:11 26 4
gpt4 key购买 nike

我正在使用以下模块来生成唯一的 ID。但每天它都会为第一个贷款 ID 抛出 IntegrityError: (1062, "Duplicate entry '122830001' for key 'PRIMARY'")

我无法找到问题的原因。请帮忙?

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

表结构:

CREATE TABLE `customer_loanid` (
`loanid` int(15) NOT NULL DEFAULT '0',
PRIMARY KEY (`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+"%'"

关于python - 重复错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12815023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com