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

📄 db_function.c

📁 unxi下共享内存的使用
💻 C
字号:
/* DB_libry 封装函数,连结 sybase */


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/************************************************
* Description : Initialize DB-Library
* Function : db_init()
* Input : 
* Ouput : int 由返回值判断初试化状态
************************************************/	

int DB_init(void)
{
	/* Initialize DB-Library. */
	if (dbinit() == FAIL) return(-1);
	//dbsetversion(DBVERSION_100);
	return(1);
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**************************************************
* Description: Connect database server		
* Function : db_connect()
* Input : char * Dbname		数据库名称
*	  char * YsrName	用户名
*	  char * UsrPwd		密码
* Ouput : DBPROCESS * dbproc	获得的process
***************************************************/

DBPROCESS* DB_connect (char *DbName,char *UsrName,char *UsrPwd) 
{
	DBPROCESS	*dbproc;
     	LOGINREC	*login; 
	
	login = dblogin();
	
	DBSETLUSER(login, UsrName);
	DBSETLPWD(login, UsrPwd);
	
	//BCP_SETL(login, TRUE) ;	//????? 

	dbproc = dbopen(login, NULL);
	
	dbloginfree(login);		//一定要释放dbloginfree
        
        if(dbproc!=NULL) dbuse(dbproc, DbName);
	else return(NULL);
 
        return(dbproc);
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/******************************************************
* Description: Disconnect Database Server
* Function : DB_disconnect()
* Input : 
* Output : 
*******************************************************/

void  DB_disconnect(DBPROCESS* dbproc)
{
	dbclose(dbproc);
	dbexit();
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**********************************************************************
* Description : Send some Sql to the server with dbsqlexec
* Function : DB_sqlexec()
* Input : DBPROCESS * dbporc
*	  int Sql_type		Sql语句的类型 1 为select 0为其他
*	  char * SqlStr         Sql语句
*	  int units		预计取得的一条记录的单元个数(不超过20)
*	  int records		预计取得的记录数
*         void * temp_space	存储空间的地址
*	  int record_len	单元的最大长度(不超过2048)
* Output : int 	
***********************************************************************/

int DB_sqlexec(DBPROCESS* dbproc,char * SqlStr,int Sql_type,int units,int records,int record_len,void* temp_space)
{
	//DBPROCESS     *dbproc;       /* Our connection with SQL Server. */
        RETCODE        result_code;

	DBCHAR	 	text[20][2048];
	
	int 		i;
	int 		j;
	
	/*	判断 process 是否为空	*/
	if (dbproc ==(DBPROCESS*)NULL)
	{
		printf("DBPROCESS NULL SqlStr:%s\n",SqlStr);
		return(-1);
	}
	
	/*	执行Sql语句		*/
	dbcmd(dbproc,SqlStr);
        dbsqlexec(dbproc);
	
	/* 判断返回值,是否成功		*/
	result_code = dbresults(dbproc);
	
	i = 0;
	j = 0;
	
	if(result_code == SUCCEED)
	{
		if(Sql_type == 1)
		/* select 型SQL语句	*/
		{
		
			/*	BIND 足够多的单元数	*/
			while( j < units )
			{
			dbbind(dbproc, j+1, NTBSTRINGBIND, (DBINT)0,text[j]);	
			
			j++;
			printf("bind %d\n",j);
			}
			
			/*	从PROCESS上取得数据	*/
			while (dbnextrow(dbproc) != NO_MORE_ROWS)
			{	
				j = 0;
				if ( i < records )//判断所取数据有否超过预留存储空间
				{
					while(j < units)
					{	
					/*	copy至对应存储空间	*/
					memcpy(temp_space+i*units*record_len+j*record_len,text[j],record_len);
					j++;
					}
				}	
				i++;
			}
			
			
		
		}
		/*INSERT,UPDATE型SQL语句*/	
		else 
		{
		
		return(0);
		}
		
		return(i);
	}
	
	else 
	{
	printf("sql error sql:%s\n",SqlStr);
	return(-1);	
	}
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**************************************************************************
*Description : Get record
*Function : Get_record()
*Intput : void * start_data 
*	  char * record		
*	  int num		第几条记录,从0开始记数
*	  int r_len		记录的长度(=在DB_sqlexec()中的 record_len)
*Output : int  			0为成功,-1为失败
***************************************************************************/

int Get_record(void *start_data,char * record,int num,int r_len)
{
	memcpy(record,start_data+num*r_len,r_len);   
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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