Insert image into SQL Server 2008 Express database without front end application - image

I am working with test data in Visual Studio's server explorer. How would I put images into the database just as test images? I have no front end component built to take care of image uploading.

It will work for SQL server 2008r2...but first u have to create a filestream database.
//create databse
CREATE DATABASE Archive
ON
PRIMARY ( NAME = Arch1,FILENAME = 'c:\data\archdat1.mdf'),
FILEGROUP FileStreamGroup1 CONTAINS FILESTREAM( NAME = Arch3,FILENAME = 'c:\data\filestream1')
LOG ON ( NAME = Archlog1,FILENAME = 'c:\data\archlog1.ldf')
GO
//table creation
Use Archive
GO
CREATE TABLE [FileStreamDataStorage]
(
[ID] [INT] IDENTITY(1,1) NOT NULL,
[FileStreamData] VARBINARY(MAX) FILESTREAM NULL,
[FileStreamDataGUID] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE DEFAULT NEWSEQUENTIALID(),
[DateTime] DATETIME DEFAULT GETDATE()
)
ON [PRIMARY]
FILESTREAM_ON FileStreamGroup1
GO
//inserting value
Use Archive
GO
INSERT INTO [FileStreamDataStorage] (FileStreamData)
SELECT * FROM
OPENROWSET(BULK N'C:\Users\Public\Pictures\Sample Pictures\image1.jpg' ,SINGLE_BLOB) AS Document
GO

You can upload an image into a db (and retrieve it) using byte[] as data type, assuming that in your db corresponding column is a BLOB.
So if you load your image with byte[] img = File.ReadAllBytes(your_file) then you can use a query like this INSERT INTO table SET image_col = #par, where par is a parameter whose value is img.

Related

Query/flatten a serialized protobuf stored as a string column in Clickhouse

TLDR
I have a column of serialized protobufs in a table in Clickhouse, and I would like to flatten those protobufs into another table via a materialized view. How do I format the materialized view to do this?
Details
I have a RabbitMQ queue which serves messages to my Clickhouse server. The message outer.proto consists of a service name and a serialized protobuf message payload:
//outer.proto
syntax = "proto2"
message WrappedMessage {
required string svc_id = 1;
required bytes msg = 2;
}
This service name and payload are then stored in Clickhouse as such:
CREATE TABLE raw_records_rmq
(
svc_id String NOT NULL
, msg String NOT NULL
) ENGINE = RabbitMQ SETTINGS
--skip rabbitmq settings, this part works
rabbitmq_format='ProtobufSingle'
rabbitmq_schema = 'outer:WrappedMessage'
;
CREATE TABLE raw_records
(
svc_id String NOT NULL
, msg String NOT NULL
) ENGINE = MergeTree ORDER BY tuple()
;
CREATE MATERIALIZED VIEW raw_mv
TO raw_records AS
SELECT * FROM raw_records_rmq
This process works as expected, with the serialized message stored in raw_records.msg. The message is defined as such:
//inner.proto
syntax = "proto2"
message Person {
optional uint32 id = 1;
optional string name = 2;
optional bool arb_bool = 3;
}
I would now like to query the contents of the stored message; to simplify this, I create a destination table:
CREATE TABLE people
(
id UInt32
, name String
, arb_bool UInt8
) ENGINE = MergeTree ORDER BY tuple()
But this is where my success stops. My attempts so far have been to query the column as a subquery and then attempt to parse the results as protobufs using Clickhouse FORMAT and SETTINGS, as described in their documentation:
CREATE MATERIALIZED VIEW mv
TO people AS
SELECT * FROM (SELECT proto FROM raw_records)
FORMAT ProtobufSingle
SETTINGS format_schema='inner:Person'
However, this fails to unpack the protobuf message. Changing from a Materialized View to a standard View shows that Clickhouse is returning only the single column specified in the subquery with the entire protobuf message as each result.
Any advice on how to properly format this materialized view, or alternatives for processing protobufs-inside-protobufs would be greatly appreciated!
You can't convert a single column into multiple values.
You can use the protobuf format but it will be the entire protobuf message: https://clickhouse.com/docs/en/interfaces/formats/#protobuf

API Platform Raw Query

Anyone knows how to get raw query working in API platform controller/handler,
I am trying to do something like,
$temp_table_query = $this->entityManager->createQuery('CREATE TEMPORARY TABLE {temp_validation_code_table} LIKE {validation_code}');
$qb = $this->entityManager->createQueryBuilder();
$qb = $qb->select('LOAD DATA LOCAL INFILE {$file} INTO TABLE {temp_validation_code_table} (code) SET offer = {$offer}');
Trying to load file data into a temporary table for validation before inserting in actual db table
This needs to be done using the underlying Doctrine PDO abstraction (Doctrine\DBAL\Connection), which can be obtained via $entityManager->getConnection();.
First however, you need to make sure that the underlying PDO connection is configured to allow LOAD LOCAL INFILE (by setting PDO::MYSQL_ATTR_LOCAL_INFILE to true). This can be done by providing the following Doctrine configuration (in doctrine.yaml):
doctrine:
dbal:
#...
options:
1001: true # This sets PDO::MYSQL_ATTR_LOCAL_INFILE to true
Be warned that allowing LOAD LOCAL INFILE possibly introduces security vulnurabilities.
Given I have a file named example.csv containing the following CSV data:
1,"foo"
2,"bar"
3,"baz"
I can then insert this data using the Doctrine PDO abstraction:
<?php
//...
// Local path to the CSV file
$file = __DIR__ . '/example.csv';
// Obtaining the PDO connection
$connection = $this->entityManager->getConnection();
// Creating the temporary table
$connection->executeStatement('CREATE TEMPORARY TABLE foobar (id int AUTO_INCREMENT PRIMARY KEY , name VARCHAR(256) NOT NULL)');
// Inserting the CSV data
$connection->executeStatement("LOAD DATA LOCAL INFILE '$file' INTO TABLE foobar FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'");

