⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kjdbms_m.cpp

📁 一个小型数据库系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void Insert(void)
{
	int iChoiceUpdate, i ;
	char  sEnter[100], sWork[100], sWorkCheck[100], sPrimaryKey[100] , sri[100];
	// 初始化_Recordset的引用spRS
	CREATEiNSTANCE(spRS,Recordset);
	spRS->PutRefActiveConnection( spCON );
InsertAgain:
	//Get the Table number you want to update
	iChoiceUpdate = GetTable(" 添加 ", TABLENUM) ;
	//print the property name to choose
	//iItemNum = GetPorperty(&iChoiceProperty, iChoiceUpdate) ;
	printf("\n操作之前您可以首先进行查询,以免重复! 1...确认 ") ;
	scanf("%d", &i) ;
		//\n请根据属性号码选择进行查询: ") ;
	try
	{
		if (i==1)
		{
			QueryPart(iChoiceUpdate) ;
			//---------------------------
				if (spRS->adoEOF == -1)
					printf("\n该元组不存在!\n") ;
				else
				{
					PrintData(iChoiceUpdate) ;				
				}//找到的话,打印,完毕
			//--------------------------
			spRS->Close () ;//close 425->316

		}			

		strcpy(sWork, "") ;
		strcpy(sWork, "select * from ") ;
		strcat(sWork, sTableName[iChoiceUpdate-1][0]) ;
		strcat(sWork, " where ") ;
		strcat(sWork, sTableName[iChoiceUpdate-1][1]) ;
		strcat(sWork, " = 'test' ;") ;
		spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1); //取属性close 483
		//constructe the sql sentence
		strcpy(sWork, "") ;
		strcat(sWork, "Insert into ") ;
		strcat(sWork, sTableName[iChoiceUpdate-1][0]) ;
		strcat(sWork, " values (" ) ;
		i = 0 ;
		while( strcmp(sTableName[iChoiceUpdate-1][i+1], "") !=0 )
		{	
			i++ ;
			//获取属性类型
			//if (spRS->Fields ->Item [_variant_t	(sTableName[iChoiceUpdate-1][i])]->Type == adBoolean)
			//	continue ;
			printf("请输入 %s 的属性值: ", sTableName[iChoiceUpdate-1][i]) ;
			scanf("%s", sEnter) ;
			
			if (i==1)
				strcpy(sPrimaryKey, sEnter) ;

			if (spRS->Fields ->Item [_variant_t (sTableName[iChoiceUpdate-1][i])]->Type == adSmallInt)
				strcat(sWork, sEnter) ;
			else
			{
				strcat(sWork, "'") ;
				strcat(sWork, sEnter) ;
				strcat(sWork, "'") ;
			}

			if (strcmp(sTableName[iChoiceUpdate-1][i+1], "") ==0)
			{
				strcat(sWork, ") ;" ) ;
				//break ;
			}
			else
				strcat(sWork, ", ") ;			
		}

		QuerySql(iChoiceUpdate, 1, sWorkCheck, sPrimaryKey) ;
		
		spRS->Close() ;//close 444
		spRS->Open (sWorkCheck, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1); //check close 489
		if (spRS->adoEOF == -1)
		{		
			spCON->Execute(sWork, &RecordsAffected,adCmdText) ;			
			printf("\nSuccess\n") ;	
			spRS->Close() ;//close 484
				strcpy(sri, "insert ");
				strcat(sri, sTableName[iChoiceUpdate-1][0]) ;
				rizhi( sri ) ;

		}
		else		
		{
			printf("\n您可能插入了重复元组,操作失败!!!") ;
			spRS->Close() ;//close 484
		}
		//spCON->Execute(sWork,&RecordsAffected,adCmdText);
	}
	catch( _com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bs =	_bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
						+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
						+ _bstr_t(e.Description());
        MessageBox(0,bs,bstrSource, MB_OK);
	}
	//break;
	printf("\n您希望再次插入吗?1...是的 ") ;
	scanf("%d", &i) ;
	if (i==1)
		goto InsertAgain ;

	return ;
}

