📄 buliddb.cpp
字号:
//////////////////////////////////////////////////////////////////////
//
// 名称: CBulidDB
//
// 功能: 根据XML库格式生成Mdb数据库
//
// 注释:
//
// 编写: 徐景周
//
// 日期: 2005.05.30
//
////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DBClient.h"
#include "BulidDB.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// 构造
CBulidDB::CBulidDB() : m_bDone( false ) // 初始置未完成
{
}
// 析构
CBulidDB::~CBulidDB()
{
}
// 导入XML柱子数据文件,此涵数从XML_PARSER类中派生
bool CBulidDB::Parse_XML_Document()
{
if( !m_bDone )
{
if( Is_Tag( "<Database>" ) )
{
// 获取生成数据库名称
if( Is_Having_Attribute( "name" ) )
m_strDBName = Get_Attribute_Value();
}
if( Is_Tag( "<Table>" ) )
{
// 获取库内表名
if( Is_Having_Attribute( "name" ) )
m_strTableName = Get_Attribute_Value();
}
// 库名或表名为空退出
if( "" == m_strDBName || "" == m_strTableName )
return false;
// 获取主程序所在路径,存在sPath中
CString sPath;
GetModuleFileName( NULL, sPath.GetBufferSetLength( MAX_PATH + 1 ), MAX_PATH );
sPath.ReleaseBuffer();
int nPos;
nPos = sPath.ReverseFind( '\\' );
sPath = sPath.Left( nPos );
CString lpszFile = sPath + "\\" + m_strDBName;
CFileFind fFind;
BOOL bSuccess;
bSuccess = fFind.FindFile( lpszFile );
fFind.Close ();
CDaoDatabase db; // 数据库
CDaoRecordset RecSet( &db ); // 记录集
// 是否已有创建好的库文件,没有则创建它
if( !bSuccess )
{
// 创建Mdb库
db.Create( lpszFile );
// 移动节点到指定位置
Go_to_Parent("Table");
Go_to_Child("Struct");
Go_to_Child( "Field" );
// 获取库结构
CString sqlCmd = "CREATE TABLE " + m_strTableName + "(";
while( Is_Tag( "<Field>" ) && Is_Child_of( "<Database><Table><Struct>" ) )
{
CString strField = "";
// 列名
if( Is_Having_Attribute( "fieldName" ) )
strField = Get_Attribute_Value();
sqlCmd += strField + " ";
// 列类型
if( Is_Having_Attribute( "fieldType" ) )
strField = Get_Attribute_Value();
sqlCmd += strField + "(";
// 列长度
if( Is_Having_Attribute( "fieldLength" ) )
strField = Get_Attribute_Value();
sqlCmd += strField + "),";
// 同一级下一节点
if( !Go_Forward() )
break;
}
// 删除尾部多余逗号
if( -1 != sqlCmd.ReverseFind( ',' ) )
sqlCmd.Delete( sqlCmd.GetLength() - 1, 1 );
sqlCmd += ");";
// 创建库结构
db.Execute( sqlCmd );
CString strQuery = "SELECT * FROM " + m_strTableName;
// 打开已创建的数据表
RecSet.Open( AFX_DAO_USE_DEFAULT_TYPE,
strQuery, 0 );
// 移动节点到指定位置
Go_to_Parent("Table");
Go_to_Child("Content");
Go_to_Child( "Record" );
// 创建库内容记录
while( Is_Tag( "<Record>" ) && Is_Child_of( "<Database><Table><Content>" ) )
{
CString sqlRecordCmd = "INSERT INTO " + m_strTableName + "(Name,Age) VALUES(";
CString strRecord = "";
// 名字
if( Is_Having_Attribute( "name" ) )
strRecord = Get_Attribute_Value();
sqlRecordCmd += "'" + strRecord + "', ";
// 年龄
if( Is_Having_Attribute( "age" ) )
strRecord = Get_Attribute_Value();
sqlRecordCmd += strRecord + ")";
// 插入记录
db.Execute( sqlRecordCmd );
// 同一级下一节点
if( !Go_Forward() )
break;
}
// 关闭记录集及库
RecSet.Close();
db.Close();
// 完成
m_bDone = true;
AfxMessageBox( lpszFile + "Access库成功创建!" );
return true;
}
else
{
AfxMessageBox( lpszFile + "Access库已经存在!" );
}
}
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -