Firedac select working with Firebird returns no records - delphi

Hello I'm working with Firedac (Delphi Seattle) using Firebird (2.5) as a database, when I run this query using a TFDQuery, no records are returned:
SELECT ID FROM USERS WHERE PWD = 'êHÆ–!+'
The same query within a Database program as IbExpert return one record. Is there some parameter with Firedac components to configure that can solve this issue. Thanks.

It's in the query string and it's the ! char. By default, query strings are preprocessed, and you must escape constant chars like !, &, :, ?, { or }, otherwise they are used as special chars.
Your best option is using parameters. That will (except other benefits) get rid of that ! char from the preprocessed command:
FDQuery.SQL.Text := 'SELECT ID FROM USERS WHERE PWD = :Password';
FDQuery.ParamByName('Password').AsString := 'êHÆ–!+';
FDQuery.Open;
Another option is escaping that constant char or disable macro preprocessor. For more information see the Special Character Processing topic.

Related

Sybase ASE: show the character encoding used by the database

I am working on a Sybase ASE database and would like to know the character encoding (UTF8 or ASCII or whatever) used by the databae.
What's the command to show which character encoding the database uses?
The command you're looking for is actually a system stored procedure:
1> sp_helpsort
2> go
... snip ...
Sort Order Description
------------------------------------------------------------------
Character Set = 190, utf8
Unicode 3.1 UTF-8 Character Set
Class 2 Character Set
Sort Order = 50, bin_utf8
Binary sort order for the ISO 10646-1, UTF-8 multibyte encodin
g character set (utf8).
... snip ...
From this output we see this particular ASE dataserver has been configured with a default character set of utf8 and default sort order of binary (bin_utf8). This means all data is stored as utf8 and all indexing/sort operations are performed using a binary sort order.
Keep in mind ASE can perform character set conversions (for reads and writes) based on the client's character set configuration. Though the successfulness of said conversions will depend on the character sets in question (eg, a client connecting with utf8 may find many characters cannot be converted for storage in a dataserver defined with a default character set of iso_1).
With a query:
select
cs.name as server_character_set,
cs.description as character_set_description
from
master..syscharsets cs left outer join
master..sysconfigures cfg on
cs.id = cfg.value
where
cfg.config = 131
Example output:
server_character_set character_set_description
utf8 Unicode 3.1 UTF-8 Character Set

Update a column name that starts with a number in a dbf-file with oledb

