Home > Database > Mysql Tutorial > body text

linux中c/c++连接mysql示例程序_MySQL

WBOY
Release: 2016-06-01 13:06:04
Original
1546 people have browsed it

#include
#include
#include
int main()
{
   MYSQL mysql;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *query;
   int t;
   mysql_init(&mysql);
/*connection*/
    if(!mysql_real_connect(&mysql,"localhost","root","","test",0,NULL,0)){
          printf("Connect failed/n");  
    }else{
          printf("Connect successful/n");
    }
   


/*select*/
  query="select * from t1;";
    if(mysql_real_query(&mysql,query,(unsigned int)strlen(query))==0){
          printf("query successful/n");
    }else{
          printf("query failed/n");
    }
  res=mysql_store_result(&mysql);
  while(row=mysql_fetch_row(res)){
    for(t=0;t    {
       printf("%s/t",row[t]);
    }
  }
/*create table*/
/*  query="create table t2(name char(15),age int);";
    if(mysql_real_query(&mysql,query,(unsigned int)strlen(query))==0){
          printf("create table successful/n");
    }else{
          printf("create table failed/n");
    }
*/ 
/*insert*/
/*  query="insert into t1(name,age) values ('zhang',32);";
    if(mysql_real_query(&mysql,query,(unsigned int)strlen(query))==0){
          printf("Insert successful/n");
    }else{
          printf("Insert failed/n");
    }
*/   
/*delete*/
/*  query="delete from t1 where age=20;";
    if(mysql_real_query(&mysql,query,(unsigned int)strlen(query))==0){
          printf("Delete successful/n");
    }else{
          printf("Delete failed/n");
    }
 */   
/*update*/
/*  query="update t1 set age=100 where name='wa';";
    if(mysql_real_query(&mysql,query,(unsigned int)strlen(query))==0){
          printf("Update successful/n");
    }else{
          printf("Update failed/n");
    }
*/ 
 mysql_close(&mysql);
    return 0;

}


安装
mysql-server-5.1.73-3.el6_5.x86_64

mysql-5.1.73-3.el6_5.x86_64

mysql-connector-odbc-5.1.5r1144-7.el6.x86_64

mysql-devel-5.1.73-3.el6_5.x86_64

mysql-libs-5.1.73-3.el6_5.x86_64

编译
gcc -o sqlconnect sqlconnect.c -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template