请选择 进入手机版 | 继续访问电脑版
查看: 451|回复: 0

[软件工程] linux 下c链接mysql 首先的工作

715

主题

715

帖子

2162

积分

猿er

Rank: 1

积分
2162
发表于 2016-8-14 17:53:06
<无详细内容>
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <mysql/mysql.h>
  8. int main(int argc,char *argv[])
  9. {
  10. MYSQL my_connection;
  11. mysql_init(&my_connection);
  12. int res,use;
  13. if(mysql_real_connect(&my_connection,"localhost","ma","123456ma",
  14. "student",0,0,0))
  15. {
  16. printf("connect success\n");
  17. res = mysql_query(&my_connection,"set names utf8");
  18. if(res)
  19. {
  20. fprintf(stderr,"Init char fail %s\n",mysql_error(&my_connection));
  21. return -1;
  22. }
  23. printf(" char success\n");
  24. use = mysql_query(&my_connection,"use student");
  25. if(use)
  26. {
  27. fprintf(stderr,"use database fail %s",mysql_error(&my_connection));
  28. return -2;
  29. }
  30. write(1,"use database student\n",strlen("use database student\n"));
  31. }else
  32. abort();
  33. res = mysql_query(&my_connection,"UPDATE Student SET sname='mana' where sno = '201101001'");
  34. if(!res)
  35. {
  36. printf("Updated %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
  37. }else
  38. printf("updated data fail %s \n",mysql_error(&my_connection));
  39. mysql_close(&my_connection);
  40. return 0;
  41. }
复制代码


回复

使用道具 举报