LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2020|回复: 6

linux下c操作数据库

[复制链接]
发表于 2008-5-20 14:51:21 | 显示全部楼层 |阅读模式
这个是newc.c文件,我要编一个对数据库操作的程序,主要实现把这个newc.c文件编译之后的结果写入数据库mysql中,我是新手,不知道如何下手,请大家帮忙
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
char *env_init[]={"ATH=/usr/bin",NULL};
pid_t pid,pid2;
int fp1;
char *err1[]={"Compile err","Timeout","resent err","wrong answer","Accept"};

static void sig_usr(int signo)
{
if(signo==SIGALRM)
    { printf("Timeout.Killing...!");
     fflush(stdout);
    kill(pid2,SIGKILL);
   write(fp1,err1[1],7);
close(fp1);
    exit(0);
     }

  
  }

int main(void)
{
int compile=0;
int result_match=0;
char exec_result[3000];
char exec1_result[3000];
char saved_result[3000];


char ch;
pid_t pid,pid2;
int status;
int fd,fd1,fd2;

if((fd1=open("./input.txt",O_RDONLY))<0)
    printf("open error!\n");
dup2(fd1,0);
if((fd=open("./output.txt",O_WRONLY|O_CREAT|O_TRUNC|O_RDONLY))<0)
printf("open error!\n");
  dup2(fd,1);
if((fd2=open("./daan.txt",O_WRONLY|O_CREAT|O_TRUNC|O_RDONLY))<0)
  {printf("open error\n");
   exit(0);
   }
if ((fp1=open("./end.txt",O_WRONLY|O_CREAT|O_TRUNC|O_RDONLY))<0)
    {
      printf("cannot open file\n");
      exit(0);
    }


pid=fork();
if(pid<0) printf("fork error!\n");
  else if(pid==0)
          {if(execle("/usr/bin/gcc","gcc","/usr/local/newc/my.c","-o","/usr/local/newc/myt.out",(char *)0,env_init)<0)
             printf("exec error!\n");
             exit(0);
           }
if((pid=waitpid(pid,&status,0))<0)
       printf("waitpid error!\n");


/* i=ftell(fd)
{
if(i==-1L) printf("error\n")
else if (i!=0)
{
            compile=1;
            if(compile==1){
            fprintf(in,"Compile Error!");
            fclose(in);
           exit(0);}
  }
}

*/
printf("pid=%d\n",pid);
pid2=fork();
if(pid2<0) printf("fork error!\n");
      else if(pid2==0)
             {if(execlp("./myt.out",(char *)0)<0)
                   printf("exec error!\n");
                   exit(0);
              }   
if (signal(SIGALRM,sig_usr)==SIG_ERR)
     printf("can't catch SIGUSR!\n");
   alarm(2);  

if (signal(SIGCHLD,sig_usr)==SIG_ERR)
     printf("can't catch SIGUSR!\n");
   printf("pid2=%d\n",pid2);
  write(fp1,err1[4],6);
  close(fp1);
   pause();


}
 楼主| 发表于 2008-5-21 10:30:43 | 显示全部楼层

关于数据库与c连接的环境配置

要建立一个mysql和C语言交互的环境,不知道改怎么编程,有哪位高手指点一下啊?
回复 支持 反对

使用道具 举报

发表于 2008-5-21 12:45:42 | 显示全部楼层
我的想法是这样的。
mysql可以执行脚本的。你把mysql命令放在一个文件里面,取名a.sql
然后再执行 mysql -u username -p password dbname < a.sql

在c程序里面,就用execle执行上面那条命令。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-5-21 12:53:10 | 显示全部楼层

那数据库跟C之间联系的代码怎么搞?

那数据库与C语言之间联系的代码怎么写,得把它们关联起来操作,好像听麻烦的,因为这个C语言的批改、编译程序中有不同进程管理,还有不同的出错处理!
回复 支持 反对

使用道具 举报

发表于 2008-5-22 09:30:39 | 显示全部楼层
声明,我不熟悉mysql. 关于数据库与C语言之间联系的代码,我还是不清楚。 赫赫

建议你换个思路,数据库应该提供了录入数据的方式,如交互式的或者文件。
因为你的目的是“把这个newc.c文件编译之后的结果写入数据库mysql中”,你可以把结果
保存成一个文件,然后格式化成mysql认识的文件。然后在mysql脚本中,读入该文件。
应该可以达到你的要求。

具体怎么操作,你google吧。数据库与C语言之间联系的代码,没必要写。数据库肯定有输入数据的接口,至于数据怎么来的,看你自己--无论是用c程序得到的,还是shell
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-5-22 11:15:15 | 显示全部楼层

谢了

虽然还是有些不懂,但还是谢谢大家的帮忙!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-5-23 10:52:47 | 显示全部楼层

Linux下的C语言批量编译问题

pid=fork();
if(pid<0) printf("fork error!\n");
  else if(pid==0)
          {if(execle("/usr/bin/gcc","gcc","./my.c","-o","./myt.out",(char *)0,env_init)<0)
             printf("exec error!\n");
             exit(0);这其中有一句代码{if(execle("/usr/bin/gcc","gcc","./my.c","-o","./myt.out",(char *)0,env_init)<0)是对my.c进行编译,生成myt.out,我现在想实现批量编译一个指定文件夹下的所有文件,生成的文件也统一存放在指定文件夹中,要怎么改程序,才能实现这以功能呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表