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

📄 mysqlfunc.c

📁 一个C语言的聊天室源代码
💻 C
字号:
/*
	Version: 0.2.0(alpha)
	Author: Computer_xu
	Email: Computer_xu@sina.com
	HomePage: http://www.socketchat.com
	LastModify: 2001-07-02 (yyyy-mm-dd)
*/
#include "mysqlfunc.h"

MyDB *sqlfd;

MyDB * CreateMyDB(char *host, char *user, char *passwd, char *database)
{
	MyDB *tmp;

	tmp=(MyDB *)Malloc(sizeof(MyDB));
	tmp->mysql=mysql_init((MYSQL *)NULL);

	tmp->host=(char *)Malloc(Strlen(host)+1);
	strcpy(tmp->host,host);

	tmp->user=(char *)Malloc(Strlen(user)+1);
	strcpy(tmp->user,user);

	tmp->passwd=(char *)Malloc(Strlen(passwd)+1);
	strcpy(tmp->passwd,passwd);

	tmp->db=(char *)Malloc(Strlen(database)+1);
	strcpy(tmp->db,database);

	tmp->port=0;
	tmp->unix_socket=NULL;
	tmp->client_flag=0;
	return(tmp);
}

void OpenMyDB(MyDB *handle)
{
	if( !mysql_real_connect(handle->mysql,
				handle->host,	
				handle->user,
				handle->passwd,
				handle->db,
				0,NULL,0) )
		ErrorMyDB( handle );
}

void ReOpenMyDB(MyDB *handle)
{
	Free(handle->mysql);
	handle->mysql=mysql_init((MYSQL *)NULL);
	if( !mysql_real_connect(handle->mysql,
				handle->host,	
				handle->user,
				handle->passwd,
				handle->db,
				0,NULL,0) )
		ErrorMyDB( handle );
}

int QueryMyDB(MyDB *handle, char *query, int length)
{
	int rn;

/*
	unsigned int len;
	char *buf;

	buf=(char *)Malloc(length * 2 + 1);
	len=mysql_escape_string(buf, query, length);
	rn=mysql_real_query(mysql, buf, length);
	Free(buf);
*/

	while( 1 )
	{
		rn=mysql_real_query(handle->mysql, query, length);

		if( rn == CR_SERVER_LOST )
		{
			printf("MyDB ERROR %d : %s\n", mysql_errno(handle->mysql), mysql_error(handle->mysql));
			printf("Reconnect to SQL Server...");
			ReOpenMyDB(handle);
			printf("OK\n");
		}
		else if( rn )
		{
			printf("Query String : %s\n",query);
			ErrorMyDB(handle);
		}
		else	return( rn );
	}
}

void ErrorMyDB(MyDB *handle)
{
	printf("      Host : %s\n",handle->host);
	printf("      User : %s\n",handle->user);
	printf("  Password : %s\n",handle->passwd);
	printf("  Database : %s\n",handle->db);
	printf("ERROR %4d : %s\n", mysql_errno(handle->mysql), mysql_error(handle->mysql));
	handle = FreeMyDB(handle);
	exit(10);
}

MyDB *FreeMyDB(MyDB *handle)
{
	mysql_close( handle->mysql );
	free(handle->mysql);
	Free(handle->host);
	Free(handle->user);
	Free(handle->passwd);
	Free(handle->db);
	Free(handle);
	return(NULL);
}


/* 用完后使用mysql_free_result()释放res
   调用mysql_fetch_row()从结果集合中取出行
       mysql_row_seek()从结果集合中获得当前的行位置。
       mysql_row_tell()在设置当前的行位置。

 */
MYSQL_RES *MySqlStoreResult(MyDB *handle)
{
	MYSQL_RES *res;

	res=mysql_store_result(handle->mysql);
	if( mysql_field_count(handle->mysql) && res==NULL )	ErrorMyDB(handle);

	return(res);
}

⌨️ 快捷键说明

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