linux下使用c++语言对mysql数据库操作
来源:互联网 发布:淘宝长袖套裙小西装 编辑:程序博客网 时间:2024/04/29 10:46
#include "DBAccess.h"
#include <iostream>
//using namespace std;
DBAccess::DBAccess()
{
}
DBAccess::~DBAccess()
{
}
bool DBAccess::MysqlConnect()
{
mysql_init(&mysql);
if(mysql_real_connect(&mysql,"localhost","root","123456","DBTroFish",3306,NULL,0))//参数:主机localhost,用户名root,密码root,数据库DBTroFish
{
return true;
}
else
{
return false;
}
}
bool DBAccess::ExecuteSQL(std::string sql)//对数据的增删改操作
{
if(!MysqlConnect())
{
cout<<"连接失败!"<<mysql_error(&mysql)<<endl;//做测试用的
mysql_close(&mysql);//有效解决too many connection
return false;
//exit(1);//整个程序退出
}
if(mysql_query(&mysql,sql.c_str()))//返回值:0表示正常,非0表示发生了错误
{
cout<<"此操作有误!"<<mysql_error(&mysql)<<endl;
mysql_close(&mysql);
return false;
}
if(mysql_affected_rows(&mysql)>0)//返回值:>0表示影响的行数,=0表示无结果,=-1表示错误
{
mysql_close(&mysql);
return true;
}
else
{
mysql_close(&mysql);
return false;
}
}
MYSQL_RES* DBAccess::GetDataSet(std::string sql)//多行数据查询操作
{
MysqlConnect();
if(mysql_query(&mysql,sql.c_str()))
{
cout<<"此查询有误! 错误号为:"<<mysql_errno(&mysql)<<" 原因:"<<mysql_error(&mysql)<<endl;
}
result=mysql_store_result(&mysql);
mysql_close(&mysql);
//mysql_free_result(result);//要在使用后才调用此方法,不然内存会不断增长直至程序结束
return result;
}
MYSQL_ROW DBAccess::GetRow(std::string sql)//单行数据查询操作
{
result=GetDataSet(sql);
if(mysql_num_rows(result))
{
row=mysql_fetch_row(result);
mysql_free_result(result);//解决内存不断增长问题
return row;
}
else
{
return NULL;
}
}
string DBAccess::IntToString(int a)
{
char t[20];
sprintf(t,"%d",a);
return t;
}
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <mysql/mysql.h>
#include "DBAccess.h"
#include <stdlib.h>
using namespace std;
int main()
{
if (1)
{
DBAccess myDBAccess;
string sql;
sql
= "insert into tblCoinDetail(CoinTime,Bet,BeginCredit,dEndCredit,GiftFlag)values('2011-02-42 1:1:1','1',5041,115,1)";
if (myDBAccess.ExecuteSQL(sql))
cout << "操作成功" << endl;
else
cout << "操作失败" << endl;
}
else
{
DBAccess myDBAccess;
MYSQL_ROW row;
string
sql =
"select date(CoinTime),sum(BeginCredit),sum(EndCredit),sum(BeginCredit-EndCredit) from tblCoinDetail group by CoinTime";
MYSQL_RES *result;
result = myDBAccess.GetDataSet(sql);
if(result!=NULL)// if (mysql_num_rows(result))//判断是否为空
{
while ((row = mysql_fetch_row(result)))
{
if(row[0]==NULL)//解决空地址问题
{
row[0]=(char*)"2011-01-01";
}
if(row[2]==NULL)
{
row[2]=(char*)"0";
}
fprintf(stdout, "%s|/t%s|/t%s|/t%s/n", row[0], row[1],row[2], row[3]);
}
} }
}
- linux下使用c++语言对mysql数据库操作
- linux下c语言操作mysql数据库!
- linux下C语言操作MySQL数据库
- linux下c语言操作mysql数据库
- C语言下用MySQL对数据库操作
- Linux下C语言中对MySQL数据库的练习
- Linux下C对MYSQL数据库的操作
- Linux下C/C++对MySQL数据库操作
- linux下c语言进行mysql数据库操作---笔记
- Linux下通过C语言操作MySQL数据库
- linux下C语言编程操作MySQL数据库
- linux下C语言编程操作MySQL数据库
- linux下C语言编程操作MySQL数据库
- C语言对mysql数据库的操作
- C语言对mysql数据库的操作
- Linux下C语言Mysql数据库使用范例
- Linux下C语言Mysql数据库使用范例
- Linux下C语言Mysql数据库使用范例
- 个人学习杂谈
- 27款经典的CSS框架
- 转帖 做验证码识别 用的到 WebBrowser抓图示例——截取网页上的验证码图片
- R400 A73安装windows XP 全过程
- 安装和配置魔灯(Moodle)
- linux下使用c++语言对mysql数据库操作
- MyEclipse8.5插件FindBugs的安装
- fur render(毛发渲染)
- 计算机比人类,更容易交往
- 根据表中记录的变化情况自动维护作业
- parent是当前的意思
- Spring中JavaBean简单的预处理与后处理
- chrome 功能体验
- 今天就开始 unix高级编程 的学习