void DeleteField ( void )
{
	int iChoiceUpdate, iChoiceField, i ;
	char sPrimaryKey[100], sWork[100] , sri[100];
	// 初始化_Recordset的引用spRS
	CREATEiNSTANCE(spRS,Recordset);
	spRS->PutRefActiveConnection( spCON );
DeleteAgain:
	//Get the Table number you want to update
	iChoiceUpdate = GetTable(" 删除 ", TABLENUM) ;
	//print the property name to choose
	//iItemNum = GetPorperty(&iChoiceProperty, iChoiceUpdate) ;
	printf("\n删除之前您必须进行查询,确保被删元组的存在!\n") ;
	QueryPart(iChoiceUpdate) ;
	//---------------------------
	if (spRS->adoEOF == -1)
	{
		printf("\n对不起,没有找到!\n") ;
		spRS->Close() ;//close 316
	}
	else
	{
		PrintData(iChoiceUpdate) ;
	//找到的话,打印,完毕
	//--------------------------
		printf("\n您需要删除哪一个? ") ;
		scanf("%d", &iChoiceField) ;
		spRS->MoveFirst () ;
		spRS->Move (iChoiceField-1 ) ;
		strcpy(sPrimaryKey, UC _bstr_t(RsITEM(spRS, sTableName[iChoiceUpdate-1][1]))) ;
		spRS->Close() ; //close 316
		
		//----test
		//for (int i=1; i<=iItemNum; i++)
		//	printf("%-17s", UC _bstr_t( RsITEM ( spRS, sTableName[iChoiceUpdate-1][i] ) ) ) ;
		//----test end
		//constructe the sql sentence
		strcpy(sWork, "") ;
		strcat(sWork, "Delete from ") ;
		strcat(sWork, sTableName[iChoiceUpdate-1][0]) ;
		strcat(sWork, " where ") ;
		strcat(sWork, sTableName[iChoiceUpdate-1][1]) ;
		strcat(sWork, " = '") ;
		strcat(sWork, sPrimaryKey) ;
		strcat(sWork, "' ; ") ;
		try
		{					
			spCON->Execute(sWork,&RecordsAffected,adCmdText);
			printf("\nSuccess\n") ;
				strcpy(sri, "delete ");
				strcat(sri, sTableName[iChoiceUpdate-1][0]) ;
				rizhi( sri ) ;

		}
		catch( _com_error &e)
		{
			_bstr_t bstrSource(e.Source());
			_bstr_t bs =	_bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
							+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
							+ _bstr_t(e.Description());
			MessageBox(0,bs,bstrSource, MB_OK);
		}
	}
	printf("\n您想再次删除吗?1...是的 ") ;
	scanf("%d", &i) ;
	if (i==1)
		goto DeleteAgain ;
	return ;
}

void PrintData( int iChoice )
{
	//Print the Table property, as the head line
	int iItemNum ;
	//getch() ;
	printf("   ") ;
	iItemNum=1 ;
	while( (iItemNum<ITEMNUM) && ((strcmp(sTableName[iChoice-1][iItemNum], "")) != 0) )
	{
		printf("%-17s", sTableName[iChoice-1][iItemNum]) ;
		iItemNum++ ;
	}
	printf("\n") ;
	iItemNum-- ;
	
	//从读出的记录集合中获取数据
	int j=1 ;
	while(spRS->adoEOF == false)
	{
		if ( strcmp(UC _bstr_t( RsITEM ( spRS, sTableName[iChoice-1][1])), "test") != 0)
		{
			printf("%2d.", j) ;
			for (int i=1; i<=iItemNum; i++)							
			{
				if ( spRS->Fields ->Item [_variant_t(sTableName[iChoice-1][i])]->Type == adBoolean )
				{
					if (strcmp(UC _bstr_t ( RsITEM( spRS, sTableName[iChoice-1][i])), "0") == 0)
						printf("%-17s", "空闲") ;
					else
						printf("%-17s", "工作") ;
				}
				else
					printf("%-17s", UC _bstr_t( RsITEM( spRS, sTableName[iChoice-1][i] ) ) ) ;
			}
			printf("\n") ;
			if (j%5==0)
			{
				printf("\n     ##### Press any key to continue! #####\n") ;
				getch() ;
			}
			j++ ;
		}
		
		//前往下一条记录
		spRS->MoveNext();
	}

	return  ;
}

void QuerySql( int iChoiceQuery, int iChoiceProperty, char * sWork, char * sEnter)
{
	strcpy(sWork, "") ;
	strcat(sWork, "select * from ") ;
	strcat(sWork, sTableName[iChoiceQuery-1][0]) ;
	strcat(sWork, " where ") ;
	strcat(sWork, sTableName[iChoiceQuery-1][iChoiceProperty]) ;
	
	//根据类型决定操作
	if (spRS->Fields ->Item [_variant_t(sTableName[iChoiceQuery-1][iChoiceProperty])]->Type == adSmallInt)
	{
		strcat(sWork, " = ") ;
		strcat(sWork, sEnter) ;
	}
	else if (spRS->Fields ->Item [_variant_t(sTableName[iChoiceQuery-1][iChoiceProperty])]->Type ==adDBTimeStamp)
	{
		strcat(sWork, " like '") ;
		strcat(sWork, sEnter) ;
		strcat(sWork, "'") ;
	}
	else
	{
		strcat(sWork, " = '") ;
		strcat(sWork, sEnter) ;
		strcat(sWork, "'") ;
	}

	return ;
}

