c语言读取mysql库中的数据的程序头文件怎么设置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言读取mysql库中的数据的程序头文件怎么设置相关的知识,希望对你有一定的参考价值。

  mysql C API编程步骤
  1、首先我们要包含mysql的头文件,并链接mysql动态库。即添加以下语句:
  #include <WinSock2.h> // 进行网络编程需要winsock2.h
  #include <mysql.h>
  #pragma comment(lib, “libmysql.lib”)
  2、创建MYSQL变量。如:
  MYSQL mysql;
  3、初始化MYSQL变量。
  mysql_init(&mysql);
  4、调用mysql_real_connect函数连接Mysql数据库。mysql_real_connect函数的原型如下:
  MYSQL * STDCALL mysql_real_connect(MYSQL *mysql, const char *host,const char *user,const char *passwd,const char *db,unsigned int port,const char *unix_socket,unsigned long clientflag);
  参数说明:mysql–前面定义的MYSQL变量;host–MYSQL服务器的地址;user–登录用户名;passwd–登录密码;db–要连接的数据库;port–MYSQL服务器的TCP服务端口;unix_socket–unix连接方式,为NULL时表示不使用socket或管道机制;clientflag–Mysql运行为ODBC数据库的标记,一般取0。连接失败时该函数返回0。
  5、调用mysql_real_query函数进行数据库查询。mysql_real_query函数的原型如下:
  int STDCALL mysql_real_query(MYSQL *mysql, const char *q, unsigned long length);
  参数说明:mysql–前面定义的MYSQL变量;q–SQL查询语句;length–查询语句的长度。
  查询成功则该函数返回0。
  6、通过调用mysql_store_result或mysql_use_result函数返回的MYSQL_RES变量获取查询结果数据。
  两个函数的原型分别为:
  MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql);
  MYSQL_RES * STDCALL mysql_use_result(MYSQL *mysql);
  这两个函数分别代表了获取查询结果的两种方式。第一种,调用mysql_store_result函数将从Mysql服务器查询的所有数据都存储到客户端,然后读取;第二种,调用mysql_use_result初始化检索,以便于后面一行一行的读取结果集,而它本身并没有从服务器读取任何数据,这种方式较之第一种速度更快且所需内存更少,但它会绑定服务器,阻止其他线程更新任何表,而且必须重复执行mysql_fetch_row读取数据,直至返回NULL,否则未读取的行会在下一次查询时作为结果的一部分返回,故经常我们使用mysql_store_result。
  7、调用mysql_fetch_row函数读取结果集数据。
  上述两种方式最后都是重复调用mysql_fetch_row函数读取数据。mysql_fetch_row函数的原型如下:
  MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);
  参数result就是mysql_store_result或mysql_use_result的返回值。
  该函数返回MYSQL_ROW型的变量,即字符串数组,假设为row,则row〔i〕为第i个字段的值。当到结果集尾部时,此函数返回NULL。
  8、结果集用完后,调用mysql_free_result函数释放结果集,以防内存泄露。mysql_free_result函数的原型如下:
  void STDCALL mysql_free_result(MYSQL_RES *result);
  9、不再查询Mysql数据库时,调用mysql_close函数关闭数据库连接。mysql_close函数的原型为:
  void STDCALL mysql_close(MYSQL *sock);
参考技术A mysql也是c\c++的,,,,一开始就是c、c++使用的,官方有文档的
~~~~~
安装的mysql里面有头文件.h、和库文件.lib
参考技术B 1、添加头文件路径(MySQL安装路径中的include路径)
2、添加库文件(直接从MySQL安装路径中copy libmysql.lib即可)
3、编程操作数据库
代码

// AccessToMySQL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <mysql.h>
#pragma comment(lib,"libmysql.lib")

MYSQL mysql;
MYSQL_RES* result;
MYSQL_ROW row;

int main(void)

//init the mysql parameter
mysql_init(&mysql);
//connect the database
if(!mysql_real_connect(&mysql,"127.0.0.1","root","111","mytest",3306,NULL,0))

