mysql> create database school;
mysql> use school;
Database changed
mysql> create table teacher
-> (
-> id int(3) auto_increment not null primary key,
-> name char(10) not null,
-> );
mysql> insert into teacher values('','glchengang');
mysql> insert into teacher values('','jack');
mysql> use school;
mysql> select * from teacher ;
+----+------------+
| id | name |
+----+------------+
| 1 | glchengang |
| 2 | jack |
+----+------------+
******************************************************************************************************************
// kkkk.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "kkkk.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// The one and only application object
CWinApp theApp;
using namespace std;
#import "c:/Program Files/Common Files/System/ADO/msado15.dll" /
no_namespace rename("EOF", "EndOfFile")
#include <iostream>
#include <stdio.h>
#include <afx.h> //for CString()
using namespace std;
class STU
{
public:
char id[10];
char name[10];
public:
STU(){}
~STU(){}
};
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
STU student;
CString strName,strId;
::CoInitialize(NULL); // 初始化OLE/COM库环境 ,为访问ADO接口做准备
_RecordsetPtr m_pRecordset("ADODB.Recordset");
_ConnectionPtr m_pConnection("ADODB.Connection");
_bstr_t bstrSQL("select * from teacher"); //查询语句
char * query_cmd = "DELETE FROM DemoTable WHERE name = 'jack'";
try
{
// 创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect="Driver={MySQL ODBC 3.51 Driver};Server=localhost;"
"Database=school;User=root;Password=111111121;Option=4;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!/n";
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
_variant_t vsid,vsname; //对应库中的id,name
cout << "ID 名字 ";
cout << "/n----------------------------------------------------------------/n";
while (!m_pRecordset->EndOfFile)
{
vsid= m_pRecordset->GetCollect("id");
vsname = m_pRecordset->GetCollect("name");
if (vsid.vt != VT_NULL && vsname.vt != VT_NULL)
{
strId = (LPCSTR)_bstr_t(vsid);
strName = (LPCSTR) _bstr_t(vsname);
printf("%s/t",strId);
printf("%s",strName);
printf("/n"); //一个记录完给个回车
}
m_pRecordset->MoveNext(); ///移到下一条记录
}
cout << "/n----------------------------------------------------------------/n";
cout << "/n请输入你要添加的记录/n";
cout << "/id:";
cin >> student.id;
cout << "/n名字:";
cin >> student.name;
m_pRecordset->MoveFirst(); //移动到第一条记录
m_pRecordset->AddNew(); ///添加新记录
atol(student.id);
m_pRecordset->PutCollect("id",_variant_t(student.id));
m_pRecordset->PutCollect("name",_variant_t(student.name));
m_pRecordset->Update();
m_pConnection->Execute(query_cmd,NULL,1); //用Execute执行sql语句来删除
getchar();
m_pRecordset->Close(); // 关闭记录集
}
// 捕捉异常
catch(_com_error e)
{
// 显示错误信息
cerr << "/nERROR:" << (char*)e.Description(); //被抛出则异常
getchar();
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
}
return nRetCode;
}