C++用mysql內建的標頭檔串連資料庫_C 語言

來源:互聯網
上載者:User

mysql.h檔案在哪,怎麼尋找。自行百度

#include <mysql/mysql.h>#include <stdio.h>#include<iostream>#include<fstream>#include<string.h>using namespace std;MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; class people{public:  char name[20];  int pid;  int type;  char phone[30];public:  people(int a){};  people(){    setall();  };  ~people(){};public:  void setall(); }; void people::setall(){  cout<<"請輸入該使用者的編號"<<endl;  cin>>pid;  cout<<"請輸入該使用者的名字"<<endl;// gets(name);  cin>>name;  cout<<"請輸入該使用者的類型"<<endl;  cin>>type;  cout<<"請輸入該使用者的連絡方式"<<endl;  cin>>phone; }void save(){  char sql[1000];  people a;  sprintf(sql,"insert into student values(%d,'%s',%d,'%s')",a.pid,a.name,a.type,a.phone);  if(mysql_query(conn, sql))    {      printf("添加失敗: (%s)\n",mysql_error(conn));      return;    }    else    {      printf("添加成功!\n");      return;    }  return;}void update(){  char sql[1000];  people a(1);  cout<<"請輸入你要更改的使用者的編號:";  cin >> a.pid;  cout<<"請輸入你要此編號使用者的姓名:";  cin >> a.name;  cout <<"請輸入你要更改的使用者的類型:";  cin >> a.type;  cout << "請輸入你要更改的使用者的電話:";  cin >> a.phone;  sprintf(sql,"update student set name = '%s',usetype=%d,phone='%s' where pid = %d",a.name,a.type,a.phone,a.pid);    if(mysql_query(conn, sql))      {        printf("更改失敗: (%s)\n",mysql_error(conn));        return;      }      else      {        printf("更改成功!\n");        return;      }    return;}void del(){  char sql[1000];  int pid;  cout<<"請輸入你要刪除的人的編號"<<endl;  cin>>pid;  sprintf(sql,"delete from student where pid = %d",pid);  if(mysql_query(conn, sql))    {      printf("刪除 失敗(%s)\n",mysql_error(conn));      return;    }    else    {      printf("刪除成功!\n");      return;    }  return;}void menu(){  cout<<"1.使用者錄入"<<endl;  cout<<"2.顯示"<<endl;  cout<<"3.更改"<<endl;  cout<<"4.刪除"<<endl;  cout<<"5.退出"<<endl;}void show(){   if (mysql_query(conn, "select * from student")) {     fprintf(stderr, "%s\n", mysql_error(conn));     return;    }    res = mysql_use_result(conn);     printf("編號\t名字\t類型\t連絡方式\n");    while ((row = mysql_fetch_row(res)) != NULL){     cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<endl;    }    mysql_free_result(res); }int main() {  int s;  conn = mysql_init(NULL);  if (!mysql_real_connect(conn, "localhost",     "root", "root", "abc", 0, NULL, 0)) {   fprintf(stderr, "%s\n", mysql_error(conn));   return -1;  }  mysql_query(conn,"set names utf8");  while(true){    menu();    cin>>s;    if(s==2){show();}    if(s==1){save();}    if(s==3){update();}    if(s==4){del();}    if(s==5){mysql_close(conn);return 0;}    cout<<"按任意鍵繼續.."<<endl;    getchar();  }  return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.