printf(mysql_error(&mysql));
printf("\nCannot access to the database!!!\n");
system("pause");
exit(-1);


//construct the query SQL statements
char* sql="select * from student where name='";
char dest[100]="";
strcat(dest,sql);
printf("Please enter the student name:");
char name[10]="";
gets(name);
strcat(dest,name);
strcat(dest,"'");

//excute the SQL statements
if(mysql_query(&mysql,dest))

printf("Cannot access the database with excuting \"%s\".",dest);
system("pause");
exit(-1);


//deal with the result
result=mysql_store_result(&mysql);
if(mysql_num_rows(result))

while((row=mysql_fetch_row(result)))

printf("%s\t%s\t%s\n",row[0],row[1],row[2]);


//release the resource
mysql_free_result(result);
mysql_close(&mysql);

system("pause");
return 0;

c语言读取文件名问题

有一个文本文件g_name.out,里面存放的是其他数据文件的文件名(如图),每行只有一个文件名,行数不定,如何读取这个g_name.out文件,并根据里面的文件名读取相应的数据文件?谢谢!
g_name.out(存放数据文件名)下

data1_002.txt内容:
请帮忙给出相应的程序语句

  用C语言读取目录中的文件名的方法:

  1、如果是在window环境下,可以用一下方法:

  使用stdlib.h头文件声明的system()函数

  _CRTIMP int __cdecl system (const char*);

  system("dir c:\\\\ /a:h /b > c:\\\\dir.txt");

  调用系统命令dir,把c:目录下文件列表写入文件dir.txt中

  2、使用dirent.h头文件中声明的opendir(),readdir()函数;

int main(int argc, char *argv[])
 
  DIR *directory_pointer; 
    struct dirent *entry; 
   
    if((directory_pointer=opendir("d:\\\\XL"))==NULL) 
        printf( "Error opening \\n "); 
    else 
     
        while((entry=readdir(directory_pointer))!=NULL) 
         
          printf("%s\\n",entry-> d_name);
         
        closedir(directory_pointer); 
       
    

  system("PAUSE");
 return 0;

  3、如果没有dirent.h,可以使用io.h头文件中声明的_findfirst(),_findnext()函数;

  示例代码:

int main(int argc, char *argv[])

  long file;
    struct _finddata_t find;
    
     _chdir("d:\\\\");
     if((file=_findfirst("*.*", &find))==-1L)
  
      printf("空白!\\n");
     exit(0);
     
     printf("%s\\n", find.name);
     while(_findnext(file, &find)==0)
     
          printf("%s\\n", find.name);
     
     _findclose(file);
  
  system("PAUSE");
     return 0;
参考技术A C语言中有两个格式化读写文件的函数,先将文件g_name.out中的内容格式化读取到链表或者数组中,然后遍历链表或者数组,依次取得每个数据文件名,再根据读取到的文件名读取数据文件。 参考技术B #include <stdio.h>
void main()

FILE *fp1,*fp2;
char name[100];
fp1=fopen("g_name.out","r");
while(!feof(fp1))

fgets(fp1,name);
fp2=fopen(name,"r");
以下再读取fp2中的内容。
由于没有编译器,没办法调试,你自己可以根据这个思路写下去。

本回答被提问者采纳

以上是关于c语言读取mysql库中的数据的程序头文件怎么设置的主要内容,如果未能解决你的问题,请参考以下文章

c语言逐行读取文件?

C语言含有包含自定义头文件的程序怎么编译?

c语言读取文件名问题

C 语言动态库封装与设计 ( 动态库调用环境搭建 | 创建应用 | 拷贝动态库相关文件到源码路径 | 导入头文件 | 配置动态库引用 | 调用动态库中的函数 )

用C语言读取xml文件,怎么实现?

读bmp图片:头文件为66字节。。。怎么用c语言来读取数据啊,网上的代码我看不懂。新手,希望能写明白些