zoukankan      html  css  js  c++  java
  • 连接mysql数据库

    #include  <windows.h> 

    #include  <iostream> 

    #include  <mysql.h> 

    #include  <stdio.h> 

    using namespace std;

    int main() 

    //connection params 

     FILE *file; 

     char *host = "localhost"; 

     char *user = "root"; 

     char *pass = "123456"; 

     char *db = "mysql"; 

     

     

     

     //sock 

     MYSQL *sock; 

     MYSQL_RES *results; 

     MYSQL_ROW record; 

       

     sock = mysql_init(0); 

     if (sock)

     cout  << "sock handle ok!"  << endl; 

     else 

     {

     cout  << "sock handle failed!" << mysql_error(sock)<< endl; 

     } 

     //connection 

     if (mysql_real_connect(sock, host, user, pass, db, 3306, NULL, 0)) 

     cout  << "connection ok!"  << endl;

     else { 

     cout << "connection fail: " << mysql_error(sock)<< endl; 

     } 

     //connection character set 

     mysql_set_character_set(sock,"gb2312"); 

     cout  << "connection character set: " << mysql_character_set_name(sock) <<                    endl; 

     //wait for posibility to check system/mysql sockets 

     

     if(mysql_query(sock,"select * from user")) 

     { 

     cout <<"very good!" <<endl; 

     } 

     

     //links=mysql_fetch_row(sock);

     

     results=mysql_store_result(sock);

     printf("user\tlevel\n"); 

     while(record=mysql_fetch_row(results)) 

     { 

     printf("%s\t %s\n",record[1],record[3]); 

       

     } 

     

     

     mysql_free_result(results); 

     system("PAUSE"); 

       

     //closing connection 

     mysql_close(sock);

     return EXIT_SUCCESS;

    }

     

  • 相关阅读:
    centos6.5 源码安装 gtk 环境
    世界的复杂性
    将 shell 脚本打包到 rpm 包中
    使用 ipdb 调试 Python
    shell 处理 文件名本身带星号的情况
    如果可以更更完善,为什么不呢?
    比较有名的开源项目
    各种小工具合集
    各种版本对应关系
    dns相关
  • 原文地址:https://www.cnblogs.com/yaowen/p/2846201.html
Copyright © 2011-2022 走看看