mysql触发器不允许我检索我的LAST_INSERT_ID

时间:2019-06-23 12:25:20

标签: python triggers insert mysql-connector-python last-insert-id

我正在尝试检索插入的行的ID,我需要将其插入到其他表的行中。但我不断获得0张照片

我创建了一个带有触发器的表,该触发器允许自动增加我的VARCHAR主键,但是我认为它不允许我检索我的last_insert_ID。我已经检查了数据库端(访问:MyPHPAdmin),并且仍然保持0的返回值。我还创建了一个没有触发器的类似数据库,并将主ID设置为整数,并且所有Last_Insert_ID检索方法都能正常工作。

考虑到我的打印为0(零),我将触发器设置值更改为空白'',而不是NULL。这没有任何改变。

cc_ins_sql = """INSERT into client_contractor(client_contractor, category, address, phone_number) values(%s,%s,%s,%s)"""
cc = raw_input("Client/ Contractor Name: ")
cat = raw_input("Client or Contractor?: ")
cc_addy = raw_input("Client/ Contractor Address: ")
pn = raw_input("Phone Number: ")
cc_ins_val = (cc, cat, cc_addy, pn)
my_cur.execute(cc_ins_sql, cc_ins_val)
my_cur.execute('SELECT LAST_INSERT_ID()')
c_id = my_cur.fetchone()
print c_id
my_db.commit()

我的表并触发创建以(提供更多见解)

def c_c():
    cc_seq_table = "CREATE TABLE cc_seq(id INTEGER NOT NULL AUTO_INCREMENT 
    PRIMARY KEY);"
    my_cur.execute(cc_seq_table, my_db)
    try:
        cc_sql = """CREATE TABLE client_contractor(client_contractor_ID     
        VARCHAR(6) NOT NULL PRIMARY KEY, client_contractor text, category 
        text, address text, phone_number text);"""
        my_cur.execute(cc_sql, my_db)
        cc_trigger = """CREATE TRIGGER tg_client_contractor_insert BEFORE 
        INSERT on client_contractor for each row BEGIN INSERT into cc_seq 
        VALUES(NULL); SET NEW.client_contractor_ID =                
        CONCAT('CC', LPAD(LAST_INSERT_ID(), 4, '0')); END"""
        my_cur.execute(cc_trigger)
        my_db.commit()
    except Error as e:
        print e
        return None

image link provided below of output from MyPHPAdmin

0 个答案:

没有答案