linux c++ mysql_linux下C++访问mysql数据库(2)

在这个程序中,比上一次的要完善一些,实现了通过C++对数据表进行添加,修改,插入,删除的功能。

头文件——myDB.h:

#ifndef myDB_class

#define myDB_class

#include 

#include 

#include 

class myDB

{

public:

myDB();

~myDB();

int initDB(std::string host, std::string user, std::string password, std::string db_name);

void Run();

private:

int exeSQL();

int insertSQL();

int deleteSQL();

int updateSQL();

MYSQL *connection;

MYSQL_RES *result;

MYSQL_ROW row;

};

#endif

myDB.cpp

#include "myDB.h"

#include 

#include 

using namespace std;

myDB::myDB()

{

connection = mysql_init(NULL); // 初始化数据库的连接变量

if(connection == NULL)

{

std::cout <

exit(1);// exit(1)表示发生错误后退出程序, exit(0)表示正常退出

}

}

myDB::~myDB()

{

if(connection != NULL)

{

mysql_close(connection); // 关闭数据库连接

}

}

int myDB::initDB(std::string host, std::string user, std::string password, std::string db_name)

{

char value = 1;

mysql_options(connection_,MYSQL_OPT_RECONNECT,(char*)&value);//mysql自动重连

connection = mysql_real_connect(connection, host.c_str(), user.c_str(), password.c_str(), db_name.c_str(), 0, NULL, 0); // 建立数据库连接

if(connection == NULL)

{

std::cout <

exit(1);// exit(1)表示发生错误后退出程序, exit(0)表示正常退出

}

return 0;

}

int myDB::exeSQL()

{

// mysql_query()执行成功返回0,失败返回非0值。与PHP中不一样

std::string sql;

sql = "select * from user;";

if(mysql_query(connection, sql.c_str()))

{

std::cout <

exit(1);

}

else

{

result = mysql_use_result(connection); // 获取结果集

// mysql_field_count()返回connection查询的列数

for(int i=0; i 

{

// 获取下一行

row = mysql_fetch_row(result);

if(row <= 0)

{

break;

}

// mysql_num_fields()返回结果集中的字段数

for(int j=0; j 

{

std::cout <

}

std::cout <

}

std::cout <

// 释放结果集的内存

mysql_free_result(result);

}

return 0;

}

int myDB::insertSQL()

{

std::string username;

std::string psd;

std::string level;

std::cout <

std::cin >> username >> psd >> level;

string sql = "insert into user(id,username,password,level) values ( NULL,'" + username +"','"+ psd  +"', "+ level +");";

if(mysql_query(connection, sql.c_str()))

{

std::cout <

exit(1);

}

std::cout <

exeSQL();

return 0;

}

int myDB::deleteSQL()

{

std::string id;

std::cout <

std::cin >> id;

std::string sql;

sql = "delete from user where id = " + id + ";";

if(mysql_query(connection, sql.c_str()))

{

std::cout <

exit(1);

}

std::cout <

exeSQL();

return 0;

}

int myDB::updateSQL()

{

std::string id;

std::string thing;

std::cout <

std::cin >> id >> thing;

std::string sql;

sql = "update user set " + thing + "where id =" + id + ";";

if(mysql_query(connection, sql.c_str()))

{

std::cout <

exit(1);

}

std::cout <

exeSQL();

return 0;

}

void myDB::Run()

{

int i = 1;

while(i != 0)

{

int sel;

std::cout <

std::cin >> sel;

switch(sel)

{

case 1:exeSQL();

break;

case 2:insertSQL();

break;

case 3:deleteSQL();

break;

case 4:updateSQL();

break;

case 5:i = 0;

break;

default:std::cout <

break;

}

}

}

main.cpp

#include 

#include "myDB.h"

int main()

{

myDB db;

db.initDB("localhost", "root", "wei123", "wei");

db.Run();

return 0;

}

所有代码就都在这里了,下面是makefile文件

Makefile

mydb:main.cpp myDB.cpp

g++ -o mydb main.cpp myDB.cpp -lmysqlclient

clean:

rm -f *.o mydb

(g++ 和rm之前用Tab隔开)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值