打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
last_insert_id()不可靠
看了半天mysql的代码。mysql_insert_id是这么定义的
my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql)
{
  return mysql->;last_used_con->;insert_id;
}

MYSQL是个结构体,里面包括数据库链接和一些当前数据库链接的状态值,

其中在MYSQL结构体里面有insert_id,mysql_insert_id函数返回的就是结构体里面的找个值。




QUOTE:
typedef struct st_mysql
{
  NET           net;                    /* Communication parameters */
  gptr          connector_fd;           /* ConnectorFd for SSL */
  char          *host,*user,*passwd,*unix_socket,*server_version,*host_info,*info;
  char          *db;
  struct charset_info_st *charset;
  MYSQL_FIELD   *fields;
  MEM_ROOT      field_alloc;
  my_ulonglong affected_rows;
  my_ulonglong insert_id;               /* id if insert on table with NEXTNR */
  my_ulonglong extra_info;              /* Used by mysqlshow */
  unsigned long thread_id;              /* Id for connection in server */
  unsigned long packet_length;         
  unsigned int  port;
  unsigned long client_flag,server_capabilities;
  unsigned int  protocol_version;
  unsigned int  field_count;
  unsigned int  server_status;
  unsigned int  server_language;
  unsigned int  warning_count;
  struct st_mysql_options options;
  enum mysql_status status;
  my_bool       free_me;                /* If free in mysql_close */
  my_bool       reconnect;              /* set to 1 if automatic reconnect */
  
  /* session-wide random string */
  char          scramble[SCRAMBLE_LENGTH+1];
  
/*
   Set if this is the original connection, not a master or a slave we have
   added though mysql_rpl_probe() or mysql_set_master()/ mysql_add_slave()
*/
  my_bool rpl_pivot;
  /*
    Pointers to the master, and the next slave connections, points to
    itself if lone connection.
  */
  struct st_mysql* master, *next_slave;

  struct st_mysql* last_used_slave; /* needed for round-robin slave pick */
/* needed for send/read/store/use result to work correctly with replication */
  struct st_mysql* last_used_con;

  LIST  *stmts;                     /* list of all statements */
  const struct st_mysql_methods *methods;
  void *thd;
  /*
    Points to boolean flag in MYSQL_RES  or MYSQL_STMT. We set this flag
    from mysql_stmt_close if close had to cancel result set of this object.
  */
  my_bool *unbuffered_fetch_owner;
} MYSQL;



把insert_id理解成max(id)是错误的,因为有并发存在。
结构体里面有insert_id是针对当前MYSQL连接的,而每次mysql_query操作在mysql服务器上可以理解为一次“原子”操作。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Mysql C API
The MySQL C API 编程实例
Mysql_数据库评估
ios中通过调试来使用私有api
MySQL数据库技术(18)
在MySQL中使用外键
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服