C++用mysql自帶的頭文件連接數(shù)據(jù)庫(kù)
更新時(shí)間:2016年07月17日 22:13:32 投稿:hebedich
現(xiàn)在正做一個(gè)接口,通過不同的連接字符串操作不同的數(shù)據(jù)庫(kù)。要用到mysql數(shù)據(jù)庫(kù)。通過網(wǎng)上的一些資料和自己的摸索,大致清楚了C++連接mysql的方法??梢酝ㄟ^2種方法實(shí)現(xiàn)。第一種方法是利用ADO連接,第二種方法是利用mysql自己的api函數(shù)進(jìn)行連接。今天主要來講解下使用API
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<<"請(qǐng)輸入該用戶的編號(hào)"<<endl;
cin>>pid;
cout<<"請(qǐng)輸入該用戶的名字"<<endl;
// gets(name);
cin>>name;
cout<<"請(qǐng)輸入該用戶的類型"<<endl;
cin>>type;
cout<<"請(qǐng)輸入該用戶的聯(lián)系方式"<<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<<"請(qǐng)輸入你要更改的用戶的編號(hào):";
cin >> a.pid;
cout<<"請(qǐng)輸入你要此編號(hào)用戶的姓名:";
cin >> a.name;
cout <<"請(qǐng)輸入你要更改的用戶的類型:";
cin >> a.type;
cout << "請(qǐng)輸入你要更改的用戶的電話:";
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<<"請(qǐng)輸入你要?jiǎng)h除的人的編號(hào)"<<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("編號(hào)\t名字\t類型\t聯(lián)系方式\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<<"按任意鍵繼續(xù).."<<endl;
getchar();
}
return 0;
}
相關(guān)文章
淺談c++性能測(cè)試工具google benchmark
本文將會(huì)介紹如何使用模板以及參數(shù)生成器來批量生成測(cè)試用例,簡(jiǎn)化繁瑣的性能測(cè)試代碼2021-06-06
C++實(shí)現(xiàn)LeetCode(146.近最少使用頁面置換緩存器)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(146.近最少使用頁面置換緩存器),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++vector的insert函數(shù)用法小結(jié)
std::vector::insert是C++中用于在指定位置插入元素的函數(shù),支持插入單個(gè)元素、多個(gè)相同元素、一個(gè)范圍的元素或初始化列表中的元素,插入操作可能會(huì)使插入點(diǎn)之后的迭代器失效,并且時(shí)間復(fù)雜度為O(n),本文介紹C++vector的insert函數(shù)用法小結(jié),感興趣的朋友一起看看吧2025-03-03
C語言MultiByteToWideChar和WideCharToMultiByte案例詳解
這篇文章主要介紹了C語言MultiByteToWideChar和WideCharToMultiByte案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
QT網(wǎng)絡(luò)通信TCP客戶端實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了QT網(wǎng)絡(luò)通信TCP客戶端實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
QT編寫簡(jiǎn)單登錄界面的實(shí)現(xiàn)示例
登陸界面是網(wǎng)頁中常見的界面,本文主要介紹了QT編寫簡(jiǎn)單登錄界面的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02

