wxsqlite3 and custom query

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
rppatil
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Aug 05, 2010 2:46 pm

wxsqlite3 and custom query

Post by rppatil »

Hi,
I am newbie with wxsqlite3.
I am trying to get data from sqlite3 database. But working with ExecuteQuery is not working perfectly.

Here is my code.

This works perfectly and my code in while loop gets executed.
sprintf(buf,"select * from analog where pt_name = 'New Analog';");


wxSQLite3ResultSet q1 = db->ExecuteQuery(buf);
while (q1.NextRow())
{
// some code ;
}

But when I run following code for variable query, I dont get desired result.

wxString pt_name contains 'New Analog';
sprintf(buf,"select * from analog where pt_name ='%s'",(const char*)ptName.mb_str());

Thanks in advance;
Any other efficient method would be appreciated.
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxsqlite3 and custom query

Post by utelle »

rppatil wrote:But when I run following code for variable query, I dont get desired result.
Could you please be more specific? Which result do you get? Does the program crash? Or is the result set empty? Or what else?
rppatil wrote: wxString pt_name contains 'New Analog';
sprintf(buf,"select * from analog where pt_name ='%s'",(const char*)ptName.mb_str());
I don't know what exactly is going wrong. Using C string functions is potentially unsafe. Why don't you use wxString?
rppatil wrote:Any other efficient method would be appreciated.
Use prepared statements and bind the value(s) to it.

Example:

Code: Select all

wxString pt_name;
wxSQLite3Statement stmt = db->PrepareStatement("select * from analog where pt_name = ?;");
// ... assign value to pt_name
stmt.Bind(1, pt_name);
wxSQLite3ResultSet q1 = stmt.ExecuteQuery();
Regards,

Ulrich
rppatil
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Aug 05, 2010 2:46 pm

Post by rppatil »

Thank you for you reply.
i will try and post result here.
rppatil
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Aug 05, 2010 2:46 pm

Post by rppatil »

Suggestion worked for me and got me new way.

Thanks
Post Reply