Special characters and empty fields from database

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

Added row fields from an sql database in a listview and some fields contain names with a single quote, like Cetti's.
Values like this don't give any result with the next ExecQuery.
Special characters like é, ï and ñ work fine.

The database also contains several empty fields.
How can I skip those lines from being added to a Listview? (Using "laguages" as a var)

If Cursor.GetString(languages).Lenght > 0 Then
lvNames.AddSingleLine(Cursor.GetString(languages)
End if

Thanks,
Helen.
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Added row fields from an sql database in a listview and some fields contain names with a single quote, like Cetti's.
Values like this don't give any result with the next ExecQuery.

I had the same problem and came up with this:

Your query looks like this to sqlite:
SELECT * FROM table WHERE Name = 'Cetti's'​
where the apostrophe in Cetti's is taken to be the end of the Name.

Instead of using apostrophes (single quotes) as delimiters around the word, use double quotes:
SELECT * FROM table WHERE Name = "Cetti's"​

The B4A code would look like this:
nm = "Cetti's"
Cursor = SQL1.ExecQuery("SELECT * FROM table WHERE name = " & QUOTE & nm & QUOTE)​
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User

Thanks. That is very helpful. I thought that "Array" meant that you to pass an array rather than a single parameter.

I assume that the main advantage over the method I posted is that in my method, if the parameter contains quotes, then using quotes as a delimiter as I did will still cause a problem (which I didn't consider) while passing the field separately eliminates any conflict between the delimiter and the contents of the parameter. Is that right?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I thought that "Array" meant that you to pass an array rather than a single parameter.
That is correct. In this specific case we have an array with one item. You can however have several question marks in your query and then you can pass an array with several items.
This is one advantage. It also makes your code simpler as it easier to combine the variables in the query string. And it also protects against SQL injections which can happen if you take some string value from an untrusted source and directly add it to your query.
 
Upvote 0

Merlot2309

Active Member
Licensed User
Longtime User
Hello gentlemen,

Brillant :sign0060:. The little app runs good and stable now.

To get it working I already changed cursor.GetString to cursor.GetString2 and selected the specific columns from the db.

The part of the code that may help others:


Thank you for your help,
Helen.
 
Upvote 0

francoisg

Active Member
Licensed User
Longtime User
Hi,
also you can do something like the following (replace single quote chars with two single quote chars) ...

dim mysql as SQL
dim s, v as string
v = "My 'test' string"
' *** Replace single ' charcater with double ' caracters ...
s = "insert into table values ('" & v.Replace("'", "''") & "')"
mysql.Initialize(File.DirDefaultExternal, "dbname.db", true)
mysql.ExecNonQuery(s)
.
.
.


Regards,
M. le Roux
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…