LinuxSir.cn,穿越时空的Linuxsir!

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

谁给一个C+mysql的例子

[复制链接]
发表于 2003-3-15 10:59:14 | 显示全部楼层 |阅读模式
最近在学C下的数据库编程,谁能给一个例子呀,先谢谢
发表于 2003-3-15 13:58:26 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<mysql/mysql.h>

int main ()
{
/********变量声明部分************/
    MYSQL            *mysql;
    MYSQL_RES    *result;
    MYSQL_ROW    row;
    unsigned int num_fields;
    int i;
/*********mysql连接部分**********/
       mysql=mysql_init(mysql);
   if (!mysql_real_connect(mysql,"locahost","www","www",NULL,0,NULL,0))
{
  
  fprint(stdrr,"Failed to connect to     database:Error:%s\n",mysql_error($mysql));
   return -1;
}

/*******选择数据库**********/
  if (mysql_select_db(mysql,"test"))
{
   printf("select db wrong:%s,mysql_error(mysql));
   return -1;
}

/*******查询并处理*********/
mysql_query(mysql,"select * from test");
result=mysql_stroe_resutl(mysql);
if (result==NULL)
{  printf ("Error:%s\n",msyql_error(mysql));
   return -1;
}
num_fileds=mysql_num_filelds(result);
while((row=mysql_fetch_row(result)))
  {
     unsigned long *lengths;
     printf("#######ROW#########\n");
    lengths=mysql_fetch_lengths(result);
    for (i=0;i<num_fileds;i++)
    {
      printf ("[%.*s]\n",(int) lengths,row?row:"NULL");
    }
}


/**********释放内存,关闭连接**************/
msyql_free_result(result);
mysql_close(mysql);
}
 楼主| 发表于 2003-3-15 22:08:43 | 显示全部楼层
先谢谢,再提个问题,我编译时为什么出错呀,我该怎么做??
[yni@localhost c]$ gcc a.c -lmysqlclient
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status
 楼主| 发表于 2003-3-15 22:31:26 | 显示全部楼层

还有错

[yni@localhost c]$ gcc a.c
/tmp/ccoouGvf.o: In function `main':
/tmp/ccoouGvf.o(.text+0x17): undefined reference to `mysql_init'
/tmp/ccoouGvf.o(.text+0x3c): undefined reference to `mysql_real_connect'
/tmp/ccoouGvf.o(.text+0x6f): undefined reference to `mysql_select_db'
/tmp/ccoouGvf.o(.text+0x84): undefined reference to `mysql_error'
/tmp/ccoouGvf.o(.text+0xb1): undefined reference to `mysql_query'
/tmp/ccoouGvf.o(.text+0xc8): undefined reference to `msyql_error'
/tmp/ccoouGvf.o(.text+0xee): undefined reference to `mysql_fetch_row'
/tmp/ccoouGvf.o(.text+0x117): undefined reference to `mysql_fetch_lengths'
/tmp/ccoouGvf.o(.text+0x12a): undefined reference to `msyql_free_result'
/tmp/ccoouGvf.o(.text+0x138): undefined reference to `mysql_close'
collect2: ld returned 1 exit status
发表于 2003-3-18 12:53:11 | 显示全部楼层
#include(mysql/msyql.h)
请确定你的mysql.h头文件的位置。
因为你的mysql.h头文件并没有包含进来!
发表于 2003-3-18 12:57:29 | 显示全部楼层
你没有lmysqlclient文件,所以你的C不能调用与mysql的接口函数!
发表于 2003-3-20 13:44:11 | 显示全部楼层
gcc a.c -lm 试一试!
发表于 2003-4-1 09:54:45 | 显示全部楼层
以下是什么问题?
# gcc test.c
test.c: In function `main':
test.c:18: `stdrr' undeclared (first use in this function)
test.c:18: (Each undeclared identifier is reported only once
test.c:18: for each function it appears in.)
test.c:18: `$mysql' undeclared (first use in this function)
test.c:25:8: warning: multi-line string literals are deprecated
test.c:25: parse error before "select"
test.c:30:38: warning: multi-line string literals are deprecated
test.c:25: stray '\' in program
test.c:33:22: warning: multi-line string literals are deprecated
test.c:25: syntax error at '##' token
test.c:25: syntax error at '##' token
test.c:25: syntax error at '##' token
test.c:25: syntax error at '#' token
test.c:25: syntax error at '##' token
test.c:25: syntax error at '##' token
test.c:25: syntax error at '##' token
test.c:25: syntax error at '##' token
test.c:25: syntax error at '#' token
test.c:25: stray '\' in program
test.c:40:30: warning: multi-line string literals are deprecated
test.c:25: stray '\' in program
test.c:44:56: warning: multi-line string literals are deprecated
test.c:44:56: missing terminating " character
test.c:25:8: possible start of unterminated string literal
发表于 2003-4-18 19:48:01 | 显示全部楼层
老兄,笔误太多,误人不浅啊
发表于 2003-5-1 22:25:12 | 显示全部楼层
看这个怎样:

#include "mysql.h"
#include <stdio.h>

int main(int argc,char *argv[])
{
char *user = "root", *pwd = "mysql", *dbname = "mysql";
MYSQL mysql;
MYSQL_RES *mysql_ret;
MYSQL_ROW mysql_row;
unsigned long num_rows;
int ret;

mysql_init(&mysql);

if(mysql_real_connect(&mysql,NULL,user,pwd,dbname,0,NULL,0))
{
printf("Connection success!\n");
ret = mysql_query(&mysql,"select * from user");
if(!ret)
{
printf("Query Success!\n");
mysql_ret = mysql_store_result(&mysql);
if(mysql_ret != NULL)
{
printf("Store Result Success!\n");
num_rows = mysql_num_rows(mysql_ret);
if(num_rows != 0)
{
printf("%d\n",num_rows);
while(mysql_row = mysql_fetch_row(mysql_ret))
{
printf("%s\t%s\t%s\t%s\t%s\t%s\n",mysql_row[0],mysql_row[1],mysql_row[2],mysql_row[3],mysql_row[4],mysql_row[5]);
}
}
else
{
printf("mysql_num_rows Failed!\n");
exit(-1);
}
mysql_free_result(mysql_ret);
exit(0);
}
else
{
printf("Store Result Failed!\n");
exit(-1);
}
}
else
{
printf("Query Failed!\n");
exit(-1);
}
}
else
{
printf("Connection Failed\n");
exit(-1);
}
}

/*
gcc -I /usr/local/mysql/include mysql.c -L /usr/local/mysql/lib -lmysqlclient -lz -o $(HOME)/bin/mysql
*/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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