Serialized object to mysql database in R - mysql

If I have serialized data like:
> a <- 1:5
> a
[1] 1 2 3 4 5
> b <- serialize(a,NULL)
> b
[1] 58 0a 00 00 00 02 00 02 0f 02 00 02 03 00 00 00 00 0d 00 00 00 05 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05
> b[1]
[1] 58
> b[8]
[1] 02
How can I put that serialized data into a MySQL table? I have other info there also. I read that it can be done as blob, but I don't know how it works. I am using RMySQL. I have tried:
dbGetQuery(con, "INSERT INTO table(",b," info, moreInfo, otherStuff, more, date )")
but it won't work.
If I use
query <- paste ("INSERT INTO table(",b," info, moreInfo, otherStuff, more, date )")
dbGetQuery(con,query)
it still won't work.

try this:
library(RODBC)
dt=data.table(a=sample(10),b=sample(10)*10)
sqlSave(con, dt, tablename='sampletablename') # overwrites existing sampletablename table
sqlSave(con, dt, tablename='sampletablename', append=TRUE) # append instead of overwrite

Related

How to display an image from mysql using Express

I have a sql table 'animals' where there are blobs images. I found out how to upload images but not how to display them.
I would like to display the image which is called 'Dog' in my table.
Here is my code, where I print the result of my blob img.
let sql = 'SELECT * FROM animals WHERE file_name=\'Dog\''
connection.query(sql, (err,result) => {
console.log(result[0].img)
})
Here is the result of my code:
<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 84 00 09 06 07 13 12 12 15 13 13 13 16 16 15 15 18 18 17 16 18 15 17 15 17 17 16 ... >
Is there is any way to display that picture?
Thank you.
You can use the Fetch API to get the resource on your web page.
You can display the image like this :
fetch('http://localhost:1234/your_api')
.then(function(response) {
return response.blob();
})
.then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
document.querySelector("#databaseimage").src = objectURL;
});
In HTML :
<img id="databaseimage"/>
You can read more about Fetch API here :
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Delphi memory leak TJSONTextReader

I get memory leaks with TJSONTextReader. I more or less implemented the example from embarcadero. http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/RTL.JSONReader
Is this a bug or am I missing anything?
Content of memoryleak_example.txt
{
"products": [{
"id": "14469654611354654",
"name": "productname_xyz",
"height": 111.550000,
}],
"products_feedback": null
}
This is my example code:
var
streamreader: TStreamReader;
jsonreader: TJSONTextReader;
arrstart, objstart: boolean;
height: double;
id, name: string;
begin
// initialize or compiler warning
arrstart := false;
objstart := false;
height := 0;
streamreader := TStreamReader.Create('C:\temp\memoryleak_example.txt', TEncoding.UTF8);
try
jsonreader := TJSONTextReader.Create(streamreader);
try
while jsonreader.Read do begin
case jsonreader.TokenType of
// product object in products array
TJsonToken.StartObject: if arrstart then objstart := true;
// check for prodcuts array
TJsonToken.StartArray: if (LowerCase(jsonreader.Path) = 'products') then arrstart := true;
TJsonToken.Float:
if objstart then
if jsonreader.Path.EndsWith('height', true) then
height := jsonreader.Value.AsExtended;
TJsonToken.String:
if objstart then begin
if jsonreader.Path.EndsWith('id', true) then
id := jsonreader.Value.AsString;
if jsonreader.Path.EndsWith('name', true) then
name := jsonreader.Value.AsString;
end;
// end product object
TJsonToken.EndObject:
if arrstart and objstart then begin
objstart := false;
if id <> '' then
Memo1.Lines.Add(Format('id: %s - name: %s - height: %g', [id, name, height]));
// reset values
id := '';
name := '';
height := 0;
end;
// end of products array
TJsonToken.EndArray: if (LowerCase(jsonreader.Path) = 'products') then arrstart := false;
end;
end;
finally
jsonreader.Free;
end;
finally
streamreader.Free;
end;
end;
This is the memory leak report on shutdown:
---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block leaks are:
1 - 12 bytes: Unknown x 6
13 - 20 bytes: UnicodeString x 1
21 - 28 bytes: UnicodeString x 2, Unknown x 1
29 - 36 bytes: UnicodeString x 1
45 - 52 bytes: TList<System.JSON.Types.TJsonPosition> x 6
85 - 92 bytes: Unknown x 5
---------------------------
OK
---------------------------
I use Delphi Seattle.
Embarcadero RAD Studio 10 Seattle Version 23.0.21418.4207
I found this discussion:
https://forums.embarcadero.com/thread.jspa?threadID=118014
I don't know if this is related.
--------------------------------2018/7/13 11:48:16--------------------------------
A memory block has been leaked. The size is: 12
This block was allocated by thread 0x18B0, and the stack trace (return addresses) at the time was:
41CA186 [System][System.#GetMem]
4241E94 [System.Generics.Defaults][System.Generics.Defaults.MakeInstance]
424256A [System.Generics.Defaults][System.Generics.Defaults.Comparer_Selector_Binary]
4242E84 [System.Generics.Defaults][System.Generics.Defaults._LookupVtableInfo]
EED8629 [System.JSON.Types][System.JSON][System.JSON.Types.{System.Generics.Defaults}TComparer<System.JSON.Types.TJsonPosition>.Default]
EEE1572 [System.JSON.Readers][System.JSON][System.JSON.Readers.{System.Generics.Collections}TList<System.JSON.Types.TJsonPosition>.Create]
EEDB91E [System.JSON.Readers][System.JSON][System.JSON.Readers.TJsonReader.GetPath]
The block is currently used for an object of class: Unknown
The allocation number is: 3192748
Current memory dump of 256 bytes starting at pointer address 79DC9440:
7C EF A0 04 01 00 00 00 10 00 00 00 FE C5 93 13 00 00 00 00 A0 93 DC 79 00 00 00 00 00 00 00 00
60 74 1E 04 00 00 00 00 B9 B7 30 00 86 A1 1C 04 94 1E 24 04 6A 25 24 04 84 2E 24 04 29 86 ED 0E
72 15 EE 0E 1E B9 ED 0E AD 16 EF 0E CC 0C EF 0E 73 54 EF 0E 04 81 20 04 B0 18 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 0C 00 00 00 00 00 00 00 DE 3A 6C EC 7C EF A0 04 01 00 00 00
10 00 00 00 21 C5 93 13 00 00 00 00 A0 93 DC 79 00 00 00 00 00 00 00 00 60 74 1E 04 00 00 00 00
C6 B7 30 00 86 A1 1C 04 94 1E 24 04 6A 25 24 04 84 2E 24 04 29 86 ED 0E 72 15 EE 0E 1E B9 ED 0E
E5 16 EF 0E CC 0C EF 0E 73 54 EF 0E 04 81 20 04 B0 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00
| ï   . . . . . . . . . þ Å “ . . . . .   “ Ü y . . . . . . . .
` t . . . . . . ¹ · 0 . † ¡ . . ” . $ . j % $ . „ . $ . ) † í .
r . î . . ¹ í . ­ . ï . Ì . ï . s T ï . . . ° . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . Þ : l ì | ï   . . . . .
. . . . ! Å “ . . . . .   “ Ü y . . . . . . . . ` t . . . . . .
Æ · 0 . † ¡ . . ” . $ . j % $ . „ . $ . ) † í . r . î . . ¹ í .
å . ï . Ì . ï . s T ï . . . ° . . . . . . . . . . . . . . .
Thank you Dalija Prasnikar.
I took the System.JSON.Readers.pas source of Tokyo and the memory leak is gone.
try
Result := TJsonPosition.BuildPath(Positions);
finally
if Positions <> FStack then
Positions.Free;
end;
This was missing in the function TJsonReader.GetPath: string; method.

CompileError: WebAssembly.compile()

I am trying to execute basic C code of calculating factorial in WebAssembly and when I am loading WASM file in Google Chrome ( 57.0.2987.98) I am getting
CompileError: WebAssembly.compile():
Wasm decoding failedResult = expected magic word 00 61 73 6d,
found 30 30 36 31 #+0`
C Code :
double fact(int i) {
long long n = 1;
for (;i > 0; i--) {
n *= i;
}
return (double)n;
}
WAST :
(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "_Z4facti" (func $_Z4facti))
(func $_Z4facti (param $0 i32) (result f64)
(local $1 i64)
(local $2 i64)
(block $label$0
(br_if $label$0
(i32.lt_s
(get_local $0)
(i32.const 1)
)
)
(set_local $1
(i64.add
(i64.extend_s/i32
(get_local $0)
)
(i64.const 1)
)
)
(set_local $2
(i64.const 1)
)
(loop $label$1
(set_local $2
(i64.mul
(get_local $2)
(tee_local $1
(i64.add
(get_local $1)
(i64.const -1)
)
)
)
)
(br_if $label$1
(i64.gt_s
(get_local $1)
(i64.const 1)
)
)
)
(return
(f64.convert_s/i64
(get_local $2)
)
)
)
(f64.const 1)
)
)
WASM Compiled Code :
0061 736d 0100 0000 0186 8080 8000 0160
017f 017c 0382 8080 8000 0100 0484 8080
8000 0170 0000 0583 8080 8000 0100 0106
8180 8080 0000 0795 8080 8000 0206 6d65
6d6f 7279 0200 085f 5a34 6661 6374 6900
000a c380 8080 0001 bd80 8080 0001 027e
0240 2000 4101 480d 0020 00ac 4201 7c21
0142 0121 0203 4020 0220 0142 7f7c 2201
7e21 0220 0142 0155 0d00 0b20 02b9 0f0b
4400 0000 0000 00f0 3f0b
`
Code executed in Chrome :
async function load(){
let binary = await fetch('https://flinkhub.com/t.wasm');
let bytes = await binary.arrayBuffer();
console.log(bytes);
let module = await WebAssembly.compile(bytes);
let instance = await WebAssembly.Instance(module);
}
load().then(instance => console.log(instance.exports.fact(3)));
Can anyone help me out, I have been stuck on this for a whole day and not able to understand what is going wrong.
I used WebAssembly Explorer to to get the WAST and WASM code.
Using the WebAssembly Explorer's download capability you reference, I get the following file (as seen with hexdump):
0000000 00 61 73 6d 01 00 00 00 01 86 80 80 80 00 01 60
0000010 01 7f 01 7c 03 82 80 80 80 00 01 00 04 84 80 80
0000020 80 00 01 70 00 00 05 83 80 80 80 00 01 00 01 06
0000030 81 80 80 80 00 00 07 95 80 80 80 00 02 06 6d 65
0000040 6d 6f 72 79 02 00 08 5f 5a 34 66 61 63 74 69 00
0000050 00 0a c3 80 80 80 00 01 bd 80 80 80 00 01 02 7e
0000060 02 40 20 00 41 01 48 0d 00 20 00 ac 42 01 7c 21
0000070 01 42 01 21 02 03 40 20 02 20 01 42 7f 7c 22 01
0000080 7e 21 02 20 01 42 01 55 0d 00 0b 20 02 b9 0f 0b
0000090 44 00 00 00 00 00 00 f0 3f 0b
000009a
That's a valid .wasm binary which starts with the magic 00 61 73 6d a.k.a. \0asm. According to the error message you get, your file starts with 30 30 36 31 which isn't valid.
Double-check the .wasm file you have.
Decoding 30 30 36 31 as ASCII gives 0061 which seems to be your problem: you're loading the textual version of your hex file. Sure enough, the URL you provide (https://flinkhub.com/t.wasm) contains the following content as-is (I didn't hexdump it! It's ASCII):
0061 736d 0100 0000 0186 8080 8000 0160
017f 017c 0382 8080 8000 0100 0484 8080
8000 0170 0000 0583 8080 8000 0100 0106
8180 8080 0000 0795 8080 8000 0206 6d65
6d6f 7279 0200 085f 5a34 6661 6374 6900
000a c380 8080 0001 bd80 8080 0001 027e
0240 2000 4101 480d 0020 00ac 4201 7c21
0142 0121 0203 4020 0220 0142 7f7c 2201
7e21 0220 0142 0155 0d00 0b20 02b9 0f0b
4400 0000 0000 00f0 3f0b
I'm guessing you overwrote the file saved from the Explorer.
I solved this by setting GOARCH and GOOS environment variable correctly in zsh shell of mac before generating wasm object. Looks like go compiler does not recognize both these variables unless you explosively export them as a global variable in the parent shell. I simply exported both variables and run the compiler.
% export GOARCH=wasm
% export GOOS=js
% go build -o hello.wasm hello.go
I am not sure about your system, but I am using react and esbuild.wasm where while running the below code I was getting error.
const service = await esbuild.startService({
worker: true,
wasmURL: "/esbuild.wasm/esbuild.wasm"
})
esbuild.wasm is in my public folder along with node_module.
So I corrected the url which is wasmURL: "https://unpkg.com/esbuild-wasm#0.8.27/esbuild.wasm" and now it is working.
30 30 36 31 is the hex dump of the string "0061" which is the start of the hex dump of your wasm binary. Did you somehow fetch the textual hexdump instead of the actual binary?
In general, this error means that instead of the binary .wasm file your browser received something else. That can be a error page generated by the web server or the same .wasm but corrupted somehow. I recommend to open your browser Developer Tools, go to Network tab, Refresh the page and look at the .wasm resource request and response.

How to store outputs in a vector?

I need to put the outputs of my function into a vector.
In fact my results seems like this:
ans=
result 1
ans=
result2
ans=
result3
......
I need that my result look like this:
Vector= (result1
result2
result3
....)
The head of my function is like this:
function[keys]=generate()
for k=1:257
x= round(rand*9);
vet = x*ones(1,16);
i= round(rand*length(vet));
i(i==0)=1;
val= round(rand*257);
vet(i)=val;
keys=dec2hex (vet)
endfor
endfunction
I would be very grateful if you could help me.
This is, generally speaking, a very vague question.
If the function you are calling returns multiple things you can capture them in a vector like so:
[a, b, c] = myFunc()
If you are calling multiple functions each storing one thing you could do something like this:
vec = []
a = myFunc1()
vec = [vec a]
a = myFunc2()
vec = [vec a]
etc...
or shorter:
vec = [myFunc1() myFunc2() ... ]
It seems as though the ans output you're seeing is a result of the lack of a semi-colon on the line:
dec2hex(vet)
You should save that output into a vector and return that:
function[keys]=generate()
keys = [];
for k=1:257
x= round(rand*9);
vet = x*ones(1,16);
i= round(rand*length(vet));
i(i==0)=1;
val= round(rand*257);
vet(i)=val;
keys = [keys dec2hex(vet)];
endfor % endfor for octave
endfunction
I would omit the for loop and you should use randi if you want to generate random integers, for example for indexing. And I guess you want to split generating of keys and converting them to hex because the hex format is for visualisation. At the end I guess this question suffers from the XY-problem
function keys = generate ()
key_cnt = 5;
key_offset = 9;
key_length = 16;
keys = ones (1, key_length) .* randi (key_offset + 1, key_cnt, 1) - 1;
val = randi (2 ** 8, key_cnt, 1) - 1;
i = randi (key_length, key_cnt, 1);
ind = sub2ind ([key_cnt, key_length], 1:key_cnt, i');
keys (ind) = val;
endfunction
function ret = hex_keys (k)
## convert keys to hex string
for i = 1:rows (k)
ret(i, :) = [sprintf("%02X ",k(i,1:15)), sprintf("%02X",k(i,16))];
endfor
endfunction
k = generate ();
hex_keys (k)
gives
00 00 00 2A 00 00 00 00 00 00 00 00 00 00 00 00
03 03 C7 03 03 03 03 03 03 03 03 03 03 03 03 03
07 07 07 07 07 07 07 07 07 07 07 07 A7 07 07 07
03 03 03 03 03 03 03 8F 03 03 03 03 03 03 03 03
03 03 03 03 03 03 03 03 03 D0 03 03 03 03 03 03
Of course increase key_cnt to 257 if you are finished with testing.
very likely you can wrap your statement to compute ans into either horzcat or vertcat to achieve what you want.

MySQL COM_EXECUTE_STMT fails with error but the error does not make sense

I'm sending a COM_EXECUTE_STMT message and the server always returns an:
Error 1048 - #23000 - Column 'number_tinyint' cannot be null
The query is like this:
insert into numbers (
number_tinyint,
number_smallint,
number_mediumint,
number_int,
number_bigint,
number_decimal,
number_float,
number_double
) values
(
?, 679, 778, 875468, 100007654, 198.657809, 432.8, ?)
And what I send in is:
0: 18 00 00 00 17 01 00 00 . . . . . . . .
1: 00 00 01 00 00 00 00 00 . . . . . . . .
2: 01 01 05 0a 29 5c 8f c2 . . . . ) \ . .
3: f5 b0 58 40 . . X #
And simplified for reading:
18 00 00 - size
00 - sequence
17 - type
01 00 00 00 - statement id
00 - flags
01 00 00 00 - iteration-count
00 00 - null bitmap
01 - new params bound flag
01 - byte type
05 - double type
0a - byte value - 10
29 5c 8f c2 f5 b0 58 40 - double value
The statement parameters are 10 (for the tinyint column) and 98.765 (for the double column). From what I can see the message is encoded correctly but it always fails for some reason (at least from what the documentation says)
Am I missing something in here?
From the documentation to which you've linked:
payload:
[ deletia ]
n NULL-bitmap, length: (num-params+7)/8
Therefore, with two parameters in your case, the NULL-bitmap should have a length of (2+7)/8 = 1 byte, whereas you currently have a 2-byte bitmap.