wxSQLite3 error Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

wxSQLite3 error

Post by xskater11x » Wed Jan 31, 2007 12:30 am

I get an unhandled wxSQLite3Exception when i have the following code execute.

Code: Select all

	int Database::CheckPlayerPassword(wxString name, wxString givenPass)
		{
			wxSQLite3ResultSet player;
			wxSQLite3StatementBuffer query;
			wxString dbPass;

			query.Format("SELECT pass FROM players WHERE name = '%q'", name);

			if (IsCorrectSyntax(query))
			{
				player = db.ExecuteQuery(query);
				dbPass = player.GetString("pass", wxEmptyString);

				if (givenPass == dbPass)
				{
					return 1;
				}

			} else {

				const wxString error = _T("Invalid syntax.");
				throw wxSQLite3Exception(WXSQLITE_ERROR, error);

			}

			return 0;
      } 
Any clue what cold be wrong? IsCorrectSyntax() and Query() just pass through the built in functions, nothing extra added.

utelle
Moderator
Moderator
Posts: 898
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxSQLite3 error

Post by utelle » Wed Jan 31, 2007 2:29 pm

xskater11x wrote:I get an unhandled wxSQLite3Exception when i have the following code execute.

[snip]

Any clue what cold be wrong? IsCorrectSyntax() and Query() just pass through the built in functions, nothing extra added.
In the code you posted here I don't see a try-catch-block, so first of all try to handle the exception. Include a try-catch-block in your code - something like the following - the error message of the exception object will usually give you a hint what's going wrong.

Code: Select all

try
{
  // your code goes here
}
catch (wxSQLite3Exception& e)
{
  cerr << e.GetErrorCode() << ":" << (const char*) (e.GetMessage().mb_str()) << endl;
}
One problem might be, that you don't call the method NextRow of the result set object and that the query returns an empty result set. Your code does not handle this case. As noted in the wxSQLite3 API documentation you have to call the method NextRow before accessing the data of a row of the result set. Add code like the following after executing the query:

Code: Select all

if (player.NextRow())
{
  // at least one row was retrieved by the query
  // process the data of the first row
}
else
{
  // the result set is empty
}
Regards,

Ulrich

xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Post by xskater11x » Wed Jan 31, 2007 8:19 pm

Now it just locks when it hits this code, no error reported or anything.

Code: Select all

		int Database::CheckPlayerPassword(wxString name, wxString givenPass)
		{
			wxSQLite3ResultSet player;
			wxSQLite3StatementBuffer query;
			wxString dbPass;

			query.Format("SELECT pass FROM players WHERE name = '%q'", name);

				try
				{
					if (IsCorrectSyntax(query))
					{
						player = Query(query);
						if (player.NextRow())
						{
							dbPass = player.GetString("pass", wxEmptyString);

							if (givenPass == dbPass)
							{
								return 1;
							} else {
								return 0;
							}

						} else {

							const wxString error = _T("No data in player file.");
							throw wxSQLite3Exception(WXSQLITE_ERROR, error);

						}
					} else {

						const wxString error = _T("Invalid syntax.");
						throw wxSQLite3Exception(WXSQLITE_ERROR, error);

					}
				}
				catch(wxSQLite3Exception& e)
				{
					std::cerr << e.GetErrorCode() << ":" << (const char*) (e.GetMessage().mb_str()) << std::endl;
				}

utelle
Moderator
Moderator
Posts: 898
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Post by utelle » Wed Jan 31, 2007 9:55 pm

xskater11x wrote:Now it just locks when it hits this code, no error reported or anything.
Without seeing the whole application and without knowing your environment it's hard to tell, what's going wrong.

Probably you should use wxLogError instead of writing the exception error message to cerr.

If possible you should debug your application single stepping through your code to identify where exactly it hangs and for what reason.

Regards,

Ulrich

xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Post by xskater11x » Thu Feb 01, 2007 2:01 am

I know for a fact it is hanging on the query itself, but I have no clue why it is doing so. I also cannot run in debug mode due to Python embedding, python24 does not come with a debug library, rendering debug compilation impossible without removal of the python scripting. So I have been going off the little amount of debug information release build gives out. Stepping through via comments showed me the query is not properly working though. The same query executed via the sqlite3 console application returned the correct value though.

utelle
Moderator
Moderator
Posts: 898
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Post by utelle » Thu Feb 01, 2007 5:27 pm

xskater11x wrote:I know for a fact it is hanging on the query itself, but I have no clue why it is doing so.

I guess it has something to do how you use SQLite in your application. Since I know only the small bit of code you posted here, it's hard to tell what's going wrong.

Maybe your application tries to access the database from different threads. You definitely can't use the same instance of wxSQLite3Database from more than one thread. Please read http://www.sqlite.org/faq.html#q8 for further details of SQLite's threadsafety.
xskater11x wrote:I also cannot run in debug mode due to Python embedding, python24 does not come with a debug library, rendering debug compilation impossible without removal of the python scripting.

In that case the only way to debug the application will be adding the output of messages to a file for example at various points in the application.
xskater11x wrote:So I have been going off the little amount of debug information release build gives out. Stepping through via comments showed me the query is not properly working though. The same query executed via the sqlite3 console application returned the correct value though.
What do you mean with "not properly working"? In your first posting you told that you get an unhandled exception. After adding exception handling code you told your program "hangs". This is weird. I would have expected you would actually "see" the exception message.

If your application really hangs then my guess is you have a concurrency problem.

Regards,

Ulrich

xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Post by xskater11x » Fri Feb 02, 2007 1:15 am

thank you for all your help and being patient with me thus far.

I have narrowed it down to a syntax error with the query:

Code: Select all

query.Format("SELECT pass FROM players WHERE name = '%q'", name);
when passed to CheckSyntax it returns false. Can yous ee anything possibly wrong with the code?

As for what else goes on, the Db is opened inside of the class right before this function is called, it is closed right after this function. The Db loads, and all the data is, as far as i can tell, there.

utelle
Moderator
Moderator
Posts: 898
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Post by utelle » Fri Feb 02, 2007 10:14 am

xskater11x wrote:I have narrowed it down to a syntax error with the query:

Code: Select all

query.Format("SELECT pass FROM players WHERE name = '%q'", name);
when passed to CheckSyntax it returns false. Can you see anything possibly wrong with the code?
Yes. Sorry for not seeing it earlier. In SQLite an SQL statement must be terminated with a semicolon. The CheckSyntax method just checks whether this is the case. So add a terminating semicolon to your SQL statement. Then it should work, hopefully.

Code: Select all

query.Format("SELECT pass FROM players WHERE name = '%q';", name);
Regards,

Ulrich

xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Post by xskater11x » Sat Feb 03, 2007 5:01 am

thank you so much. Helped me fix a lot of problems and answered a lot of questions.

After fixing it, I got players didn't exist, for future reference U fixed that be removing the leading slash from the filename.

Post Reply