- 论坛徽章:
- 0
|
- 1 #include<stdio.h>
- 2 #include<stdlib.h>
- 3 #include<mysql.h>
- 4 #include<string.h>
- 5
-
- 24 int main(){
- 25 char* DBName = "judge";
- 26 MYSQL *conn_ptr;
-
- 28 conn_ptr = mysql_init(NULL);
- 29 conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","hjhlovehf",DBName,0,NULL,0);
- 30
- 31 MYSQL_RES* res_answer;//保存从answer表中得到的数据
- 32 MYSQL_RES* res_user_answer;//保存从user_answer_order_prim表中得到的数据
- 33
- 34 int res;
- 35
- 36 char *selectUA = "select id,user_id,user_name,answer,status from user_answer_order_prim";
- 37 char *selectA = "select answer from answer1";
- 38 char insertUAP[128];//往user_answer_precision表中插入结果 如果声明为指针将出错
- 39
- 40 res = mysql_query(conn_ptr,selectUA);
- 41 if(res){
- 42 fprintf(stderr,"select from user_answer_order_prim error:%s\n",mysql_error(conn_ptr));
- 43 exit(EXIT_FAILURE);
- 44 }
- 45 else{
- 46 res_user_answer = mysql_store_result(conn_ptr);
- 47 printf("select %lu rows from user_answer_order_prim\n",(unsigned long)mysql_num_rows(res_user_answer));
- 48 }
- 49
- 50 MYSQL_ROW row_answer;
- 51 MYSQL_ROW row_user_answer;
- 52
- 53 int flag = 1;
- 54 row_user_answer = mysql_fetch_row(res_user_answer);
- 55 int count = 0;//代表人数
- 56 int hundred = 0;//每个人100条记录
- 57 int ten = 0;//每个人的100天记录分成10份
- 58 char* user_name;
- 59 char* user_id;
- 60 double precision[10];
- 61
- 62 while(count<76){
- 63
- 64 res = mysql_query(conn_ptr,selectA);
- 65 if(res){
- 66 fprintf(stderr,"select from answer error:%s\n",mysql_error(conn_ptr));
- 67 exit(EXIT_FAILURE);
- 68 }
- 69 else{
- 70 res_answer=mysql_store_result(conn_ptr);
- 71 printf("select %lu rows from answer\n",(unsigned long)mysql_num_rows(res_answer));
- 72 }
- 73 while(flag && hundred<100){
- 74
- 75 if(hundred == 0){
- 76 user_id = row_user_answer[1];
-
- 78 user_name = row_user_answer[2];
- 79 }
- 80 double right = 0;
- 81 while(ten<10){
- 82 if(!(row_answer=mysql_fetch_row(res_answer))){
-
- 84 break;
- 85 }
- 89 if(!strcmp(row_user_answer[3],row_answer[0])){
- 90 right++;
- 91 }
- 92 if(!(row_user_answer = mysql_fetch_row(res_user_answer))){
- 93 flag = 0;
- 94 break;
- 95 }
- 96 ten++;
- 97 hundred++;
- 98 }
- 99 precision[(hundred-1)/10] = right/10;
- 100 ten =0;
- 101 }
-
- 103 count++;
- 104 hundred=0;
- 105 sprintf(insertUAP,"insert into user_answer_precision(user_id,user_name,one,two,three,four,five,six,seven,eight,nine,ten) values('%s','%s',%f,%f,%f,%f,%f,%f,%f,%f,%f,%f)",user_id ,user_name,precision[0],precision[1],precision[2],precision[3],precision[4],precision[5],precision[6],precision[7],precision[8],precision[9]);
- 106 printf("%s\n",insertUAP);
- 107 //mysql_data_seek(res_answer,0);//重置answer表结果集到第一行
- 108 mysql_free_result(res_answer);<span style="color: #FF0000;"><strong>//编译通过,运行是提示Segmentation fault</strong></span>
- 109 res = mysql_query(conn_ptr,insertUAP);
- 110 if(res){
- 111 fprintf(stderr,"insert into user_answer_precision error:%s\n",mysql_error(conn_ptr));
- 112 }
- 113 int i;
- 114 for(i = 0;i<10;i++){
- 115 precision[i]=0;
- 116 }
- 117 }
- 118
- 119
- 120 mysql_free_result(res_user_answer);
- 121 mysql_close(conn_ptr);
-
- 123 exit(EXIT_SUCCESS);
- 124
复制代码 以上程序编译通过,运行时出现Segmentation fault错误,一直没找到原因,本人对mysql c api不是很熟悉
调试时108行提示一下错误:
Program received signal SIGSEGV, Segmentation fault.
0x00641b81 in mysql_free_result () from /usr/lib/mysql/libmysqlclient.so.15
有人碰到过这种情况吗?有的话麻烦告知,还有就是运用mysql c api的时候有什么需要特别注意的?
最后想问一下关于mysql中统一中文编码问题,能不能给个详细的设置步骤。谢谢!
|
|