Home > Database > Mysql Tutorial > SCO下用gcc编译调用mysql数据库的程序_MySQL

SCO下用gcc编译调用mysql数据库的程序_MySQL

WBOY
Release: 2016-06-01 14:12:37
Original
1400 people have browsed it

  环境:SCO OpenServer 5.0.6, mysql 3.23.42,gcc 2.95.2
  
  备注:gcc已集成了支持mysql的函数,可以在C源代码中直接调用数据库,不用像informix那样要用ESQL/C嵌入调用.
  
  1.makefile文件
  
  CC=gcc
  
  LIB=-lcurses -lsocket -lmysqlclient -lm -lc -lnsl
  
  all:gspser
  
  gspser:get_data.o getfileargv.o
  
  $(CC) -o gj get_data.o getfileargv.o $(LIB)
  
  get_data.o:get_data.c
  
  $(CC) -c get_data.c
  
  getfileargv.o:getfileargv.c
  
  $(CC) -c getfileargv.c
  
  clean:
  
  rm -f *.o
  
  2.get_data.c文件
  
  #include
  
  #include /local/mysql/include/mysql.h>
  
  #define DBCFG "./gsp.conf"
  
  main()
  
  {
  
  char dbhost[32],dbuser[16],dbpasswd[16],db[16];
  
  char query[256];
  
  int count;
  
  MYSQL * mysql; /*表示对一个数据库连接的句柄*/
  
  MYSQL_RES *mysql_res; /*代表返回行的一个查询的结果*/
  
  MYSQL_ROW mysql_row; /*字符串数组*/
  
  my_ulonglong rows;
  
  /*该类型用于行编号和mysql_affected_rows()、mysql_num_rows()和mysql_insert_id()
  
  */
  
  getfileargv(DBCFG,"DBHOST",dbhost); /*从文件中读取gsp.conf*/
  
  getfileargv(DBCFG,"DBUSER",dbuser);
  
  getfileargv(DBCFG,"DBPASSWD",dbpasswd);
  
  getfileargv(DBCFG,"DATABASE",db);
  
  if(!(mysql=mysql_init(NULL))) /*获得或初始化一个MYSQL结构*/
  
  {
  
  rintf("mysql_init失败!");
  
  mysql_close(mysql);
  
  exit(0);
  
  }
  
  if(!mysql_real_connect(mysql,dbhost,dbuser,dbpasswd,db,0,NULL,0))
  
  { /*连接一个MySQL服务器*/
  
  rintf("连接服务器失败,请联系系统管理人员!");
  
  mysql_close(mysql);
  
  exit(0);
  
  }
  
  trcpy(query,"select * from zuh where jgm=\"11110501\" order by zuh");
  
  if(mysql_query(mysql,query)) /*执行指定为一个空结尾的字符串的SQL查询*/
  
  {
  
  rintf("mysql_query出错!");
  
  mysql_close(mysql);
  
  exit(0);
  
  }
  
  mysql_res=mysql_store_result(mysql); /*检索一个完整的结果集合给客户*/
  
  rows=mysql_num_rows(mysql_res); /*返回一个结果集合重的列的数量*/
  
  if(rows==0) /*此管理码不存在 返回*/
  
  {
  
  rintf("返回值为空");
  
  mysql_free_result(mysql_res); /*释放一个结果集合使用的内存*/
  
  mysql_close(mysql); /*关闭一个服务器连接*/
  
  exit(0);
  
  }
  
  for(count=0; count  
  {
  
  mysql_row=mysql_fetch_row(mysql_res); /*从结果集合中取得下一行*/
  
  rintf("社号:%s ",mysql_row[0]);
  
  rintf("组号:%s ",mysql_row[1]);
  
  rintf("组名:%s\n",mysql_row[2]);
  
  }
  
  mysql_free_result(mysql_res);
  
  mysql_close(mysql);
  
  }
  
  3.getfileargv.c文件
  
  #include
  
  int getfileargv(const char * filename,const char * argvname, char * retbuf)
  
  {
  
  FILE *fp;
  
  char line[128];
  
  char tempbuf[40];
  
  trncpy(tempbuf,argvname,30);
  
  tempbuf[39]=0;
  
  if ((fp=fopen(filename,"r"))==NULL)
  
  return -1;
  
  trcat(tempbuf,"=");
  
  while(fgets(line,120,fp))
  
  {
  
  if(line[0]=='#')
  
  continue;
  
  if (!strncmp(line,tempbuf,strlen(tempbuf)))
  
  {
  
  fclose(fp);
  
  line[strlen(line)-1]=0;
  
  trcpy(line,(char *)(strchr(line,'=')+1));
  
  trcpy(retbuf,line);
  
  return 0;
  
  }
  
  }
  
  fclose(fp);
  
  return -2;
  
  }
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template