How can i retrive my image from sql server 2008 using matlab - sql-server

i had inserted image in sql server using matlab but when i am retrieving that image i get this
[845941x1 int8] as my output so please tell me how will i get my original image using this output........ or any other way to retrieve that image....
this is my code to insert image:
// conn = database('test','prashant','');
//pes = exec(conn,'insert into filee select ''jpg'' as filetype,* from
//openrowset (bulk ''D:\file\index.jpg'' , single_blob) as x ');
//close(conn);
code to retrieve is:
// conn = database('test','prashant','');
// pess = exec(conn,'select * from filee');
//pess = fetch(pess);
// pess.data[1,3];
// close(conn);
please tell me the code to retrieve my image back.....

It is very unclear what you want, if you want to get the information from a table, perhaps something like this can do the trick:
exec(conn,'select * from filee')

I think this vector is the raw database representation of the image... Try to reshape it to it's original size and then to draw it:
image(reshape(int8array,height,width));
But since I just see that your dimension of the int8 array is a prime number I wonder whether it can be reshaped in an image...

Related

pyodbc - Image from Microsoft SQL Server writing in binary mode error

Hopefully I'm missing something obvious here...
Whenever I try and use pyodbc to convert and save images from a Microsoft SQL Server, I generate files that are not recognised as images files.
Python code:
cursor = conn.cursor()
q = "SELECT top 1 * from reactorA.dbo.ProcessedImageData"
records = cursor.execute(q).fetchall()
for row in records:
data = row.AnalysedImage
with open('test.bmp','wb') as f:
f.write(data)
When I open the newly created image file it's just repeating ΓΏ symbols
When I view the record it seems to be in the correct bytes format
b'\xff\xff\xff .......
Any help on fixing this will be greatly appreciated.

Decrease value of a field using Spring JDBC template

I am trying to decrease the value of a field in DB2 Table using Spring JDBC template.Below is my code
String sql= "UPDATE XYZ SET CURR_COUNT = CURR_COUNT-1 WHERE COMPANY_CODE =?";
int rowsUpdated = jdbcTemplate.update(sql, new Object[] {compCode});
This gives me error "**PreparedStatementCallback; bad SQL grammar**"
If I am not wrong the error is because of this **"CURR_COUNT = CURR_COUNT-1"** as program dont have value of CURR_COUNT. Can anybody suggest me a solution for this.
You could take value from database via SELECT to variable (int or long), decrement it in java, and then save new value in UPDATE query.

R Converting SQL Server Query with Geometry Datatype to spatialpolygonsdataframe

I am trying to plot geometry (binary) polygon data from an SQL Server data source. What I want to do is use the Geometry Data Type from the SQL query for the polygons, and also the rest of the columns in the query as the #data attribute table within the SpatialPolygonsDataFrame class.
This is my code so far, to get the SQL query data into a simple data.frame and convert the binary datatype using wkb::readWKB().
From this stage, I do not know how to create the SpatialPolygonsDataFrame dataframe.
library(RODBC)
library(maptools)
library(rgdal)
library(ggplot2)
dbhandle <- odbcDriverConnect("connection string",rows_at_time = 1)
sqlStatement <- "SELECT ID
, shape.STAsBinary() as shape
, meshblock_number
, areaunit_code
, dpz_code
, catchment_id
FROM [primary_parcels] hp "
sqlStatement <- gsub("[\r\n]", "", sqlStatement)
parcelData <- sqlQuery(dbhandle,sqlStatement )
odbcClose(dbhandle)
parcelData$shape <- wkb::readWKB(parcelData$shape)
This might be too late, however, with the help of a friend and this I found a solution for that. It seems a bit funny, but I am also working on a similar set of data and had the same problem. Please remember, it is difficult to replicate your approach as you did not provide a replicable example. You also need to adjust the projection. Please note that it is easier to build the connection in odbc once and call it each time, rather than writing the connection each time.
library(rgeos)
library(mapview)
library(raster)
library(dplyr)
library(sp)
#You may ignore this
odbcCh<-odbcConnect("Rtest")
sqlStatement=sqlQuery(odbcCh, 'SELECT ID , shape.STAsBinary() as shape, meshblock_number , areaunit_code, dpz_code, catchment_id FROM [primary_parcels] hp')
parcelData <- sqlQuery(odbcCh,sqlStatement )
#untill here
things <- vector("list", 1)
z = 0
for(line in parcelData$shape)
{
{
things[[z+1]]<-readWKT(line)
}
z = z + 1
}
Things <- do.call(bind,things)
Things.df= SpatialPolygonsDataFrame(Things,data.frame(parcelData$ID,parcelData$catchment_id))
plot(Things.df)
#you may not need the rest
Things.df#proj4string= CRS("+proj=nzmg +lat=-41.0 +lon=173.0 +x_0=2510000.0 +y_0=6023150.0 +ellps=intl +units=m")
mapview(Things.df)

Create data for Image column in SQL Server

I need to create data to test the limit of Image Datatype in SQL Server. It is able to hold variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes. I am using the following directly in SQL Server:
INSERT INTO Employees (Id, Name, Photo)
SELECT 10, 'John', BulkColumn FROM Openrowset( Bulk 'E:\photo.jpeg', Single_Blob) as EmployeePicture
My question is, how do I ensure that the max size (2,147,483,647 bytes) is being inserted into the column? Do I just find an image of size 2G and insert it into the column? Is there another way to do this?
Thanks in advance.
Most JPEG viewers completely ignore the bytes after the image data. The following code will tack on enough bytes to make the file whatever size you want. I tested it and the resulting image opened in the Windows image viewer, paint.net, and Photoshop. You should be able to create test files with it...
long desiredSize = 200000;
byte[] buffer = new byte[1];
string filename = #"c:\temp\sample.jpg";
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
{
fs.Seek(desiredSize - fs.Length - 1, SeekOrigin.End);
fs.Write(buffer, 0, 1);
fs.Close();
}

PostgreSQL query within PostGIS function

I am trying to run the following PostGIS query:
select ST_distance_spheroid(
ST_GeomFromText('POINT(
(select AsText(location) from test where name="EGMC")
)', 4326),
ST_GeomFromText('POINT(
(select AsText(location) from test where name="EGDY")
)', 4326),
'SPHEROID["WGS_1984",6378137,298.257223563]'
);
but keep getting an error:
ERROR: parse error - invalid geometry
HINT: "POINT(
(s" <-- parse error at position 9 within geometry
I'm happy that I know what the error means, I just don't know how to go about achieving what I want to do. I don't want to manually specify the location, it's stored in the database! I know the name of the place, so I want to get it's location by looking it up. How should I be doing this? Also, it seems a bit unnecessary to convert to a string to convert back, what else can I do?
If I can do this without having to specify variables that would be great.
Thanks.
You are mixing SQL and WKT, which are not the same. Furthermore, you don't need to recreate geometries that already exist. Query the existing geometries instead:
select ST_distance_spheroid(g1.location, g2.location, 'SPHEROID["WGS_1984",6378137,298.257223563]')
from test g1, test g2
where g1.name = "EGMC" and g2.name = "EGDY";

Resources