I have a table with TEXT, INTEGER, REAL and other data types.
I wrote a generic sql function to read results for all queries using sqlite3_column_text() like so:
char *dataAsChar = (char *) sqlite3_column_text(compiledStatement, ii);
Although I should be using sqlite3_column_int etc. to read the numeric values, the above code seems to work for me. I get the number values as string which I later convert to int using [*numberAsString* intValue].
Since I am using a generic function to read all my db values, this is very convenient for me. But is there something that can go wrong with my code?
I could use sqlite3_column_type for each column to determine the type and use appropriate function. Am I correct in assuming that sqlite3_column_text basically returns the column value in TEXT format and does not necessarily need the value itself for be TEXT?
The only situation where I can see this implementation failing is with BLOB data type.
The documentation says:
These routines attempt to convert the value where appropriate. […]
The following table details the conversions that are applied:
Internal Type Requested Type Conversion
NULL TEXT Result is a NULL pointer
INTEGER TEXT ASCII rendering of the integer
FLOAT TEXT ASCII rendering of the float
BLOB TEXT Add a zero terminator if needed
Related
I have an app UI that expects a HEX value e.g. foo = 0x113
I'm doing this in Lua to try to write to foo:
menu.set("Presets", "foo", "0x318")
menu.set("Presets", "888x", "-258")
menu.set("Presets", "89ab", "-60"
The values for 888x and 89ab in the app are set. The HEX value field remains empty. Could someone help please? Thanks.
There is no such thing as an hex value. There are numbers expressed in hex.
So your API expects a number. No wonder "0x318" does not work. The other two work because the strings are convertible to numbers.
Bottom line: use menu.set("Presets", "foo", 0x318).
I am trying to store the ComboColorBox.Color property in a database field through LiveBindings in a Firemonkey Project. I want to store the hexadecimal AlphaColor value, but the hex color value is automatically converted to a decimal value and not an AlphaColor.
I've searching in the LiveBindings documentation and found that I can change the value of control property before is stored in database writing a binding expression in CustomParse property of the Binding. The problem is that there is no built in function to convert an AlphaColor to a String in an expression.
Do I have to write a custom function to do that? How do I write that function and where? Or there is another solution?
You should know that a TAlphaColor is an integer, or more precisley a cardinal as defined in System.UITypes
type TAlphaColor = Cardinal;
Thus you can apply any integer to string conversion functions, like:
Label1.Text := IntToStr((Sender as TComboColorBox).Color); // decimal notation
Label2.Text := IntToHex((Sender as TComboColorBox).Color, 8); // hexadecimal notation
So, to store the TAlphaColor value as a hexadecimal string, you would convert the color value using IntToHex(). On the other hand, Are you sure you really want to store it in the db as a string in the first place.
Here's what happens:
Internal database stuff: one class has a string property on it, that stores a phone number. This number is set using the code
CFBridgingRelease(ABMultiValueCopyValueAtIndex(ABRecordCopyValue(record, kABPersonPhoneProperty), 0));
My function: finds all objects of this type, and stores phone numbers of each object in an NSMutableSet.
Debug: I print the description of the set to the console.
Results:
Some of the set's objects look as expected (the majority actually): "+64 27 0124 975"
Some are missing quotation marks: 027 7824 565
Some have weird unicode symbols: "021\U00a0026\U00a017788"
My question:
Why the difference - what does it mean, and do I need to fix anything?
NSLog with %# – as I assume you are using – has some intelligence in how it presents NSStrings as it calls the description method. If the string has anything other than alphanumerics, such as the '+' or '\' above, it will use quotes. The string with unicode characters simply has its characters encoded as shown, and they are automatically converted into this lossless format. You should be able to convert it to something prettier for the console if you really need to with something like this:
NSLog(#"%#", [NSString stringWithCString:[myString.description cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSNonLossyASCIIStringEncoding]);
I'm using Delphi XE2 and use the following code to enter the letter Y into a bookmark in a Word (2010) template.
Doc.Bookmarks.Item('NS').Range.InsertAfter('Y');
Except in the document, instead of the letter Y, the number 89 appears.
Is the fault likely to be from my code or in the Word document? Any direction gratefully received.
Your literal 'Y' is a character literal rather than a string string literal. The ASCII code for Y is 89.
So, you are passing a Char rather than a string. When Word needs to get a string representation of that integer it simply converts the integer 89 to its textual representation, the string '89'.
To get around the problem you can do this:
var
Text: string;
....
Text := 'Y';
Doc.Bookmarks.Item('NS').Range.InsertAfter(Text);
The idea is that we ensure that we pass a string to InsertAfter() rather than a character. Remember that InsertAfter() receives a variant parameter and so you do need to be careful about the type of the payload stored in the variant.
why we have to use cast or cnvert function in 2nd and 3rd selecct statement, and what we achieve by casting or converting it to varchar, what varchar data type do in it exactly?
DECLARE #mybin1 binary(5), #mybin2 binary(5)
SET #mybin1 = 0xFF
SET #mybin2 = 0xA5
-- No CONVERT or CAST function is necessary because this example
-- concatenates two binary strings.
SELECT #mybin1 + #mybin2
-- A CONVERT or CAST function is necessary because this example
-- concatenates two binary strings plus a space.
SELECT CONVERT(varchar(5), #mybin1) + ' '
+ CONVERT(varchar(5), #mybin2)
-- Here is the same conversion using CAST
SELECT CAST(#mybin1 AS varchar(5)) + ' '
+ CAST(#mybin2 AS varchar(5))
Casting it to a VarChar converts the data from a sequence of bytes to character text. As Flip says, you can't directly concatentate the different types. Typically binary strings are for images, videos, streams, etc, where as varchar is usually string data like what is found in a text file.
because in your select statement you are concatenating the the variables with space(' ') which is a type of varchar to you need to cast them to get the desired results.
in a sense you it might give you an error because your data types are different/incompatible.
Since they are different data types, you can't directly concatenate binary data and text strings (varchar), which is why you need either the CAST or the CONVERT.