Maximo MAXINTMSGTRK table: How to extract text from MSGDATA column? (HUGEBLOB)

I'm attempting to extract the text from the MSGDATA column (HUGEBLOB) in the MAXINTMSGTRK table:
I've tried the options outlined here: How to query hugeblob data:
select
msg.*,
utl_raw.cast_to_varchar2(dbms_lob.substr(msgdata,1000,1)) msgdata_expanded,
dbms_lob.substr(msgdata, 1000,1) msgdata_expanded_2
from
maxintmsgtrk msg
where
rownum = 1
However, the output is not text:
How can I extract text from MSGDATA column?
It's is possible to do it using Automation script, uncompress data using psdi.iface.jms.MessageUtil class.
from psdi.iface.jms import MessageUtil
...
msgdata_blob = maxintmsgtrkMbo.getBytes("msgdata")
byteArray = MessageUtil.uncompressMessage(msgdata_blob, maxintmsgtrkMbo.getLong("msglength"))
msgdata_clob = ""
for symb1 in byteArray:
msgdata_clob = msgdata_clob + chr(symb1)
It sounds like it's not possible because the value is compressed:
Starting in Maximo 7.6, the messages written by the Message Tracking
application are stored in the database. They are no longer written as
xml files as in previous versions.
Customers have asked how to search and view MSGDATA data from the
MAXINTMSGTRK table.
It is not possible to search or retrieve the data in the maxintmsgtrk
table in 7.6.using SQL. The BLOB field is stored compressed.
MIF 7.6 Message tracking changes

JDBC, Oracle SQL, adding and displaying picture on label

I have picture 'osta.jpg' on my desktop and I want to add this to my database Oracle SQL and then load this picture from database in Java application (JDBC) and show on label by a JLabel lblFilm = new JLabel(new ImageIcon( "picture_from_SQL") (Swing library)
I try adding image to database:
ALTER TABLE film ADD bfile_loc bfile, bfile_type varchar2(4);
UPDATE film SET bfile_type = 'JPEG';
UPDATE film SET bfile_loc = bfilename('GIF_FILES','C:\Users\Maciej\Desktop\osta.jpg') WHERE kategoria IN 'thriller';
However, I don't know how load it in the Java application.
First things first, in order to create a valid BFILE you need to specify an Oracle directory and then the location of the file within that directory. I don't know what your GIF_FILES directory is, but if it were created using
CREATE DIRECTORY GIF_FILES AS 'C:\Users\Maciej\Desktop';
then you would use the following to set the BFILE:
UPDATE film SET bfile_loc = bfilename('GIF_FILES', 'osta.jpg') WHERE kategoria IN 'thriller';
Secondly, in order to read the BFILE out of Oracle using JDBC, you need to use some Oracle-specific classes. Firstly, you need to cast the ResultSet to oracle.jdbc.OracleResultSet, so you can use the getBFILE() method to get the BFILE locator from the ResultSet. Then open the BFILE, get an InputStream from the BFILE's binaryStreamValue() method and read the data out of that.
Here's some example code (error handling could be improved):
import java.io.*;
import java.sql.*;
import oracle.sql.BFILE;
import oracle.jdbc.OracleResultSet;
// ...
// 'connection' here is your Oracle database connection.
Statement stmt = connection.createStatement();
OracleResultSet rSet = (OracleResultSet)stmt.executeQuery(
"SELECT bfile_loc FROM film");
if (rSet.next()) {
BFILE bfile = rSet.getBFILE(1);
System.out.println("Length: " + bfile.length());
bfile.open();
InputStream is = bfile.binaryStreamValue();
// Read data from input stream...
is.close();
bfile.close();
}
In the code above, I also fetch the length of the file, which isn't essential but it will fail if the file cannot be found.
Thirdly, you need to convert the InputStream to an ImageIcon. The ImageIcon class has a constructor that takes a byte array, and you can use answers to this question to convert the InputStream into a byte array.

How do I insert CData value into azure table storage row

I just can't insert a CData value into table row.
my new table entity is like
new Book { Description = new XCData("Asp.net<What>XXXXX</What>").ToString(), CreatedOn = DateTime.Now, })
and then I insert the book via context.
THe result:
<Properties><CreatedOn>2010-02-18T10:17:10.953Z</CreatedOn><Name><![CDATA[Asp.net<What>XXXXX</What>]]></Name></Properties>
What I want is:
<Properties><CreatedOn>2010-02-18T10:17:10.953Z</CreatedOn><Name><![CDATA[Asp.net<What>XXXXX</What>]]></Name></Properties>
How do I make it?
Such usage isn't part of the happy way with the StorageClient 1.1 library; but you can use DataServiceContext.WritingEntities to achieve this behavior (although, it's rather a hacky approach).

Resources