我正在尝试检索插入的行的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