I'm trying to update a column named value in a dbf-file. Since value is a reserved word I need to add the tablename to the query. So this works:
UPDATE "C:\TEMP\TEST_PARAM.DBF" SET TEST_PARAM.value='new value' WHERE Id='Some ID';
But now I have the problem that many of my dbf-files start with numbers in the filenames and following does not work:
UPDATE "C:\TEMP\0016_PARAM.DBF" SET 0016_PARAM.value='new value' WHERE Id='Some ID';
I've tried enclosing the table_name in single quotes, double quotes, [,... but nothing of that works. Is there something else I could try?
You don't say what language you're doing this in, but here we go in C#. The same approach should work in any language.
You need to open the DBF under an alias, and you need to be able to send multiple commands through OLEDB.
This should work.
* -- Open the dbf in the first available work area under the alias 'param'.
* -- Now you don't have to worry about the zeroes.
OleDbCommand myCommand = new OleDbCommand(#"execscript([use 'C:\TEMP\0016_PARAM.DBF' in 0 alias param])", myConnection);
var result = myExecuteNonQuery();
* -- Now do the update, referring to the alias, not the DBF filename.
myCommand = new OleDbCommand(#"execscript([update param set param.value='new' where id='some id'])", myConnection);
result = myCommand.ExecuteNonQuery();
Regarding the square brackets, Visual FoxPro has three default string delimiters, namely the usual single and double quotes, but also square brackets.
So we're using the double quotes for the C# string. The Visual Foxpro command we're running via ExecScript needs quotes too, around 'new' and 'some id', so that's using single quotes. But we need to pass that command to Execscript as a string, so that string is using the brackets.

Figuring out what this Trim() function is doing?

I have a constraint on a transformer with this:
Trim(CollectFrom.collect_from,"-","A")<=TheDate
Here is what collect_from looks like:
'2017-02-27'
And here is what TheDate looks like:
'20170227'
I am unsure exactly how this Trim() function works. My first guess is that it is giving it the same format as 'TheDate' however I don't understand the "A" argument. Could somebody explain it?
The manual page for TRIM() says that shouldn't work.
When I try to run what you show, I get errors:
SQL[2405]: select trim('2017-02-01', '-', 'A') from dual;
SQL -674: Routine (trim) can not be resolved.
SQLSTATE: IX000 at /dev/stdin:1
SQL[2406]: select trim('2017-02-01', '-') from dual;
SQL -674: Routine (trim) can not be resolved.
SQLSTATE: IX000 at /dev/stdin:2
The manual says you need TRIM({BOTH|LEADING|TRAILING} [char] FROM source):
SQL[2407]: select trim(both '-' from '2017-02-12') from dual;
2017-02-12
SQL[2408]: select trim(both '-' from '2017-02-12-') from dual;
2017-02-12
SQL[2409]: select trim(both '-' from '-2017-02-12-') from dual;
2017-02-12
SQL[2410]:
(The SQL command interpreter used is my SQLCMD.)
There's a chance that someone has defined a TRIM function that takes three arguments — that's not detectable from here. You'd have to look in the system catalog of your database to find that.
OTOH, that doesn't seem to be allowed, either:
SQL[2411]: create function trim(a varchar(10), b varchar(20), c varchar(30)) returning varchar(64);
return trim(leading from a) || ' ' || trim(both from b) || ' ' || trim(trailing from c);
end function;
SQL -9710: Overloading of built-in functions is not allowed.
SQLSTATE: IX000 at /dev/stdin:8
SQL[2412]:
Informix 11.50 has build in trim() function: https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.sqls.doc/ids_sqs_1556.htm But it looks little different from trim() in your question, so I think you use trim() function build by some user.
Normally trim() takes care only at chars at beginning or at end of the string, but in your example trim() had to remove - chars that are in the middle of the string. I guess that last argument A tells trim() to remove all such characters from source string.
To find out what trim() function really do you must find it source. You can do it with some GUI database tools like SQirreL SQL Client (it uses JDBC), you can do it with dbschema Informix utility or with my Python/Jython program that uses ODBC/JDBC: https://code.activestate.com/recipes/576621-dump-informix-schema-to-text
It would be helpful to tell us what version of Informix you use.
Trim(CollectFrom.collect_from,"-","A")
Here A means to Remove all the occurrences of '-' from the input string CollectFrom.collect_from . Hence You are getting '20170227' . Here is the document which would give further more information on Trim function.
https://www.ibm.com/support/knowledgecenter/en/SSZJPZ_11.5.0/com.ibm.swg.im.iis.ds.parjob.dev.doc/topics/r_deeref_String_Functions.html

Anydac TADTable component collation issue

I'm having an issue with sorting strings that have special characters like ^ and ! in a Firebird database.
When using the TADTable component with the following settings and a table that uses collation unicode_ci_ai
CachedUpdates := false;
FetchOptions.Unidirectional := false;
FetchOptions.CursorKind := ckAutomatic;
FetchOptions.Mode := fmOnDemand;
FormatOptions.SortOptions := [soNoCase];
The server will put strings that start with ^ before strings that start with !, but TADTable does the opposite. This results in duplicates when bringing down the records.
I'm looking for best practice when sorting strings with special characters. I have to use TADTable (legacy system) and Live Data Window mode for speed.
Thank you.
This is most likely to do with the database connections having different default character encoding. See Firebird Character Sets and Collations

Entity framework with Firebird throws dynamic SQL error

I've got stuck with FbException
SQL error code = -104
Token unknown - line 2, column 4
.
when trying to run this code
var result = from x in _context.Bunts
select x;
I've checked the query which was produced by EF
SELECT
"A"."BUNTCODE" AS "BUNTCODE",
"A"."BUNTNAME" AS "BUNTNAME",
"A"."BUNTDIAM" AS "BUNTDIAM"
FROM "BUNTS" AS "A"
So server thinks that something is wrong with dot after "A" statement. But this query runs just fine in IBExpert on the same machine. How to fix this problem?
I'm using:
Firebird server v2.1.6.18547
EntityFramework v6.0.0.0
EntityFramework.Firebird v4.5.2.0
FirebirdSql.Data.FirebirdClient 4.5.2.0
The error suggests you are connecting using dialect 1. Dialect 1 is the old dialect of Interbase 5 and earlier and should be considered deprecated (although unfortunately 15 years on it is still supported by Firebird...).
In dialect 1 it is not possible to quote object names, and double quotes are used for strings (instead of single quotes in dialect 3 and the SQL standard). When your query is parsed in dialect 1, Firebird sees "A" as a string constant, and the following dot (.) is not expected by the parser.
Switching to dialect 3 should fix this, however if you do that, make sure that your database itself is also dialect 3, otherwise you might get other unexpected behavior like certain datatypes not working, or errors, etc.

Resources