vc c mysql

来源:互联网 发布:腾讯社招java面试题 编辑:程序博客网 时间:2024/06/05 12:08

先是对vc++的设置1. project->settings->Link   category选General  添加libmysql.lib 

                                 2.Tools->Options->Directories-include files  添加mysql的include文件路径

                                  3.Tools->Options->Directories-Library files  添加mysql的lib文件路径如C:/PROG  RAM FILES/MYSQL/MYSQL SERVER 5.1/LIB/OPT

将mysql的bin目录下的libmysql.dll拷贝到C:/WINDOWS/system32下。

 

#include <windows.h>
   #include <stdio.h>
   #include <string.h>
   #include <mysql.h>
   //#include "winsock2.h"   
void main()
{
 MYSQL my_connection;
 MYSQL_RES *res_ptr;
 //MYSQL_RES *res;
 MYSQL_ROW sqlrow;
 mysql_init(&my_connection);
 if(mysql_real_connect(&my_connection, "localhost", "root","0","abccs", 0, NULL, 0))
 {
  printf("Connection success/n");
  //res = mysql_query(&my_connection, "select * from first");
        mysql_query(&my_connection, "insert into first values('3','mou','12','f')");//插入操作
  if(mysql_query(&my_connection, "select * from first"))
  {
   printf("select error: %s/n", mysql_error(&my_connection));
  }
  else
  {
   res_ptr = mysql_store_result(&my_connection);
   if(res_ptr)
   {
    printf("Retrieved %lu rows/n", (unsigned long)mysql_num_rows (res_ptr));         
    while (sqlrow = mysql_fetch_row(res_ptr))//每次输出每一行
    {  
     unsigned int field_count;
     field_count =0;
     while(field_count < mysql_field_count(&my_connection))//列
     {
      printf("%s ", sqlrow[field_count]);field_count++;
     }
     printf("/n");//换行
    }
    if(mysql_errno(&my_connection))
    {
     printf("Retrive error : %s/n", mysql_error(&my_connection));
    }
   }
  }
  mysql_free_result(res_ptr);
 }
     mysql_close(&my_connection);
}

原创粉丝点击