如果值相同,如何根据循环打印唯一代码?

时间:2019-04-05 03:14:11

标签: python postgresql psycopg2

如果值相同,如何根据循环打印唯一代码?

FullChar = 'CEFLMPRTVWXYK0123456789'
total = 1000
count = 10
count = int(count)
entries = []
bcd = ""
flg = ""
rll = ""

try:
    conn = psycopg2.connect(host="192.168.13.10",database="postgres", port="5432", user="postgres", password="potatona1")
    cursor = conn.cursor()

    def inputDatabase(data):
        postgres_insert_query = """INSERT INTO unique_code(unique_code, barcode, flag, roll) VALUES (%s,%s,%s,%s)"""
        cursor.executemany(postgres_insert_query, data)
        conn.commit()


    for i in range(5):
        for x in range(total):    # banyaknya code yang di print
            unique_code = ''.join(random.sample(FullChar, count - 1))
            unique_code = ''.join(random.sample(unique_code, len(unique_code)))

        entry = (unique_code, bcd, flg, rll)
        entries.append(entry)

        inputDatabase(entries)
        print(i)

    count = cursor.rowcount
    print (count, "Record inserted successfully into mobile table")

except (Exception, psycopg2.DatabaseError) as error:
    print(error)
    conn.rollback()

exmp: 如果此代码MTY9X4L2E再次显示/重复出现,则循环将停止 我收到此消息 重复的键值违反了唯一约束“ unique_code_pkey”

1 个答案:

答案 0 :(得分:1)

要跟踪唯一值,请使用set

<div class="level">
   <ul>
      <li>
         <a href="#">
            <div>
               <img src="<?php echo "img_avatar.png" ?>" class="img-responsive img-circle main-img">
            </div>
         </a>
         <ul class="sub-level">
            <li>
               <a href="#">
                  <div>
                     <img src="<?php echo "img_avatar.png" ?>" class="img-responsive img-circle sub-img">
                  </div>
               </a>
            </li>
            <li>
               <a href="#">
                  <div>
                     <img src="<?php echo "img_avatar.png" ?>" class="img-responsive img-circle sub-img">
                  </div>
               </a>
            </li>
            <li>
               <a href="#">
                  <div>
                     <img src="<?php echo "img_avatar.png" ?>" class="img-responsive img-circle sub-img">
                  </div>
               </a>
            </li>
            <li>
               <a href="#">
                  <div>
                     <img src="<?php echo "img_avatar.png" ?>" class="img-responsive img-circle sub-img">
                  </div>
               </a>
            </li>
            <li>
               <a href="#">
                  <div>
                     <img src="<?php echo "img_avatar.png" ?>" class="img-responsive img-circle sub-img">
                  </div>
               </a>
            </li>
            <li>
               <a href="#">
                  <div>
                     <img src="<?php echo "img_avatar.png" ?>" class="img-responsive img-circle sub-img">
                  </div>
               </a>
            </li>
            <li>
               <a href="#">
                  <div>
                     <img src="<?php echo "img_avatar.png" ?>" class="img-responsive img-circle sub-img">
                  </div>
               </a>
            </li>
         </ul>
      </li>
   </ul>
</div>

不清楚这些循环在做什么; unique_codes = set() ... for i in range(5): for x in range(total): # banyaknya code yang di print unique_code = ''.join(random.sample(FullChar, count - 1)) unique_code = ''.join(random.sample(unique_code, len(unique_code))) if unique_code in unique_codes: # The unique code has already been used. # Do something? else: # Add the code to the set of used codes. unique_codes.add(unique_code) 在内部循环的每次迭代中都会被覆盖。

该示例代码还有另一个问题:unique_code列表永远不会清除,因此外循环的第二次迭代将导致重复的键错误,因为entries不仅包含新数据,而且还包含前一次迭代的数据。每次调用entries后,应清除或重新初始化entries

inputDatabase