📄 mysqloperat.cpp
字号:
#include "stdafx.h"
#include ".\mysqloperat.h"
#include <iostream>
using namespace std;
CMysqlOperat::CMysqlOperat(void)
{
DWORD dwError = 0;
retCode = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLAllocHandle error = " << dwError << endl;
return;
}
retCode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLSetEnvAttr error = " << dwError << endl;
return;
}
retCode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLAllocHandle error = " << dwError << endl;
return;
}
retCode = ::SQLConnect(hdbc, (SQLCHAR*)"mysqlconnect", SQL_NTS, (SQLCHAR*)"root", SQL_NTS, (SQLCHAR*)"root", SQL_NTS);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLConnectA error = " << dwError << endl;
return;
}
retCode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLAllocHandle error = " << dwError << endl;
return;
}
}
CMysqlOperat::~CMysqlOperat(void)
{
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
int CMysqlOperat::RunSQL(const char* szSql)
{
DWORD dwError = 0;
retCode = SQLExecDirect(hstmt, (SQLCHAR*)szSql, SQL_NTS);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLExecDirectA error = " << dwError << endl;
return 0;
}
return 0;
}
int CMysqlOperat::RunSQL(const char* szSql, int)
{
DWORD dwError = 0;
retCode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLAllocHandle error = " << dwError << endl;
return 0;
}
retCode = SQLExecDirect(hstmt, (SQLCHAR*)szSql, SQL_NTS);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLExecDirectA error = " << dwError << endl;
return 0;
}
return 0;
}
/*
// MysqlOdbc.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
using namespace std;
int main()
{
SQLRETURN retCode;
SQLHANDLE henv;
SQLHDBC hdbc;
DWORD dwError = 0;
retCode = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLAllocHandle error = " << dwError << endl;
return 0;
}
retCode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLSetEnvAttr error = " << dwError << endl;
return 0;
}
retCode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLAllocHandle error = " << dwError << endl;
return 0;
}
retCode = ::SQLConnect(hdbc, (SQLCHAR*)"mysqlconnect", SQL_NTS, (SQLCHAR*)"root", SQL_NTS, (SQLCHAR*)"root", SQL_NTS);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLConnectA error = " << dwError << endl;
return 0;
}
SQLHSTMT hstmt;
retCode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLAllocHandle error = " << dwError << endl;
return 0;
}
string strSQL = "select * from Student;";
retCode = SQLExecDirectA(hstmt, (SQLCHAR*)strSQL.c_str(), SQL_NTS);
if ( (retCode != SQL_SUCCESS) && (retCode != SQL_SUCCESS_WITH_INFO) )
{
dwError = GetLastError();
cout << "SQLExecDirectA error = " << dwError << endl;
return 0;
}
SQLCHAR szName[10] = "\0";
SQLCHAR szClass[10] = "\0";
SQLCHAR szPasswd[20] = "\0";
SQLINTEGER dwSid = 0;
int nResult = 0;
int i = 0;
while (true)
{
retCode = SQLFetch(hstmt);
if (retCode == SQL_ERROR)
{
return 0;
}
else if (retCode == SQL_NO_DATA)
{
break;
}
else
{
SQLGetData(hstmt, 1, SQL_INTEGER, &dwSid, 8, (SQLINTEGER*)&nResult);
SQLGetData(hstmt, 2, SQL_CHAR, szName, 10, (SQLINTEGER*)&nResult);
SQLGetData(hstmt, 3, SQL_CHAR, szClass, 10, (SQLINTEGER*)&nResult);
SQLGetData(hstmt, 4, SQL_CHAR, szPasswd, 16, (SQLINTEGER*)&nResult);
cout << "------------ information for record: " << i << " -------------" << endl;
cout << "sid = " << dwSid << endl;
cout << "sname = " << szName << endl;
cout << "sclass = " << szClass << endl;
cout << "szPasswd = " << szPasswd << endl;
cout << "-----------------------------------------------------" << endl << endl;
i++;
}
}
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return 1;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -