Insert binary std::string using ODBC into BLOB column - blob

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.

Related

SQL data types for AnyLogic

I am saving the output of my AnyLogic model into an SQL server database. For non-AnyLogic aficionados, AnyLogic is based on Java. However, I am not sure what data types I need to specify for my columns in the database.
So far I am using these:
double in AnyLogic : float in SQL
string in AnyLogic : varchar in SQL
int in AnyLogic : int in SQL
I also have parameters that are of type Option list, which is, if I understand correctly, a form of Java enum. I tried to save those parameters as varchar, but this (obviously) does not work. In addition, my model contains various boolean parameters. For my boolean parameters, I add columns of type bit in SQL by running:
ALTER TABLE myTable
ADD my_bool BIT NOT NULL DEFAULT 0;
However, running the model returns this error
SQLServerException: Invalid column name 'false'. Caused by: Invalid column name 'false'
So concretely, how can I export parameters of type Option list and boolean?
This addresses the original question which was tagged MySQL.
I don't know all the issues around "option list". Seems like a string (with a length such as varchar(255)) would work. You can also look into the built-in enum type, although I would not normally recommend using enums.
I would recommend using boolean instead of bit as the equivalent for boolean. Seems more mnemonic.
That said, MySQL understands false as a constant. You can check this by running:
select false
This also works:
select "false"
However, this returns the error that you specify:
select `false`
I suspect that the code being generated is using this construct. You will need to look at the code -- and you might need to figure out some other way of handling this. In MySQL you can use 0 for false and that might fix your problem.
The AnyLogic database is a standard HSQLDB database (not something proprietary) but they've added AnyLogic client functionality to define 'column types' as though they are Java types (with special types for option lists and compiled-on-the-fly-and-run Java code).
If you look at the db.script file (HSQLDB just stores the persistent DB data as an SQL script which creates the tables and INSERTs the values) you can see the underlying HSQLDB types which map closely to SQL Server types.
boolean --> BOOLEAN
double --> DOUBLE
int --> INT
String --> VARCHAR(16777216)
Date --> TIMESTAMP
Code --> VARCHAR(16777216)
Option List --> VARCHAR(255)
NB: The 'Java column types' are supposed to make it easier for a non-technical user to understand what they will get from a Java perspective when querying that column but, for example, they are confusing in that queries will return Java nulls for missing values, so a boolean column actually effectively returns a Boolean.
That should help.
I managed to address part of my problem. I am now able to store String variables from Java into my SQL database. The issue was due to incorrect use of quotations.
Java uses double quotations for String variables (e.g.: ""). SQL expects single quotations (e.g.: '') for string-like columns such as varchar() and char()
I had to amend my SQL query to this:
String insertTableSQL = "INSERT INTO test (my_string) VALUES(" +" '"+my_variable_string+"' )";
Note that my_variable_string is a derivative of a Java enum, which I obtained by executing String my_variable_string= my_enum.name();

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

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/.

Inserting large xml data into DB2

I have the following situation,
in my database table I have a column for storing xml data. When I try to insert data into this table by using Insert query, I am getting string is too large exception.
I tried keeping the type as XML and VACHAR(32000), I get the same exception.
Can anyone help me with this?
UPDATE
This is the error I am getting while inserting.
The string constant beginning with "'<!--Sample XML file generated by XMLSpy v2013 sp1 (http://www.altova." is too long.. SQLCODE=-102, SQLSTATE=54002, DRIVER=3.59.81
The XMLPARSE function is useful for converting text strings into DB2's XML data type. Have you tried wrapping your XML text with a call to XMLPARSE?
If your XML document is in a file, there's a UDF you can add that makes it easier to pull the file contents directly into an XML column.
you can set the size of each column in your table. Try changing the default size(which is not max size) to an appropriate one.
Also when you try to query directly using SQL command line, the DBMS has to create a large string constant form its string pool that can hold your XML(which in this case, is very large and hence String cannot be created). If you query the same programmatically though, it works.
If the same error persists or if DBMS throws Data Integrity error, then try changing the data structure to a bigger one like CLOB(most likely in this case) or BLOB(in case of images and multi media).
It's a bit silly but the answer is use the XMLPARSE and segment your XML string in 32K chunks and prepend a CLOB statement to override the string size limitation
XMLPARSE ( DOCUMENT
CLOB('<?xml version="1.0"?>') ||
'<aLotOfVeryBigXmlData32kPart1 ......
' ||
'<aLotOfVeryBigXmlData32kPart2 ......
' ||
... etc ...
'<aLotOfVeryBigXmlData32kPartN ......
' )

Change SQL decimal delimiter

I am trying to import data into my table using
INPUT INTO
The problem is my decimals is using , as a delimiter, and it expects .. So it won't work!
How can i change this? Search and replace in the input file is not an option!
I am using SQL Anywhere 10
I don't believe that it's possible to change the decimal delimiter. You could either preprocess the file (which I know you said is not an option) or load it into a temporary table with the decimal column defined as a string and then use an insert from the temporary table to your real table, performing the necessary conversion at that point.

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.