MYSQL *mysql_real_connect (MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
MYSQL_RES *mysql_store_result(MYSQL *mysql)
/*************************************************************************
> File Name: student.cpp
> Author: Tanswer_
> Mail: 98duxm@gmail.com
> Created Time: 2017年05月28日 星期日 16时50分34秒
************************************************************************/
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
#include <sstream>
#include <mysql/mysql.h>
#include <unistd.h>
using namespace std;
MYSQL mysql;
MYSQL_ROW row;
MYSQL_FIELD* field = NULL;
MYSQL_RES* result;
string IntToStr(int num)
{
stringstream ss;
ss.clear();
ss << num;
return ss.str();
}
void Add()
{
string fname,fsex,ftel,faddr;
int fage;
char choice;
do
{
┊ cout << "请依次输入以下信息:" << endl;
┊ cout << "\nName: ";cin >> fname;
┊ cout << "\nSex: ";cin >> fsex;
┊ cout << "\nAge: "; cin >> fage;
┊ cout << "\nTel: "; cin >> ftel;
┊ cout << "\nAddr: "; cin >> faddr;
┊ string sql = "INSERT INTO Infor (name,sex,tel,addr,age) values('"+fname+"','"+fsex+"','"+ftel+"','"+faddr+"', "+IntToStr(fage)+");";
┊ //string sql = "INSERT INTO Infor (name,sex,age,tel,addr) values('小红','女',18,'13333333333', '陕西省西安市雁塔区');";
┊ mysql_query(&mysql,sql.c_str());
┊ ┊
┊ cout << "是否继续添加(y/n)?: ";
┊ cin >> choice;
}while(choice == 'y');
}
void Select()
{
int id;
cout << "请输入要查询学生的学号: ";
cin >> id;
string sql = "SELECT * FROM Infor WHERE id = "+IntToStr(id)+";";
mysql_query(&mysql,sql.c_str());
result = mysql_store_result(&mysql);
if(result == NULL)
┊ cout << "fail\n";
for(int i=0; i<mysql_num_fields(result); i++)
{
┊ field = mysql_fetch_field_direct(result,i);
┊ cout << field->name << "\t\t";
}
cout << endl;
row = mysql_fetch_row(result);
while(row != NULL)
{
┊ for(int i=0; i<mysql_num_fields(result); i++)
┊ {
┊ ┊ cout << row[i] << "\t\t";
┊ }
┊ cout << endl;
┊ row = mysql_fetch_row(result);
}
}
void Update()
{
int id;
char choice;
string newaddr;
┊ cout << "请输入要修改同学的学号: ";
┊ cin >> id;
┊ cout << endl << "请输入修改后的地址: ";
┊ cin >> newaddr;
┊ string sql = "UPDATE Infor SET addr = '"+newaddr+"'WHERE id= "+IntToStr(id)+"; ";
┊ mysql_query(&mysql,sql.c_str());
┊
}
int main()
{
char choice[5];
mysql_init(&mysql);
/*连接数据库*/
if(!mysql_real_connect(&mysql,"localhost","root","dxm242012","Student",0,NULL,0))
{
┊ cout << "connect fial\n";
┊ return -1;
}
while(atoi(choice) != 'q')
{
┊ sleep(4);
┊ system("clear");
┊ cout << "1.添加学生信息" << endl;
┊ cout << "2.查询学生信息" << endl;
┊ cout << "3.修改学生信息" << endl;
┊ cin >> choice;
┊ cout << choice << endl;
┊ switch(atoi(choice))
┊ {
┊ ┊ case 1:
┊ ┊ ┊ Add();
┊ ┊ ┊ break;
┊ ┊ case 2:
┊ ┊ ┊ Select();
┊ ┊ ┊ break;
┊ ┊ case 3:
┊ ┊ ┊ Update();
┊ ┊ ┊ break;
┊ ┊ default:
┊ ┊ ┊ break;
┊ }
}
mysql_close(&mysql);
return 0;
}
/*************************************************************************
> File Name: myDB.h
> Author: Tanswer_
> Mail: 98duxm@gmail.com
> Created Time: 2017年05月28日 星期日 22时26分22秒
************************************************************************/
#ifndef _MYDB_H
#define _MYDB_H
#include <string>
#include <iostream>
#include <mysql/mysql.h>
using namespace std;
class MyDB
{
public:
MyDB();
~MyDB();
bool InitDB(string host,string user,string pwd,string dbname);
bool ExeSQL(string sql);
private:
MYSQL* mysql;
MYSQL* mysql;
MYSQL_ROW row;
MYSQL_RES* result;
MYSQL_FIELD* field;
};
#endif
/*************************************************************************
> File Name: myDB.cpp
> Author: Tanswer_
> Mail: 98duxm@gmail.com
> Created Time: 2017年05月28日 星期日 22时27分18秒
************************************************************************/
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
#include <mysql/mysql.h>
#include "myDB.h"
using namespace std;
MyDB::MyDB()
{
mysql = mysql_init(NULL);
if(mysql == NULL)
{
┊ cout << "Error: " << mysql_error(mysql);
┊ exit(-1);
}
}
MyDB::~MyDB()
{
if(!mysql)
{
┊ mysql_close(mysql);
}
}
bool MyDB::InitDB(string host,string user,string pwd,string dbname)
{
/*连接数据库*/
if(!mysql_real_connect(mysql,host.c_str(),user.c_str(),pwd.c_str(),dbname.c_str(),0,NULL,0))
{
┊ cout << "connect fial: " << mysql_error(mysql);
┊ exit(-1);
}
return true;
}
bool MyDB::ExeSQL(string sql)
{
/*执行失败*/
if(mysql_query(mysql,sql.c_str()))
{
┊ cout << "query fail: " << mysql_error(mysql);
┊ exit(1);
}
else
{
┊ /*获取结果集*/
┊ result = mysql_store_result(mysql);
┊ int fieldnum = mysql_num_fields(result);
┊ for(int i=0; i<fieldnum; i++)
┊ {
┊ ┊ row = mysql_fetch_row(result);
┊ ┊ if(row <= 0)
┊ ┊ ┊ break;
┊ ┊ for(int j=0; j<fieldnum; j++)
┊ ┊ {
┊ ┊ ┊ cout << row[j] << "\t\t";
┊ ┊ }
┊ ┊ cout << endl;
┊ }
┊ mysql_free_result(result);
}
return true;
}
/*************************************************************************
> File Name: main.cpp
> Author: Tanswer_
> Mail: 98duxm@gmail.com
> Created Time: 2017年05月28日 星期日 22时53分43秒
************************************************************************/
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
#include <mysql/mysql.h>
#include "myDB.h"
using namespace std;
int main()
{
MyDB db;
db.InitDB("localhost","root","xxxxxx","Student");
db.ExeSQL("SELECT * FROM Infor;");
return 0;
}
#include<mysql/mysql.h> ^
string IntToStr(int num)
{
stringstream ss;
ss.clear();
ss << num;
return ss.str();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有