//#include "stdafx.h"
#include <iostream>
#include "VspdCTOMySQL.h"
using namespace std;
int main(int argc, char* argv[])
{
char* host = "localhost";
char* user = "root";
char* port = "3306";
char* passwd = "123456";
char* dbname = "database2";
char* charset = "utf8";
char* Msg = "";//消息变量
//初始化
VspdCToMySQL * vspdctomysql = new VspdCToMySQL;
if (vspdctomysql->ConnMySQL(host, port, dbname, user, passwd, charset, Msg) == 0)
cout << "连接成功" << endl;
else
cout<<"连接失败" << endl;
//查询
char * SQL = "select * from table2";
string str = vspdctomysql->SelectData(SQL, 3, Msg);//检索到的字符串
if (str.length() > 0)
{
cout << "查询成功" << endl;
cout<<str.data()<<endl;
/* cout << str.data() << endl;*/
/*cout << str.c_str() << endl;*/
}
else
{
cout<<"查询失败"<<endl;
}
//插入
//SQL = "insert into vcaccesstest(ids,username,passwd,address) values(4,'我的','123210','测试地址')";
//if (vspdctomysql->InsertData(SQL, Msg) == 0)
// printf("插入成功/r/n");
////更新
//SQL = "update vcaccesstest set username = '修改了',passwd='2345' where ids = 3 ";
//if (vspdctomysql->UpdateData(SQL, Msg) == 0)
// printf("更新成功/r/n");
////删除
//SQL = "delete from vcaccesstest where ids = 3 ";
//if (vspdctomysql->DeleteData(SQL, Msg) == 0)
// printf("删除成功/r/n");
/*vspdctomysql->CloseMySQLConn();*/
system("pause");
return 0;
}//#include "stdafx.h"
#include "VspdCTOMySQL.h"
VspdCToMySQL::VspdCToMySQL()
{
}
VspdCToMySQL::~VspdCToMySQL()
{
}
//初始化数据
int VspdCToMySQL::ConnMySQL(char *host, char * port, char * Db, char * user, char* passwd, char * charset, char * Msg)
{
if (mysql_init(&mysql) == NULL)
{
Msg = "inital mysql handle error";
return 1;
}
if (mysql_real_connect(&mysql, host, user, passwd, Db, 0, NULL, 0) == NULL)
{
Msg = "Failed to connect to database: Error";
return 1;
}
if (mysql_set_character_set(&mysql, "utf8") != 0)
{
Msg = "mysql_set_character_set Error";
return 1;
}
return 0;
}
//查询数据
string VspdCToMySQL::SelectData(char * SQL, int Cnum, char * Msg)
{
MYSQL_ROW m_row;
MYSQL_RES *m_res;
char sql[2048];
sprintf_s(sql, SQL);
int rnum = 0;
char rg = 0x06;//行隔开
char cg = { 0x05 };//字段隔开
if (mysql_query(&mysql, sql)!=0)
{
Msg = "select ps_info Error";
return "";
}
m_res = mysql_store_result(&mysql);
if (m_res == NULL)
{
Msg = "select username Error";
return "";
}
string str("");
while (m_row = mysql_fetch_row(m_res))
{
for (int i = 0; i < Cnum; i++)//Cnum是列数
{
str += m_row[i];
//str += rg;
}
// str += rg;
rnum++;
}
mysql_free_result(m_res);
return str;
}
//插入数据
int VspdCToMySQL::InsertData(char * SQL, char * Msg)
{
char sql[2048];
sprintf_s(sql, SQL);
if (mysql_query(&mysql, sql) != 0)
{
Msg = "Insert Data Error";
return 1;
}
return 0;
}
//更新数据
int VspdCToMySQL::UpdateData(char * SQL, char * Msg)
{
char sql[2048];
sprintf_s(sql, SQL);
if (mysql_query(&mysql, sql) != 0)
{
Msg = "Update Data Error";
return 1;
}
return 0;
}
//删除数据
int VspdCToMySQL::DeleteData(char * SQL, char * Msg)
{
char sql[2048];
sprintf_s(sql, SQL);
if (mysql_query(&mysql, sql) != 0)
{
Msg = "Delete Data error";
return 1;
}
return 0;
}
//关闭数据库连接
void VspdCToMySQL::CloseMySQLConn()
{
mysql_close(&mysql);
}#pragma once
#include <stdio.h>
#include <string>
#include <winsock.h>
#include <mysql.h>
using namespace std;
class VspdCToMySQL
{
public:
//变量
MYSQL mysql;
/*
构造函数和稀构函数
*/
VspdCToMySQL();
~VspdCToMySQL();
/*
主要的功能:
初始化数据库
连接数据库
设置字符集
入口参数:
host :MYSQL服务器IP
port:数据库端口
Db:数据库名称
user:数据库用户
passwd:数据库用户的密码
charset:希望使用的字符集
Msg:返回的消息,包括错误消息
出口参数:
int :0表示成功;1表示失败
*/
int ConnMySQL(char *host, char * port, char * Db, char * user, char* passwd, char * charset, char * Msg);
/*
主要的功能:
查询数据
入口参数:
SQL:查询的SQL语句
Cnum:查询的列数
Msg:返回的消息,包括错误消息
出口参数:
string 准备放置返回的数据,多条记录则用0x06隔开,多个栏位用0x05隔开
如果 返回的长度= 0,责表示舞结果
*/
string SelectData(char * SQL, int Cnum, char * Msg);
/*
主要功能:
插入数据
入口参数
SQL:查询的SQL语句
Msg:返回的消息,包括错误消息
出口参数:
int :0表示成功;1表示失败
*/
int InsertData(char * SQL, char * Msg);
/*
主要功能:
修改数据
入口参数
SQL:查询的SQL语句
Msg:返回的消息,包括错误消息
出口参数:
int :0表示成功;1表示失败
*/
int UpdateData(char * SQL, char * Msg);
/*
主要功能:
删除数据
入口参数
SQL:查询的SQL语句
Msg:返回的消息,包括错误消息
出口参数:
int :0表示成功;1表示失败
*/
int DeleteData(char * SQL, char * Msg);
/*
主要功能:
关闭数据库连接
*/
void CloseMySQLConn();
};代码如上截图如下(是utf8)(待查询表中内容)(编译后。。。。)请问是哪里出错了呢?新手求教
添加回答
举报
0/150
提交
取消