void ChangePsd(void)
{
	char sWork[100] ;
	char psd1[30], psd2[30] ;
ChangeAgain:
	printf("\n请输入新密码: ") ;
	scanf("%s", psd1) ;
	printf("\n请再输入一次: ") ;
	scanf("%s", psd2) ;

	if (strcmp(psd1, psd2) !=0 )
	{
		printf("\n密码不一致!\n") ;
		goto ChangeAgain ;
	}
	else
	{
		strcpy(sWork, "") ;
		strcat(sWork, "update 用户信息 set 密码 = '") ;
		strcat(sWork, psd1) ;
		strcat(sWork, "' where 用户名 = '") ;
		strcat(sWork, user) ;
		strcat(sWork, "'") ;
		// 初始化_Recordset的引用spRS
		CREATEiNSTANCE(spRS,Recordset);
		spRS->PutRefActiveConnection( spCON );
		spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//无集合close 694
		printf("\nSuccess\n") ;
		//spRS->Close() ;//close 692
		
		rizhi("change password") ;
		return ;
	}
}
void PlaneUp(void) 
{
	char sWork[100] ;
	char sPrimaryKey[100] ;

	printf("\n请输入战机ID: \n") ;
	scanf("%s", sPrimaryKey) ;
	try{
		strcpy(sWork, "") ;
		strcat(sWork, "select * from 战机基本信息 where 战机ID = '") ;
		strcat(sWork, sPrimaryKey) ;
		strcat(sWork, "'") ;
		// 初始化_Recordset的引用spRS
		CREATEiNSTANCE(spRS,Recordset);
		spRS->PutRefActiveConnection( spCON );
		spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//close 732

		if (spRS->adoEOF == -1)
		{
			printf("\n没有这架飞机!\n") ;
			spRS->Close() ;//close 717
		}
		else if (strcmp(UC _bstr_t ( RsITEM( spRS, sTableName[5][3])), "-1") == 0)
		{
			printf("\n这架飞机已起飞!\n") ;
			spRS->Close() ;//close 717
		}
		else
		{
			spRS->Close() ;//close 717
			strcpy(sWork, "") ;
			strcat(sWork, "update 战机基本信息 set 当前状态 = -1 where 战机ID = '") ;
			strcat(sWork, sPrimaryKey) ;
			strcat(sWork, "'") ;			
			spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//close 734
			//spRS->Close() ;//close 733
			printf("\nsuccess!\n") ;
			rizhi("plane up") ;			
		}
	}
		catch( _com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bs =	_bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
						+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
						+ _bstr_t(e.Description());
        MessageBox(0,bs,bstrSource, MB_OK);
	}

	return ;
}
void PlaneDown(void)
{
	char sWork[100] ;
	char sPrimaryKey[100] ;

	printf("\n请输入战机ID: \n") ;
	scanf("%s", sPrimaryKey) ;
	try{
		strcpy(sWork, "") ;
		strcat(sWork, "select * from 战机基本信息 where 战机ID = '") ;
		strcat(sWork, sPrimaryKey) ;
		strcat(sWork, "'") ;
		// 初始化_Recordset的引用spRS
		CREATEiNSTANCE(spRS,Recordset);
		spRS->PutRefActiveConnection( spCON );
		spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//close 786

		if (spRS->adoEOF == -1)
		{
			printf("\n没有这架飞机!\n") ;
			spRS->Close() ;//close 768
		}
		else if (strcmp(UC _bstr_t ( RsITEM( spRS, sTableName[5][3])), "0") == 0)
		{
			printf("\n这架飞机未起飞!\n") ;
			spRS->Close() ;//close 768
		}
		else
		{
			spRS->Close() ;//close 768
			strcpy(sWork, "") ;
			strcat(sWork, "update 战机基本信息 set 当前状态 = 0 where 战机ID = '") ;
			strcat(sWork, sPrimaryKey) ;
			strcat(sWork, "'") ;
			spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//close 788
			//spRS->Close() ;//close 787
			printf("\nsuccess!\n") ;
			rizhi ("plane down") ;
		}
	}
		catch( _com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bs =	_bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
						+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
						+ _bstr_t(e.Description());
        MessageBox(0,bs,bstrSource, MB_OK);
	}

	return ;
}
void Find(void)
{
	char sWork[100], sri[100] ;
	try{
		strcpy(sWork, "") ;
		strcat(sWork, "select * from ") ;
		strcat(sWork, sTableName[iType-2][0]) ;
		strcat(sWork, " where 姓名 = '") ;
		strcat(sWork, sName) ;
		strcat(sWork, "'") ;
		// 初始化_Recordset的引用spRS
		CREATEiNSTANCE(spRS,Recordset);
		spRS->PutRefActiveConnection( spCON );
		spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);//close 814
		PrintData(iType-1) ;
		spRS->Close() ;//close 812
		strcpy(sri, "") ;
		strcat(sri, "find ") ;
		strcat(sri, sTableName[iType-2][0]) ;
		rizhi(sri) ;
		
	}
		catch( _com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bs =	_bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ") 
						+ _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ") 
						+ _bstr_t(e.Description());
        MessageBox(0,bs,bstrSource, MB_OK);
	}


	return ;
}
void rizhi ( char * ss) 
{
	char sWork[100] ;
	
	strcpy(sWork, "") ;
	strcat(sWork, "insert into 日志 values ('") ;
	strcat(sWork, user) ;
	strcat(sWork, "','");
	strcat(sWork, ss) ;
	strcat(sWork, "')") ;
		// 初始化_Recordset的引用spRS
		//CREATEiNSTANCE(spRS,Recordset);
		//spRS->PutRefActiveConnection( spCON );
		spRS->Open (sWork, vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);
		printf("\n已加入日志\n") ;
		//spRS->Close() ;
		//spRS = NULL ;
	return ; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -