[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: MySql and C



Hi,
Raphaël Barbate [barbate@enst.fr] wrote:
> 	That's why I ask you if you could send me an
> example (.c), which connects to mysql and performs
> some queries.

Here are some examples:

// Extablish a tcp connection with the SQL database
	int connectSQL()
	{
		MYSQL   *tmp;

		tmp = mysql_connect(&sqlHandle, SQL_HOST, SQL_USER, SQL_PASS);

		if (tmp == NULL)
                return 0; // failed to establish connection with the SQL server

		return (!mysql_select_db(&sqlHandle, SQL_DB));
	}

// below are for queries
        sprintf(query_string,
        "INSERT INTO tran_request VALUES(\'%16s\', \'%20s\', \'%8s\', \'%1s\', \'%4s\', \'%20s\', \
        \'%4s\', \'%12s\', SYSDATE())", \
        TxnReference, login, ClientID, TransactionType, AccountNumber, \
        CardData, CardExpiryDate, TotalAmount);

        query = mysql_query(&sqlHandle, query_string);
        if (query) {
                error(query_string, NO_EXIT);
                error("Error when updating the database for transaction request, \ 
		see above query string for deails", EXIT);
        }


// using the fetched queries
        sqlResult = mysql_store_result(&sqlHandle);
        sqlRow = mysql_fetch_row(sqlResult);

        mysql_free_result(sqlResult);

        if (*sqlRow == NULL)
		blah and so on....

	Hope this helps...

Shao.

-- 
____________________________________________________________________________
Shao Zhang - Running Debian 2.1  ___ _               _____
Department of Communications    / __| |_  __ _ ___  |_  / |_  __ _ _ _  __ _ 
University of New South Wales   \__ \ ' \/ _` / _ \  / /| ' \/ _` | ' \/ _` |
Sydney, Australia               |___/_||_\__,_\___/ /___|_||_\__,_|_||_\__, |
Email: shao@cia.com.au                                                  |___/ 
_____________________________________________________________________________


Reply to: