www.pudn.com > openwebspiderv0.1a.zip > sqlfnct.h



/* OpenWebSpider
 *
 *     Coded by Shen139
 *     shen139 [at] eviltime (dot) com
 *
 *
 * This file is part of OpenWebSpider
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef __SQLFNCT
#define __SQLFNCT
#include 

#ifdef WIN32
#include "sqllib.h"
#endif

#ifdef linux
#include 
#endif


int sqlConnect(char* hostname, char* username, char* password, char* table,MYSQL* rMysql)
{
MYSQL mysql;

	mysql_init(&mysql);
	mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"OpenWebSpider");

	if (!mysql_real_connect(&mysql,hostname,username,password,table,0,NULL,0))
	{
		memcpy(rMysql,&mysql,sizeof(mysql));
		return 0;
	}

	memcpy(rMysql,&mysql,sizeof(mysql));

return 1;
}

int sqlSendSqlQuery(MYSQL* mysql,char* query, MYSQL_RES* result)
{
MYSQL_RES* mRes;
	if(mysql_query(mysql, query))
		return -1;
	else
	{
		mRes=mysql_store_result(mysql);
		if (mRes)
			 memcpy(result,mRes,sizeof(MYSQL_RES));
		else
			return 0;
	return 1;
	}
}

int sqlCreateDB(char* hostname, char* username, char* password, char* dbname)
{
MYSQL mysql;
MYSQL_RES result;
char sqlQuery[MAXQUERYSIZE];

	if(sqlConnect(hostname,username,password,NULL,&mysql)==0)
	{
		fprintf(stderr, "Failed to connect to database: Error: %s\n",mysql_error(&mysql));
		return 0;
	}

	sprintf(sqlQuery,"create database %s;",dbname);
	sqlSendSqlQuery(&mysql, sqlQuery, &result);

	if(sqlConnect(hostname,username,password,dbname,&mysql)==0)
	{
		fprintf(stderr, "Failed to connect to database: Error: %s\n",mysql_error(&mysql));
		return 0;
	}

	sqlSendSqlQuery(&mysql, "create table wordlist(wordID bigint auto_increment, word char(30), primary key(wordID));", &result);
		
	sqlSendSqlQuery(&mysql, "create table urllist (urlID bigint auto_increment, hostname TEXT, page TEXT, description TEXT, primary key(urlID));", &result);
	
    sqlSendSqlQuery(&mysql, "create table mid (wordID bigint, urlID bigint);", &result);
	
return 1;
}

int GetUrlIDfromHostPage(MYSQL* mysql, char* Hostname, char* Page, char* urlID)
{
char sqlQuery[MAXQUERYSIZE];
MYSQL_RES result;
MYSQL_ROW row;


	sprintf(sqlQuery,"SELECT * FROM urllist where hostname=\"%s\" AND page=\"%s\";",Hostname,Page);
	if(sqlSendSqlQuery(mysql, sqlQuery, &result)==1)
	{
		if((row=mysql_fetch_row(&result))==NULL)
			return 0;
		else
		{
			strcpy(urlID,row[0]);
			return 1;
		}
	}
	else
		return 0;

}
#endif