获取数据库的key值

#include <stdlib.h>

#include<stdio.h>

#include <mysql.h>


int main()

{

    MYSQL my_connection;

    MYSQL_RES *res_ptr;

    MYSQL_ROW  sqlrow;

    int res;

          

    mysql_init(&my_connection);                                                                                                                if(mysql_real_connect(&my_connection,"localhost","rick","bar","rick",0,NULL,0))           

    {

       printf("connection success\n");

       res=mysql_query(&my_connection,"insert into children (fname ,age )  values(‘robert’,7)");

       if(!res)

       {

             printf("inserted   %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));

       }

       else

     {

       fprintf(stderr,"insert error %d:%s\n",mysql_errno(&my_connection),mysql_error(&my_connection));

     }

     res=mysql_query(&my_connetion,"select LAST_INSERT_ID()");

     if(res)

     {

         printf("select error:%s\n",mysql_error(&my_connection));

     }

     else

     {

         res_ptr = mysql_use_result(&my_connection);                 //获取最后一次操作的行号

         if(res_ptr)

         {

              while((sqlrow=mysql_fetch_row(res_ptr)))

             {

                  printf("we inserted childno %s\n",sqlrow[0]);

             }

             mysql_free_result(res_ptr);

         }

         mysql_close(&my_connection);

     }

}


运行之后的结果:

 connection   sucess

inserted  1 rows

we  insertde childno  6



若想返回sqlrow[0]


int mysql_result()
{
    int *id=0;     //定义一个地址
    MYSQL_RES* res=mysql_use_result(pDB->getMySQL());
    if(res)
    {
        while((Row=mysql_fetch_row(res)))
        {
            printf("we inserted row is %s\n",Row[0]);
            *id=atoi(Row[0]);
            printf("mysql id ==%d\n",*id);
        }        
        
    }
    mysql_free_result(res);

    return *id;          //返回地址即可

}




这是我在linux程序设计p292,查找到的


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值