LAST_INSERT_ID() always returns 0 (RMySQL) - separate connection issue

前端 未结 3 1206
猫巷女王i
猫巷女王i 2021-01-18 14:49

Original example as found in some post

According to this post the following SQL statements should give me a vector 1, 2, 2, 2, 2 in the end:



        
3条回答
  •  天命终不由人
    2021-01-18 15:10

    You're inserting NULL values into the Primary Key column. Since you can't have two rows with the same PK, you're probably not actually inserting any real data (which is also probably an error you want to catch). Try:

    dbSendQuery(con, "INSERT INTO t VALUES(5);")
    

    Executing that should give you two different values for last_insert_id.

    Edit: misunderstood. See here for the details on LAST_INSERT_ID. Revised answer: if you don't specify a value in an AUTO_INCREMENT column, then you should get a LAST_INSERT_ID value returned. In that case, try:

    INSERT INTO t DEFAULT VALUES
    

提交回复
热议问题