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;

    }

     

  • 相关阅读:
    vscode使用SSH Targets连接远程系统进行开发
    python2.7版本安装pip
    ubuntu系统安装最新版本nodejs
    win7安装yarn后执行报错
    Object合并,并使用默认值处理
    解决JSON.stringify序列化循环依赖对象报错
    docker环境下安装maven私服和gitlab
    单例模式的无锁实现
    【Java学习笔记】Java的垃圾回收机制
    Ubuntu下源码安装Python
  • 原文地址:https://www.cnblogs.com/yaowen/p/2846201.html
Copyright © 2011-2022 走看看