Insert BLOB data into SQLite database field using Matlab and SQL query. Any idea? - sql

I need to store binary data in single database field (DB is SQLite, it's important!). Binary data is a some one-dimensional array.One of the best way to do that is BLOB data insertion. As far as I know Matlab doesn't consist methods for BLOB data processing. How can I do that using Matlab environment? Maybe using raw SQL query?

In a raw SQLite query, a blob can be written as a x-prefixed string, with each byte representeded by a two-digit hexadecimal number:
INSERT INTO MyTable(BlobColumn) VALUES(x'0123AB');
To return a blob as text from a query, you can use the quote() function, which uses the same format:
SELECT quote(BlobColumn) FROM MyTable; --> returns the string "x'0123AB'"
It might be a better idea to use some other SQLite3 driver for Matlab, e.g., https://github.com/kyamagu/matlab-sqlite3-driver, http://jaewon.mine.nu/jaewon/2015/06/17/another-sqlite-interface-for-matlab/.

Related

How does MyBatis deal with data type when insert and update?

I have been using String when INSERT and UPDATE no matter what data types the data in my oracle database are. Util I try to compare some ID data with NUMBER data type with String did I realize that something I forget which is the data types of the data in the database. But It seems my code work fine except retrieving data from the database and doing some comparisons. So how does MyBatis deal with data type when insert and update?

Query remote oracle CLOB data from MSSQL

I read different posts about this problem but it didn't help me with my problem.
I am on a local db (Microsoft SQL Server) and query data on remote db (ORACLE).
In this data, there is a CLOB type.
CLOB type column shows me only 7 correct data the others show me <null>
I tried to CAST(DEQ_COMMENTAIRE_REFUS_IMPORT AS VARCHAR(4000))
I tried to SUBSTRING(DEQ_COMMENTAIRE_REFUS_IMPORT, 4000, 1)
Can you help me, please ?
Thank you
No MSSQL but in my case we were pulling data into MariaDB using the ODBC Connect engine from Oracle.
For CLOBs, we did the following (in outline):
Create PLSQL function get_clob_chunk ( clobin CLOB, chunkno NUMBER) RETURN VARCHAR2.
This will return the the specified nth chunk of 1000 chars for the CLOB.
We found 1,000 worked best with multibyte data. If the data is all plain text single byte that chunks of 4,000 are safe.
Apologies for the absence of actual code, as I'm a bit rushed for time.
Create a Oracle VIEW which calls the get_clob_chunk function to split the CLOB into 1,000 char chunk columns chunk1, chunk2, ... chunkn, CAST as VARCHAR2(1000).
We found that Oracle did not like having more than 16 such columns, so we had to split the views into sets of 16 such columns.
What this means is that you must check what the maximum size of data in the CLOB is so you know how many chunks/views you need. To do this dynamically adds complexity, needless to say.
Create a view in MariaDB querying the view.
Create table/view in MariaDB that joins the chunks up into a single Text column.
Note, in our case, we found that copying Text type columns between MariaDB databases using the ODBC Connect engine was also problematic, and required a similar splitting method.
Frankly, I'd rather use Java/C# for this.

SQL Query about blob length

Is there a way to query a MySQL database that gives me all blobs that are bigger than 1kb?
Try using the LENGTH() function on the blob field.

Using an sql-function when reading and writing with NHibernate

I have the following problem.
I have a special column in my tables (a blob). In order to read and write to that column, I need to call an SQL function on its value - to convert to a string when I read, and to convert from a string to this blob when I write.
The read part is easy - I can use a formula to run the sql function against the column. But formulas are read only. Using IUserType also did not seem to assist - I can get the blob and write my own code to convert it to my own type, but I don't want to do that - I already have a database function that does this work for me.
Any ideas?
You can specify sql to insert and update, see the reference documentation, "Custom SQL for create, update and delete". Here is an example from Ayende which uses stored procedures (which is not the same, just to see how it works).
Or you could write a database trigger which does this transformation.

Insert binary std::string using ODBC into BLOB column

can somebody give me an example how to insert binary std::string using ODBC and C++ into BLOB column, please?
EDIT:
Duplicate of How to INSERT binary std::string into BLOB (MySQL).
If it is really a binary string, and you have actual binary data in it (e.g. embedded nulls) then you need to be careful not to use any of the std::string members that can treat it as a C string (e.g. with a terminating null) as you will obviously end up losing data.
The std::string class is not really appropriate for this purpose - it is written on the assumption that what you are working with is actually a string of characters - since you are using a blob or array of binary data, a std::vector is probably more appropriate.
To insert the data will require you to bind the content of the vector to the SQL query using bind parameters. That is to say, instead of using SQLExecDirect to execute a string containing the SQL query, you should use SQLPrepare to create the statement handle - you leave the spot where the value of the binary value would go with a '?', then use SQLBindParameter to bind the placeholder to the binary value. You then call the function using SQLExecute.
There are some code examples in the SQLBindParameter documentation above.