PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

eVB/SQL CE query

Mike Wagstaff · 14 Mar 2003, 06:24 UTC
I'm working on a small database-based eVB app (using ADOCE) for which I'm looking for a quick and easy way to retrieve records in a database of contact names based upon matching individual characters. Here are some example plain-English statements of the sort of look-ups I'm trying to do:

SELECT all records in the database WHERE the first character of CONTACT_NAME is "a"

Or...

SELECT all records in the database WHERE the third character of CONTACT_NAME is "f"

The trouble is, using SQL in CE is more limited than the MySQL commands that I'm used to and, as a result, I'm stuck. Here's what I tried...

Select * from Contacts_db where substring(Contact_name,1,1) like "a"

Select * from Contacts_db where Contact_name like "__f" (in this case, the underscore "_" character should act as a wildcard indicating the presence of 1 (any) character - but it doesn't seem to do so in CE).

Needless to say, no luck so far. As a result, any advice on this matter gratefully received!
(I've not done any SQL on CE yet, so I'm not sure how cut down it is).

Select * from Contacts_db where substring(Contact_name,1,1) like "a"


Couple of things:

If there's a substring function, you don't need the "like", and "left" is better for the left side of a string. If you were wanting to match both 'a' and 'A', try:

where where left(Contact_name,1) in ('A','a')


Also, it's convention/necessary to use single quotes around literal strings.

So, if Contacts_db is really a table, and not the database the name implies :P then the query:

Select *
from Contacts_db
where Left(Contact_name,1)='a'


Should work... if it doesn't, let me know what the error message is and I'll try to help.

Incidentally, a "select *" is bad coding practice because if your table changes, all "select *"s in your code suddenly return more columns than expected and things go "bang".

(Of course you'll need the substring function to check where the 3rd character is 'f' or whatever).
Oh, and jftr (I have verbal diarrhea this morning it seems ;)):

Select * from Contacts_db where Contact_name like "__f"


Would only match 3-letter names ending in 'f'. Try:

like '__f%'
Mike Wagstaff · 14 Mar 2003, 19:31 UTC
Thanks for the advice, and picking up on my deliberate errors. ;) Unfortunately, it doesn't like the syntax - the error message given is:

"Invalid column ID"

This suggests to me that it's trying to treat the "Left" function as a column name, i.e. "Left" isn't in its vocabulary.
Maybe it's case sensitive? Try LEFT(x,1)?
Is SQL CE downloadable or do you have to buy it?
Mike Wagstaff · 14 Mar 2003, 20:17 UTC
Nope, it's not a case sensitive issue, unfortunately. As for ADOCE, it's a component of eVB, so it's free.
Could you paste the schema for the table and the code you're running against it here? I might have time to have a play with SQL CE tomorrow.
"SELECT * FROM Contacts_db WHERE Contact_Name LIKE 'a%';" That should work no problem. I use MySQL on my website.

If it were an Oracle database, you'd need to use: "SELECT * FROM Contacts_db WHERE LOWER(Contact_Name) LIKE 'a%';" because it's case sensitive. MySQL isn't.
Mike Wagstaff · 14 Mar 2003, 21:14 UTC
Here's the SQL statement used to create the table...

CREATE table D9_contacts (Contact_name text, Home_number text, Home2_number text, Work_number text, Work2_number text, Mobile_number text, Assistant_number text)

Here's the actual eVB code used to query the database (assume that a connection has already been successfully established):

' Define necessary variables
Dim rs As ADOCE.Recordset
Dim Query_string As String

' Set SQL query
Query_string = "Select * from D9_contacts where LEFT(Contact_name,1) in ('m', 'M')"

' Query database
Set rs = CreateObject("ADOCE.Recordset.3.0")
rs.Open Query_string, conn, adOpenForwardOnly, adLockReadOnly

' <Do something useful with query results here...>

rs.Close
Set rs = Nothing


As the code above stands, I would get an "Invalid column ID" error message because of the Query_string. Thanks once again for your help, and if you need to see more code, give me a shout!
Mike Wagstaff · 14 Mar 2003, 21:19 UTC
SELECT * FROM Contacts_db WHERE Contact_Name LIKE 'a%'

Yep, that works fine. The trouble is, it's not exactly what I want.

I want to be able to query not only from the first character onwards, but on given characters. For example, I might want to find out if the second or third character in a given record is "a", "b", or "c", without specifying (or indeed knowing) what the other characters in that record are.
Mike, Try this:

Query_string = "SELECT * FROM D9_Contacts WHERE Contact_Name LIKE '%" & Search_string & "%';"

That will find anything that contains the search string. HOWEVER, if the search string contains an apostrophe, you'll have to replace it with TWO apostrophes, otherwise your Query_string is hosed.

In VBScript, the command would be:
Search_string = Replace(Search_string, "'", "''")
Not sure if it's the same in eVB.
Anyway, do that BEFORE the "Query_string =" line.

Code:

Search_string = Replace(Search_string, "'", "''")
Query_string = "SELECT * FROM D9_Contacts WHERE Contact_Name LIKE '%" & Search_string & "%';"

-John
Mike Wagstaff · 14 Mar 2003, 21:30 UTC
Incidentally, if anyone knows of any way that I can include regular expressions using ADOCE, that would solve all my problems... ;)

To try and clarify matters slightly, this code is part of a replacement dialler app for Phone Edition devices intended to imitate the name-finding system available on the SPV.

As you enter a number, the system looks at the letters associated with the keys you have pressed and works out what name you might be trying to type (similar to the text input system, T9). For example, if I wanted to find a contact called Mike, I would hit the following keys...

- 6 (m, n, o)
- 4 (g, h, i)
- 5 (j, k, l)
- 3 (d, e, f)
Mike Wagstaff · 14 Mar 2003, 21:36 UTC
SELECT * FROM D9_Contacts WHERE Contact_Name LIKE '%" & Search_string & "%'

Once again, that works fine, but I'm afraid it's still not exactly I want. I love being difficult! ;)

The "%" character acts a wildcard, meaning that it can replace any number of characters. So, searching for "%a%" would bring up:

Mark
Adam
Bella

I want to specify the exact position of the character that I'm trying to match. So for example, I might be trying to find records where the Contact_name has the second character, "a", e.g. Mark. I wouldn't want Bella to appear in the results list, however.
Cut off the first % before " & Search_string & ...

That will start everything at the left.

You'll probably need some FOR loops if you want to be too difficult. ;)
Mike Wagstaff · 14 Mar 2003, 22:11 UTC
You'll probably need some FOR loops if you want to be too difficult. ;)

Yes, but I want to be difficult and avoid having to use FOR or WHILE loops... :D
Yep, looks like you're right, ADOCE only understands the % in wildcards, not _s. Not much of a wildcard system really.
I've not really used it, but it also seems not to support even the most basic functions in where clauses. Left, substr (or mid in MS parlance), etc are no gos too. You can't even compare floats for equality. In other words, it's pretty useless.

I can't find a BNF, but take a look here:
http://msdn.microsoft.com/library/defau ... ref_91.asp

Looks like you're going to have to do your own tablescan in code :(

Just spent all afternoon fighting Oracle SQL, so couldn't resist investigating.
Mike Wagstaff · 15 Mar 2003, 02:24 UTC
Thanks for the link - hadn't seen that before. It pretty much seems to confirm that using an SQL query alone is out of the question. :(