[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mysql-plusplus
Subject:    C++ API not returning correct value (in one very specific spot)
From:       "Adam Clauss" <cabadam () houston ! rr ! com>
Date:       2003-10-25 3:38:15
[Download RAW message or body]

OK, let me tell you about my database that pertains to this problem.  I have
two tables, Categories and Items.
Categories consists of 3 fields, Category (text), Subcategory (text), and
catID (autoincrementing integer).

Items, among other fields, consists of an autoincrementing itemID and a
catID relating the item to the category.

I go through reading in items from a text file, adding each item into the
table as I go.  Whenever I need to set/add a category, I first check to see
if the specified category/subcategory pair exists.  If it does, I just take
its catID and set the new item's catID to it.
If the category does not exist, I create it and use LAST_INSERT_ID to
retrieve the associated catID and set the new item's catID to this.

Its when the category already exists that I am running into trouble.
I have using the following:

<snip>
CString query;
query.Format("SELECT catID FROM categories WHERE category=\'%s\' AND
subcategory=\'%s\'", newCategory, newSubCategory);
if (sqlCon->ExecuteQuery(query))
{
	CString catID;
	if (sqlCon->res.size() == 1)
	{
		//if this category already exists, just set the catID to it
		/**************** RIGHT HERE ****************/
		catID = CString(sqlCon->res[0][1]);
	}
	else //otherwise we have to create it
	{
		query.Format("INSERT INTO Categories (category, subcategory)
VALUES(\'%s\',\'%s\')", newCategory, newSubCategory);
		if (sqlCon->ExecuteQuery(query))
		{
			query = "SELECT LAST_INSERT_ID()";
			if (sqlCon->ExecuteQuery(query))
				catID = sqlCon->res[0][0];
		}
</snip>

sqlCon is a small wrapper I wrote around the SQL API (makes simple calls
into the API, nothing in depth).  For example, the ExecuteQuery consists of:
<snip>
Query query = con.query();
query << q;
try
{
	res = query.store();
}
catch (BadQuery er2)
{
	er = er2;
	return false;
}
return true;
</snip>


The problem is that the catID is being returned as:
"ллллллллю■ю■ю■ю■"
The correct answer I simply a number (small at this point, usually 1 or 2).

If I dig down into the Result object, through its members into the data, I
can SEE the number:

sqlCon->res	{...}
-	MysqlResUse	{...}
		mysql_res	0x01369208	st_mysql_res *
			row_count	1	unsigned __int64
			-	data	0x01369fa8	st_mysql_data *
				rows	1	unsigned __int64
				fields	1	unsigned int
				-	data	0x01353ec0
st_mysql_rows *
					-	data	0x01353ec8	char
* *
						+		0x01353ed0
"1"	char *


That one down there at the end is the correct result.  Any ideas why it
might be getting returned incorrectly through the [] operators?
I have many other SQL calls throughout the application that all seem to be
working just fine.
Adam Clauss
cabadam@tamu.edu


-- 
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe:    http://lists.mysql.com/plusplus?unsub=mysql-plusplus@progressive-comp.com


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic