Quote character in mysql search string

Post

Posted
Rating:
#1 (In Topic #158)
Regular
bill-lancaster is in the usergroup ‘Regular’
It's a long time since I did this:-
sNew is a string variable that may contain a "'" character.

Code (gambas)

  1. hResult = hConn.Exec("SELECT * FROM gCal WHERE EventText = &1", sNew)

But I always get a result with no records.

No doubt I'm missing something obvious but any ideas would be welcome
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘Regular’
I hate this quoting problem. It comes up from time to time and I never seem to remember what I did last time.

In an example where I was running a Gimp script I did this:-

Code (gambas)

  1.     strExt = Quote("*.[jJ][pP][gG]")
  2.     strCommand = "cd " & DirView1.Current & " && gimp -i -b '(scale-batch " & strExt & ")' -b '(gimp-quit 0)'"
  3.     lblStatus.Text = "Please wait..... " & index & " image files found"
  4.     Wait
  5.     Shell strCommand Wait
  6.  

So you may need to build the contents of sNew using Quote(sSomething) for the quoted part. Can we see an example of contents of sNew?
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
bill-lancaster is in the usergroup ‘Regular’
Exactly!
Here is the offending string:-

Code (gambas)

  1. John Lancaster's birthday
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
stevedee is in the usergroup ‘Regular’
OK, so it looks like your quoting problem is with SQL not Gambas (i.e. you need to quote to suit SQL rules).

Take a look at this post and let us know if it helps: SQL to Query text in access with an apostrophe in it - Stack Overflow
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
bill-lancaster is in the usergroup ‘Regular’
Ah yes!
The problem was with a record in the db that contained a single (').
Using Gambas Replace function in the SQL statement solves the problem.

Code (gambas)

  1. SELECT * FROM gCal WHERE EventText = '" & Replace(sNew, "'", "''") & "'"

Thanks for the help.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
jornmo is in the usergroup ‘Regular’
You can use this: /comp/gb.db/_connection/quote - Gambas Documentation

I think there's a bind parameters, or something like that too, or I'm confusing with PHP :)

Online now: No Back to the top
1 guest and 0 members have just viewed this.