急死了
9 个解决方案
#1
是你没装MYSQL数据库吧或装得不全,你先用数据库自带的软件试试能不能连接上数据库。用ADO就可以操作数据库,而且ADO是通用的,只是不同的数据库连接字符串不一样。数据库你得装上ODBC驱动及C++驱动等,这些是独立文件,不是集成在数据库安装软件里
#2
我用navicat就能连接上啊,用ADO操作数据库步骤 网上查的基本都试过还是不行啊ODBC驱动装了但是在面板里面还是没有显示C++驱动是什么?
#4
m_link.DeleteAllItems();//清空列表
//UpdateData(TRUE);
//CString ss;
//CString str[30];
MYSQL_RES *result;
MYSQL_ROW sql_row;
//MYSQL_FIELD *fd;
mysql_init(&mysql);//初始化
//bool isConnected=mysql_real_connect(&mysql,"localhost","root","密码","数据库名",3306,0,0);//连
if (mysql_real_connect(&mysql,"localhost","root","密码","数据库名",3306,0,0))
{
}
else
{
int i = mysql_errno(&mysql);//连接出错
const char * s = mysql_error(&mysql);
MessageBox("连接失败,请检查数据库.","警告",MB_OK|MB_ICONEXCLAMATION);
}
试试这个吧!
//UpdateData(TRUE);
//CString ss;
//CString str[30];
MYSQL_RES *result;
MYSQL_ROW sql_row;
//MYSQL_FIELD *fd;
mysql_init(&mysql);//初始化
//bool isConnected=mysql_real_connect(&mysql,"localhost","root","密码","数据库名",3306,0,0);//连
if (mysql_real_connect(&mysql,"localhost","root","密码","数据库名",3306,0,0))
{
}
else
{
int i = mysql_errno(&mysql);//连接出错
const char * s = mysql_error(&mysql);
MessageBox("连接失败,请检查数据库.","警告",MB_OK|MB_ICONEXCLAMATION);
}
试试这个吧!
#5
先确定你的ODBC数据源里的驱动程序里有没有MYSQL的ODBC驱动。
没有的话到MYSQL官网下载MYSQL的ODBC驱动
没有的话到MYSQL官网下载MYSQL的ODBC驱动
#6
这是一个测试例子,封装在类里面
// CDBManager.h
#ifndef CDBMANAGER_H__
#define CDBMANAGER_H__
#include "stdafx.h"
#include <winsock.h>
#include "mysql.h"
#include <string>
#include <sstream>
#pragma comment(lib, "libmysql")
#pragma comment(lib, "mysqlclient")
#include "SystemHeadFile.h"
class CDBManager
{
public:
CDBManager() {} ;
virtual ~CDBManager() {};
public:
// conncet
MYSQL* __connect();
// release
void __release(MYSQL *mysql);
private:
Conn_info info;
public:
// get record data with id
CurrentReportList GetRecordData(long lngID);
// delete record data
int DeleteRecordData(long lngID);
// replace record data(modify)
bool ReplaceRecordData(long lngID, CurrentReportList *crData);
// search record data
int SearchRecordData(long lngID);
// store record data
bool StoreRecordDataToMySQL(CurrentReportList *crData);
// add new
int AddNewRecordData(CurrentReportList *rpList, std::string strTableName);
// fill in the struct you wanna get
int GetReportStruct(long lngID, CurrentReportList *rpList);
public:
// made sentence that can be executed in the MYSQL
std::string MadeSentence(std::string strTableName,
std::string strFiledName,
OpType OtOperteType,
std::string strvalue);
public:
// sub-function
// find something
MYSQL_RES *mysql_get_result(MYSQL *mysqldata, std::string str_query);
// execute
int execute_mysql_sentence(MYSQL *mysqldata, std::string str_sentence);
public:
// convert long to std::string
std::string ConvertToString(long value)
{
std::stringstream ss;
ss << value;
return ss.str();
}
// init connect
MYSQL* init_connect()
{
MYSQL *mysqldata = __connect();
if (NULL == mysqldata)
return NULL;
else return mysqldata;
}
};
#endif
#7
// CDBManager.cpp
#include "CDBManager.h"
#include "stdafx.h"
MYSQL* CDBManager::__connect()
{
info.host="localhost";
info.user="root";
info.password="saber";
info.db="xsbdb";
MYSQL *mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql,info.host,info.user,info.password,info.db,3306,NULL,0))
{
return NULL;
}
return mysql;
}
void CDBManager::__release(MYSQL *mysql)
{
mysql_close(mysql);
}
bool CDBManager::StoreRecordDataToMySQL(CurrentReportList *crData)
{
return true;
}
std::string CDBManager::MadeSentence(std::string strTableName,
std::string strFiledName,
OpType OtOperteType,
std::string strvalue)
{
std::string str("");
std::ostringstream ostr;
switch(OtOperteType)
{
case EN_SELECT_: ostr<<"select * from "<<strTableName<<" where "<<strFiledName<<" = "<<strvalue; break;
case EN_DELETE_: ostr<<"delete from "<<strTableName<<" where "<<strFiledName<<" = "<<strvalue; break;
case EN_UPDATE_: break;
}
str = ostr.str();
return str;
}
int CDBManager::AddNewRecordData(CurrentReportList *rpList, std::string strTableName)
{
CurrentReportList rp;
rp = *rpList;
// to make the sentense short , use a struct to get the value
int nAge = rp.stMedicalHistory.nAge;
long ID = rp.stMedicalHistory.lngID;
std::string name = rp.stMedicalHistory.strName;
std::string PtAddr = rp.stMedicalHistory.strPostalAddress;
std::string Ana = rp.stMedicalHistory.strAnamnesis;
std::string dgrp = rp.strDiagnosticReport;
std::string s;
char cSex;
if (rp.stMedicalHistory.bSex) cSex = 'M'; else cSex = 'F';
// make sentence
std::ostringstream ostr;
ostr<<"insert into "<<strTableName<<" values ('"<<ID<<"','"<<name<<"','"<<cSex<<"','"<<PtAddr<<"','"<<Ana<<"','"<<nAge<<"','"<<dgrp<<"')";
s = ostr.str();
MYSQL *mysqldata = init_connect();
if (!mysqldata) return INIT_MYSQL_ERROR;
int n = execute_mysql_sentence(mysqldata,s);
if (n)
{
if (SearchRecordData(rp.stMedicalHistory.lngID))
{
__release(mysqldata);
return TRUE;
}
}
return FALSE;
}
int CDBManager::execute_mysql_sentence(MYSQL *mysqldata,std::string str_sentence)
{
int nReturn = mysql_real_query(mysqldata,str_sentence.c_str(),(UINT)str_sentence.size());
// if nReturn is 0, it JUST means the statement has been exucuted successfully
// here you can add your code to get the error type.
if (nReturn != 0)
{
__release(mysqldata);
return FALSE;
}
return TRUE;
}
int CDBManager::SearchRecordData(long lngID)
{
MYSQL *mysqldata = init_connect();
if (!mysqldata) return INIT_MYSQL_ERROR;
// get the order
std::string s;
s = ConvertToString(lngID);
std::string str_query = MadeSentence("mr","ID",EN_SELECT_,s);
MYSQL_RES *result = mysql_get_result(mysqldata,str_query);
if (!result) {__release(mysqldata);return GET_RESULT_ERROR;}
MYSQL_ROW row;
if (result)
{
while(!(row=mysql_fetch_row(result)))
{
return FALSE;;
}
}
// so it found
__release(mysqldata);
return TRUE;
}
// this function is used only when you wanna find the data not execute a sentence
MYSQL_RES * CDBManager::mysql_get_result(MYSQL *mysqldata, std::string str_query)
{
int n = execute_mysql_sentence(mysqldata,str_query);
if (n)
{
std::string str;
MYSQL_RES *result;
// initialize the retrieval of the result
if(!(result=mysql_use_result(mysqldata)))
{
return NULL;
}
return result;
}
return NULL;
}
int CDBManager::DeleteRecordData(long lngID)
{
if (TRUE == SearchRecordData(lngID))
{
// delete
MYSQL *mysqldata = init_connect();
if (!mysqldata) return INIT_MYSQL_ERROR;
// get the order
std::string s;
s = ConvertToString(lngID);
std::string str_query = MadeSentence("mr","ID",EN_DELETE_,s);
int n = execute_mysql_sentence(mysqldata,str_query);
if (n)
{
if (!SearchRecordData(lngID))
return TRUE;
}
}
return FALSE;
}
int CDBManager::GetReportStruct(long lngID, CurrentReportList *rpList)
{
MYSQL *mysqldata = init_connect();
if (!mysqldata) return INIT_MYSQL_ERROR;
char *ch_query;
ch_query="select * from mr";
if(mysql_real_query(mysqldata,ch_query,(UINT)strlen(ch_query))!=0)
{
AfxMessageBox(TEXT("数据库中表格出错"),MB_OK);
}
std::string str;
MYSQL_RES *result;
MYSQL_ROW row;
long lngGetID;
std::string strData;
if(!(result=mysql_use_result(mysqldata)))
{
AfxMessageBox(TEXT("读取数据集失败"),MB_OK);
}
while(row=mysql_fetch_row(result))
{
str = row[0];
//convert string to long
lngGetID = atol(str.c_str());
if (lngID == lngGetID)
{
// then get the data
// get id
rpList->stMedicalHistory.lngID = lngGetID;
// get name
strData = row[1];
rpList->stMedicalHistory.strName = strData;
// get sex
strData = row[2];
char sex[2];
strData.copy(sex,1,0);
sex[1] = '\0';
if (*sex == 'M')
rpList->stMedicalHistory.bSex = true;
else if (*sex == 'F')
rpList->stMedicalHistory.bSex = false;
// get post addr
strData = row[3];
rpList->stMedicalHistory.strPostalAddress = strData;
// get anamnesis
strData = row[4];
rpList->stMedicalHistory.strAnamnesis = strData;
// get age
strData = row[5];
rpList->stMedicalHistory.nAge = atoi(strData.c_str());
// get dia-report
strData = row[6];
rpList->strDiagnosticReport = strData;
break;
}
}
return TRUE;
}
#8
楼主将这两个文件弄到工程里面去,一个.h,一个cpp,然后声明一个 CDBManager类的指针,调用 __connect去连接。
#9
新建 xx.udl文件 然后填写连接方式,填完后使用记事本打开该文件,字符串的连接符就好了
#1
是你没装MYSQL数据库吧或装得不全,你先用数据库自带的软件试试能不能连接上数据库。用ADO就可以操作数据库,而且ADO是通用的,只是不同的数据库连接字符串不一样。数据库你得装上ODBC驱动及C++驱动等,这些是独立文件,不是集成在数据库安装软件里
#2
我用navicat就能连接上啊,用ADO操作数据库步骤 网上查的基本都试过还是不行啊ODBC驱动装了但是在面板里面还是没有显示C++驱动是什么?
#3
这里有如何获取
mysql连接字符串的。
#4
m_link.DeleteAllItems();//清空列表
//UpdateData(TRUE);
//CString ss;
//CString str[30];
MYSQL_RES *result;
MYSQL_ROW sql_row;
//MYSQL_FIELD *fd;
mysql_init(&mysql);//初始化
//bool isConnected=mysql_real_connect(&mysql,"localhost","root","密码","数据库名",3306,0,0);//连
if (mysql_real_connect(&mysql,"localhost","root","密码","数据库名",3306,0,0))
{
}
else
{
int i = mysql_errno(&mysql);//连接出错
const char * s = mysql_error(&mysql);
MessageBox("连接失败,请检查数据库.","警告",MB_OK|MB_ICONEXCLAMATION);
}
试试这个吧!
//UpdateData(TRUE);
//CString ss;
//CString str[30];
MYSQL_RES *result;
MYSQL_ROW sql_row;
//MYSQL_FIELD *fd;
mysql_init(&mysql);//初始化
//bool isConnected=mysql_real_connect(&mysql,"localhost","root","密码","数据库名",3306,0,0);//连
if (mysql_real_connect(&mysql,"localhost","root","密码","数据库名",3306,0,0))
{
}
else
{
int i = mysql_errno(&mysql);//连接出错
const char * s = mysql_error(&mysql);
MessageBox("连接失败,请检查数据库.","警告",MB_OK|MB_ICONEXCLAMATION);
}
试试这个吧!
#5
先确定你的ODBC数据源里的驱动程序里有没有MYSQL的ODBC驱动。
没有的话到MYSQL官网下载MYSQL的ODBC驱动
没有的话到MYSQL官网下载MYSQL的ODBC驱动
#6
这是一个测试例子,封装在类里面
// CDBManager.h
#ifndef CDBMANAGER_H__
#define CDBMANAGER_H__
#include "stdafx.h"
#include <winsock.h>
#include "mysql.h"
#include <string>
#include <sstream>
#pragma comment(lib, "libmysql")
#pragma comment(lib, "mysqlclient")
#include "SystemHeadFile.h"
class CDBManager
{
public:
CDBManager() {} ;
virtual ~CDBManager() {};
public:
// conncet
MYSQL* __connect();
// release
void __release(MYSQL *mysql);
private:
Conn_info info;
public:
// get record data with id
CurrentReportList GetRecordData(long lngID);
// delete record data
int DeleteRecordData(long lngID);
// replace record data(modify)
bool ReplaceRecordData(long lngID, CurrentReportList *crData);
// search record data
int SearchRecordData(long lngID);
// store record data
bool StoreRecordDataToMySQL(CurrentReportList *crData);
// add new
int AddNewRecordData(CurrentReportList *rpList, std::string strTableName);
// fill in the struct you wanna get
int GetReportStruct(long lngID, CurrentReportList *rpList);
public:
// made sentence that can be executed in the MYSQL
std::string MadeSentence(std::string strTableName,
std::string strFiledName,
OpType OtOperteType,
std::string strvalue);
public:
// sub-function
// find something
MYSQL_RES *mysql_get_result(MYSQL *mysqldata, std::string str_query);
// execute
int execute_mysql_sentence(MYSQL *mysqldata, std::string str_sentence);
public:
// convert long to std::string
std::string ConvertToString(long value)
{
std::stringstream ss;
ss << value;
return ss.str();
}
// init connect
MYSQL* init_connect()
{
MYSQL *mysqldata = __connect();
if (NULL == mysqldata)
return NULL;
else return mysqldata;
}
};
#endif
#7
// CDBManager.cpp
#include "CDBManager.h"
#include "stdafx.h"
MYSQL* CDBManager::__connect()
{
info.host="localhost";
info.user="root";
info.password="saber";
info.db="xsbdb";
MYSQL *mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql,info.host,info.user,info.password,info.db,3306,NULL,0))
{
return NULL;
}
return mysql;
}
void CDBManager::__release(MYSQL *mysql)
{
mysql_close(mysql);
}
bool CDBManager::StoreRecordDataToMySQL(CurrentReportList *crData)
{
return true;
}
std::string CDBManager::MadeSentence(std::string strTableName,
std::string strFiledName,
OpType OtOperteType,
std::string strvalue)
{
std::string str("");
std::ostringstream ostr;
switch(OtOperteType)
{
case EN_SELECT_: ostr<<"select * from "<<strTableName<<" where "<<strFiledName<<" = "<<strvalue; break;
case EN_DELETE_: ostr<<"delete from "<<strTableName<<" where "<<strFiledName<<" = "<<strvalue; break;
case EN_UPDATE_: break;
}
str = ostr.str();
return str;
}
int CDBManager::AddNewRecordData(CurrentReportList *rpList, std::string strTableName)
{
CurrentReportList rp;
rp = *rpList;
// to make the sentense short , use a struct to get the value
int nAge = rp.stMedicalHistory.nAge;
long ID = rp.stMedicalHistory.lngID;
std::string name = rp.stMedicalHistory.strName;
std::string PtAddr = rp.stMedicalHistory.strPostalAddress;
std::string Ana = rp.stMedicalHistory.strAnamnesis;
std::string dgrp = rp.strDiagnosticReport;
std::string s;
char cSex;
if (rp.stMedicalHistory.bSex) cSex = 'M'; else cSex = 'F';
// make sentence
std::ostringstream ostr;
ostr<<"insert into "<<strTableName<<" values ('"<<ID<<"','"<<name<<"','"<<cSex<<"','"<<PtAddr<<"','"<<Ana<<"','"<<nAge<<"','"<<dgrp<<"')";
s = ostr.str();
MYSQL *mysqldata = init_connect();
if (!mysqldata) return INIT_MYSQL_ERROR;
int n = execute_mysql_sentence(mysqldata,s);
if (n)
{
if (SearchRecordData(rp.stMedicalHistory.lngID))
{
__release(mysqldata);
return TRUE;
}
}
return FALSE;
}
int CDBManager::execute_mysql_sentence(MYSQL *mysqldata,std::string str_sentence)
{
int nReturn = mysql_real_query(mysqldata,str_sentence.c_str(),(UINT)str_sentence.size());
// if nReturn is 0, it JUST means the statement has been exucuted successfully
// here you can add your code to get the error type.
if (nReturn != 0)
{
__release(mysqldata);
return FALSE;
}
return TRUE;
}
int CDBManager::SearchRecordData(long lngID)
{
MYSQL *mysqldata = init_connect();
if (!mysqldata) return INIT_MYSQL_ERROR;
// get the order
std::string s;
s = ConvertToString(lngID);
std::string str_query = MadeSentence("mr","ID",EN_SELECT_,s);
MYSQL_RES *result = mysql_get_result(mysqldata,str_query);
if (!result) {__release(mysqldata);return GET_RESULT_ERROR;}
MYSQL_ROW row;
if (result)
{
while(!(row=mysql_fetch_row(result)))
{
return FALSE;;
}
}
// so it found
__release(mysqldata);
return TRUE;
}
// this function is used only when you wanna find the data not execute a sentence
MYSQL_RES * CDBManager::mysql_get_result(MYSQL *mysqldata, std::string str_query)
{
int n = execute_mysql_sentence(mysqldata,str_query);
if (n)
{
std::string str;
MYSQL_RES *result;
// initialize the retrieval of the result
if(!(result=mysql_use_result(mysqldata)))
{
return NULL;
}
return result;
}
return NULL;
}
int CDBManager::DeleteRecordData(long lngID)
{
if (TRUE == SearchRecordData(lngID))
{
// delete
MYSQL *mysqldata = init_connect();
if (!mysqldata) return INIT_MYSQL_ERROR;
// get the order
std::string s;
s = ConvertToString(lngID);
std::string str_query = MadeSentence("mr","ID",EN_DELETE_,s);
int n = execute_mysql_sentence(mysqldata,str_query);
if (n)
{
if (!SearchRecordData(lngID))
return TRUE;
}
}
return FALSE;
}
int CDBManager::GetReportStruct(long lngID, CurrentReportList *rpList)
{
MYSQL *mysqldata = init_connect();
if (!mysqldata) return INIT_MYSQL_ERROR;
char *ch_query;
ch_query="select * from mr";
if(mysql_real_query(mysqldata,ch_query,(UINT)strlen(ch_query))!=0)
{
AfxMessageBox(TEXT("数据库中表格出错"),MB_OK);
}
std::string str;
MYSQL_RES *result;
MYSQL_ROW row;
long lngGetID;
std::string strData;
if(!(result=mysql_use_result(mysqldata)))
{
AfxMessageBox(TEXT("读取数据集失败"),MB_OK);
}
while(row=mysql_fetch_row(result))
{
str = row[0];
//convert string to long
lngGetID = atol(str.c_str());
if (lngID == lngGetID)
{
// then get the data
// get id
rpList->stMedicalHistory.lngID = lngGetID;
// get name
strData = row[1];
rpList->stMedicalHistory.strName = strData;
// get sex
strData = row[2];
char sex[2];
strData.copy(sex,1,0);
sex[1] = '\0';
if (*sex == 'M')
rpList->stMedicalHistory.bSex = true;
else if (*sex == 'F')
rpList->stMedicalHistory.bSex = false;
// get post addr
strData = row[3];
rpList->stMedicalHistory.strPostalAddress = strData;
// get anamnesis
strData = row[4];
rpList->stMedicalHistory.strAnamnesis = strData;
// get age
strData = row[5];
rpList->stMedicalHistory.nAge = atoi(strData.c_str());
// get dia-report
strData = row[6];
rpList->strDiagnosticReport = strData;
break;
}
}
return TRUE;
}
#8
楼主将这两个文件弄到工程里面去,一个.h,一个cpp,然后声明一个 CDBManager类的指针,调用 __connect去连接。
#9
新建 xx.udl文件 然后填写连接方式,填完后使用记事本打开该文件,字符串的连接符就好了