zoukankan      html  css  js  c++  java
  • vs2010连接mysql数据库进行查询修改操作 c++

      本程序是vs2010连接mysql数据库后,并在运行界面进行查询修改等操作~~学习阶段的第一个程序,做我自我鼓励吧!

    vs运行界面:

    mysql运行界面

    #include<Windows.h> 
    #include<string> 
    #include<iostream> 
    #include<mysql.h> 
    #include<stdio.h>
    #include <string>
    using namespace std;
    char *user="root";       //用户名
    char *pass="jiangshan";    //密码
    char *db="jiang";             //所用的数据库
    unsigned int port =3306;      //
    MYSQL mydata;
    MYSQL_RES *res;
    MYSQL_FIELD *fd;
    MYSQL_ROW row;
    string SqlText;
    
    bool show();
    bool insert();
    bool change();
    bool find();
    string findbyid();
    bool findbyname();
    bool changename(string id);
    bool changesex(string id);
    
    
    int main()
    {
        if (0!=mysql_library_init(0,NULL,NULL))
        {
            cout<<"mysql_library_init() failed"<<endl;
            return -1;
        } 
        if (NULL==mysql_init(&mydata))
        {
            cout<<"mysql_init() failed!"<<endl;
            return -1;
        }
        if (NULL==mysql_real_connect(&mydata,"",user,pass,db,port,NULL,0))
        {
            cout<<"mysql_real_connect() failed!"<<endl;
            return -1;
        }
        SqlText="create table if not exists stu";
        SqlText+="(id int(10) primary key auto_increment,name char(30) not null,sex tinyint(1) default 0) default character set utf8 engine myisam;";
        if (0!=mysql_query(&mydata,SqlText.c_str()))
        {
            cout<<"mysql_query() create table failed"<<endl;
            return -1;
        }
        while(1)
        {
            cout<<"**********************"<<endl;
            cout<<" 1   查看"<<endl;
            cout<<" 2   插入"<<endl;
            cout<<" 3   修改"<<endl;
            cout<<" 4   查询"<<endl;
            cout<<" or  退出"<<endl;
            cout<<"**********************"<<endl;
            int i;
            cin>>i;
            switch(i)
            {
            case 1:
                SqlText="select * from stu;";
                show();
                break;
            case 2:
                insert();
                break;
            case 3:
                change();
                break;
            case 4:
                find();
                break;
            default:
                return 0;
            }
        }
        return 0;
    }
    
    //从表中读取所有的数据
    bool show()
    {
        cout<<"***********************************"<<endl;
        if(0!=mysql_query(&mydata,SqlText.c_str()))
        {
            cout<<"mysql_query() select data failed"<<endl;
            return false;
        }
        res=mysql_store_result(&mydata);
        int rowcount = mysql_num_rows(res);
        unsigned int fieldcount = mysql_num_fields(res);
        MYSQL_FIELD *field =NULL;
    
        //打印个字段名称
        for(int i=0;i<fieldcount;i++)
        {
            field = mysql_fetch_field_direct(res,i);
            cout<<field->name<<"		";
        }
        cout<<endl;
        //打印各行
        row=mysql_fetch_row(res);
        while (NULL!=row)
        {
            for (int i=0;i<fieldcount;i++)
            {
                cout<<row[i]<<"		";
            }
            cout<<endl;
            row=mysql_fetch_row(res);
        }
        cout<<"***********************************"<<endl;
        return true;
    }
    
    //插入数据
    bool insert()
    {
        string name;
        cout<<"请输入姓名"<<endl;
        cin>>name;
        string sex;
        cout<<"请输入性别(0:男,1:女)"<<endl;
        cin>>sex;
        
        SqlText="insert into stu(name,sex) values(' ";
        SqlText+=name;
        SqlText+="',";
        SqlText+=sex;
        SqlText+=");";
        
        //SqlText="insert into stu(name,sex)  values('haha',1);";   //这个是范例,字符串必须用 '  ' 串起来
        if(0!=mysql_query(&mydata,SqlText.c_str()))
        {
            cout<<"insert failed!"<<endl;
                return false;
        }
        return true;
    }
    
    //查找
    bool find()
    {
        cout<<"**********************"<<endl;
        cout<<" 通过何种方式查找"<<endl;
        cout<<" 1   id"<<endl;
        cout<<" 2   name"<<endl;
        cout<<"**********************"<<endl;
        int i;
        string a;
        cin>>i;
        switch(i)
        {
        case 1:
            a=findbyid();
            break;
        case 2:
            findbyname();
            break;
        default:
            return false;
        }
        cout<<endl;
        return true;
    }
    
    string findbyid()
    {
        cout<<"输入你的id"<<endl;
        string id;
        cin>>id;
        SqlText="select * from stu where id=";
        SqlText+=id;
        SqlText+=";";
        show();
        return id;
    }
    
    bool findbyname()
    {
        cout<<"输入你的name"<<endl;
        string name;
        cin>>name;
        
        SqlText="select * from stu where name =' ";
        SqlText+=name;
        SqlText+="' ;";
        //SqlText="select * from stu where name ='jiang';";
        show();
        return true;
    }
    
    //修改
    bool change()
    {
        string id=findbyid();
        cout<<"你想修改什么"<<endl;
        cout<<"1   name"<<endl;
        cout<<"2   sex"<<endl;
        int i;
        cin>>i;
        switch(i)
        {
        case 1:
            changename(id);
            break;
        case 2:
            changesex(id);
            break;
        default:
            return false;
        }
        return true;
    }
    
    bool changename(string id)
    {
        cout<<"请输入要替换的名字"<<endl;
        string name;
        cin>>name;
        SqlText="update stu set name='";
        SqlText+=name;
        SqlText+="' where id=";
        SqlText+=id;
        SqlText+=";";
        if(0!=mysql_query(&mydata,SqlText.c_str()))
        {
            cout<<"change name failed!"<<endl;
            return false;
        }
        SqlText="select * from stu where id=";
        SqlText+=id;
        SqlText+=";";
        show();
        return true;
    }
    
    bool changesex(string id)
    {
        cout<<"请输入要替换的性别"<<endl;
        string sex;
        cin>>sex;
        SqlText="update stu set sex=";
        SqlText+=sex;
        SqlText+=" where id=";
        SqlText+=id;
        SqlText+=";";
        if(0!=mysql_query(&mydata,SqlText.c_str()))
        {
            cout<<"change is failed!"<<endl;
            return false;
        }
        SqlText="select * from stu where id=";
        SqlText+=id;
        SqlText+=";";
        show();
    }
  • 相关阅读:
    攻防世界pwn高手区——pwn1
    攻防世界misc——János-the-Ripper
    攻防世界逆向——game
    PWN——ret2dl_resolve
    谈一些想法
    python网络编程(一)
    pwnable.kr之unlink
    堆的数据结构探究
    记一次GKCTF之旅
    socket笔记
  • 原文地址:https://www.cnblogs.com/tuzijs/p/3404989.html
Copyright © 2011-2022 走看看