I have a MySQL InnoDB table, one of the columns is defined as double and have stored a number 16155.987841701322 but if I perform a query from VB.NET returns
16155.9878417013
From MySQL Workbench I can see all the digits in the DB, but from VB:NET i get two digit less.
Why didn't return all the digits?
I make a little code to perform conversion test
Dim OdometerDbl As Double = 16155.987841701322
Dim OdometerStr As String
OdometerStr = CStr(OdometerDbl)
After this I get 16155.987841701322 in OdometerDbl and 16155.9878417013 in OdometerStr; so is a VB.NET Double to String conversion issue.
Theres any way to get around this or a better way to convert a Double to String without losing decimals?
From the example above
OdometerStr = OdometerDbl.ToString("R")
or
OdometerStr = OdometerDbl.ToString("G17")
References:
Why Is ToString() Rounding My Double Value?
VB.NET Converting Double Value to String = Precision loss
Related
I have a table called messages with a column (BLOB) called message_text. I'm passing a value from the web app when a user sends a new message and I want to check if it's an exact duplicate message text.
SELECT count(message_id) FROM messages WHERE message_text = '$msgTxt' AND user_id = $userId
Where $msgTxt will be a formatted string like...
"Hello there. I don\'t know you.
I\'ve just made a new line. "
The problem is that the comparison isn't working and I'm never finding duplicates. Even if I literally copy/paste an existing value from the database and replace it with $msgTxt in my query I never get any results, and so I'm assuming there's something wrong with the way I'm comparing a blob to a string.
BLOB values are treated as binary strings (byte strings). They have the binary character set and collation, and comparison and sorting are based on the numeric values of the bytes in column values. String or Text values are treated as nonbinary strings (character strings). They have a character set other than binary, and values are sorted and compared based on the collation of the character set.
So, you have to convert either BLOB to String or String to BLOB and then compare both.
If you are using java,
Convert Blob to String
byte[] bdata = blob.getBytes(1, (int)blob.length());
String data1 = new String(bdata);
What API are you using to call MySQL? I see some backslashes, but need to verify that \ is not turning into \\, and that other escapings are not unnecessarily happening or not happening.
Which OS are you using? Windows, when reading stuff, likes to convert NL into CRLF, thereby making it so that it won't match.
I have a MySQL database (5.6 Community) with a column of type bit(60). The field holds values such as 1001, 0011, etc. I am trying to pass a string of "1010" to the database through a vb.net adapter. If I use a regular query it would look like this:
insert into my_table (my_bit_field) values (b'1010');
This works and inserts the string exactly as shown but I need to use a data adapter so I can't send the query directly.
When using the data adapter in vb.net, I was getting an error saying that it was expecting a byte array. So I tried using that:
System.Text.Encoding.ASCII.GetBytes("1010")
but it just converted it to its ASCII representation of bytes (49,48,49,48).
Is there a better way to go through the data adapter and is there a way to do this?
Thanks.
In this case, you could try the following to convert your string to a byte array:
Dim bytes() As Byte = { Convert.ToByte("1010", 2) }
However, that breaks once you have more than 8 bits in your string. You could (perhaps should) break the string into byte-sized sections and convert each to a byte, such as for this question. Since you have a bit(60) column you could also cheat a little and use something like this:
Dim inputValue As String = "000100000010000000110000010000000101000001100000011100001000"
If inputValue.Length > 60 Then ' up to 64 could be supported
' Error
End If
Dim longValue As ULong = Convert.ToUInt64(inputValue, 2)
Dim bytes() As Byte = BitConverter.GetBytes(longValue)
If BitConverter.IsLittleEndian Then
Array.Reverse(bytes)
End If
This will give you the byte array that you can presumably use in your data adapter code that was not shown.
I have a MS Access database with a column that has some strange encoding. Oddly, I am unable to copy/paste this into anything (Chrome, Word, etc), because it strips out most of the unicode characters (though not all of them). What I am wondering, is there a way to determine what type of encoding is being used here?
Somehow the program I am using is taking this column and decoding it to readable text. I converted the Access database to PostgreSQL on a Linux system, but I'm pretty sure whatever encoding is being used here did not map correctly into the PostgreSQL database. What I'm trying to do is to convert this to hex, but I cannot do it since I'm unable to copy/paste the characters out of the database.
You can open the table as a recordset. Then loop the records and convert the field to hex using a function like this:
Public Function StrToByte(ByVal strChars As String) As String
Dim abytChar() As Byte
Dim lngChar As Long
Dim strByte As String
abytChar() = StrConv(strChars, vbFromUnicode)
strByte = Space(2 * (1 + UBound(abytChar) - LBound(abytChar)))
For lngChar = LBound(abytChar) To UBound(abytChar)
Mid(strByte, 1 + 2 * lngChar) = Hex(abytChar(lngChar))
Next
StrToByte = strByte
End Function
Or create a query:
Select *, StrToByte([EncryptedFieldName]) As HexField
From tblYourTable
Code converts the json string to table. But my problem is, it bypass the Array Chars. So only two fields are printed on the table. It kind of skips the middle field: "Chars"
Dim JsonStr As String = "[{""Name"": ""Banana Pudding"", ""Chars"": [""abc"",""xyz""],""ID"": ""143""}]"
tb = JsonConvert.DeserializeObject(Of DataTable)(JsonStr)
GridView1.DataSource = tb
GridView1.DataBind()
Your database does not support array as a string, actually I do not know any db that do the coerce for a string to array, you have to change the value of Chars to a simple type, break it and it will work.
You may test your code with a binary field instead of string in your table, you did not mention which DBMS are you using, so I can not be more specific. but using a binary field, worth a shot, try it before breaking array to simple types.
This is more a "why" or "any pointers to documentations" question.
Today I realised that a comparison in WHERE clause takes only the first numerical part of the string if compared to an INT column. (My own conclusion)
For example:
SELECT * FROM companies c WHERE id = '817m5'
will still return companies with id = 817.
It does make sense but I'm failing to find this documented anywhere to see if my conclusion is correct and if there are additional things to know about this exact behaviour. WHERE, INT type, comparison documentation? Where? How is it called?
Thanks!
This is the comparison:
WHERE id = '817m5'
and id is an integer.
So, MySQL has to compare an integer to a string. Well, it can't do that directly. It either has to convert the string to a number or the number to the string. MySQL converts the string to a number, which it does by converting the leading numeric characters. So, the '817m5' becomes 817.
Here is the exact quote:
To cast a string to a numeric value in numeric context, you normally
do not have to do anything other than to use the string value as
though it were a number:
mysql> SELECT 1+'1'; -> 2