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

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.

Related

How to access and update an already existing tables of sqlite database file in another python file

I wrote the following code to create a database file and then created a table in that database and inserted values using sql queries in python.
let say this is a file named info.py
conn = sqlite3.connect('sqlite_file.db', timeout=20)
c = conn.cursor()
#Creating a new SQLite table with 1 column
c.execute('CREATE TABLE STUDENTS_ (Name CHAR, RollNo INTEGER)')
a=['Richa', 'Swapnil', 'Jahanavi', 'Shivam', 'Mehul']
b=[122, 143, 102, 186, 110]
p=0
for r in b:
c.execute("INSERT INTO STUDENTS_ VALUES (?,?);", (a[p],b[p]))
p=p+1
It runs well and gives the expected result.
Now I want to update the same table named STUDENTS_, through another code in a different python file,I tried the code below.
This is another file named info_add.py
import sqlite3
sqlite_file = 'my_first_db.sqlite' # name of the sqlite database file
STUD = 'STUDENTS_' # name of the table to be created
conn = sqlite3.connect('sqlite_file.db', timeout=20)
c = conn.cursor()
a=['Riya', 'Vipul']
b=[160, 173]
p=0
for r in b:
c.execute("INSERT INTO STUDENTS_ VALUES (?,?);", (a[p],b[p]))
p=p+1
I Get the following error:
OperationalError: database is locked
What is this error?I know i am doing wrong, please anyone help me with a right method!!Thankyou
The "database is locked" message indicates that some other connection still has an active transaction.
Python tries to be clever and automatically starts transactions for you, so you have to ensure that you end your transactions (conn.commit()) when needed.

Invalid Data Causes SSIS Flat File Import to Hang

I'm working on a SSIS package in VS 2010 for a 2012 instance. I'm importing flat files from a vendor who will not clean their data. I cannot get beyond the "Flat File Source" step because the data is so mangled it hangs and won't continue parsing.
Here is an example of good data with headers:
EventID|AccountID|ListID|ID|Date
1|3000|20030|1092997696|10-Nov-2014 09:36:13
Here is bad data that will (and is) be/ing captured by error handling:
1|3000|20030|1092997696;ҧ��DAVNJ��|11-Nov-2014 06:40:28
Here is data that hangs my package:
1|3000|20030|1092997696ci[
a5��~[�t:RW�uXXïA,u��ïn��I� �JA!QXQ|11-Nov-2014 08:27:27
How do I deal with this? Remember, I cannot get beyond the flat file parse step to use a derived column/conditional split/script task.
Thanks in Advance!
Kirsten
Figured out a way to get the data in so I can clean it!
CREATE TABLE dbo.crap_data_varcharmax(
DataBlob NVARCHAR(MAX));
BULK INSERT dbo.crap_data_varcharmax
FROM '\\SQLSERVERNAME01\e$\Folder\FileName.txt' WITH (ROWTERMINATOR = '\n', FIELDTERMINATOR = '|', FIRSTROW = 2);
SELECT *
FROM dbo.crap_data_varcharmax;

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

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

FreeTDS / SQL Server UPDATE Query Hangs Indefinitely

I'm trying to run the following UPDATE query from a python script (note I've removed the database info):
print 'Connecting to db for update query...'
db = pyodbc.connect('DRIVER={FreeTDS};SERVER=<removed>;DATABASE=<removed>;UID=<removed>;PWD=<removed>')
cursor = db.cursor()
print ' Executing SQL queries...'
for i in range(len(data)):
sql = '''
UPDATE product.sanction
SET action_summary = '{action_summary}'
WHERE sanction_id = {sanction_id};
'''.format(sanction_id=data[i][0], action_summary=data[i][1])
cursor.execute(sql)
cursor.close()
db.commit()
db.close()
However, it hangs indefinitely, no error.
I'm new to pyodbc, but it should be setup correctly considering I'm having no problems performing SELECT queries. I did have to call CAST for SELECT queries (I've cast sanction_id AS INT [int identity on the database] and action_summary AS TEXT [nvarchar on the database]) to properly populate data, so perhaps the problem lies somewhere there, but I don't know where to start debugging. Converting the text to NVARCHAR didn't do anything either.
Here's an example of one of the rows in data:
(2861357, 'Exclusion Program: NonProcurement; Excluding Agency: HHS; CT Code: Z; Exclusion Type: Prohibition/Restriction; SAM Number: S4MR3Q9FL;')
I was unable to find my issue, but I ended up using QuerySets rather than running an UPDATE query.

PowerBuilder DSN Creation

I am new to PowerBuilder.
I want to retrieve the data from MSAccess tables and update it to corresponding SQL tables. I am not able to create a permanent DSN for MSAccess because I have to select different MSAccess files with same table information. I can create a permanent DSN for SQL server.
Please help me to create DSN dynamically when selecting the MSAccess file and push all the tables data to SQL using PowerBuilder.
Also give the full PowerBuilder code to complete the problem if its possible.
In Access we strongly suggest not using DSNs at all as it is one less thing for someone to have to configure and one less thing for the users to screw up. Using DSN-Less Connections You should see if PowerBuilder has a similar option.
Create the DSN manually in the ODBC administrator
Locate the entry in the registry
Export the registry syntax into a .reg file
Read and edit the .reg file dynamically in PB
Write it back to the registry using PB's RegistrySet ( key, valuename, valuetype, value )
Once you've got your DSN set up, there are many options to push data from one database to the other.
You'll need two transaction objects in PB, each pointing to its own database. Then, you could use a Data Pipeline object to manage the actual data transfer.
You want to do the DSNLess connection referenced by Tony. I show an example of doing it at PBDJ and have a code sample over at Sybase's CodeXchange.
I am using this code, try it!
//// Profile access databases accdb format
SQLCA.DBMS = "OLE DB"
SQLCA.AutoCommit = False
SQLCA.DBParm = "PROVIDER='Microsoft.ACE.OLEDB.12.0',DATASOURCE='C:\databasename.accdb',DelimitIdentifier='No',CommitOnDisconnect='No'"
Connect using SQLCA;
If SQLCA.SQLCode = 0 Then
Open ( w_rsre_frame )
else
MessageBox ("Cannot Connect to Database", SQLCA.SQLErrText )
End If
or
//// Profile access databases mdb format
transaction aTrx
long resu
string database
database = "C:\databasename.mdb"
aTrx = create transaction
aTrx.DBMS = "OLE DB"
aTrx.AutoCommit = True
aTrx.DBParm = "PROVIDER='Microsoft.Jet.OLEDB.4.0',DATASOURCE='"+database+"',PBMaxBlobSize=100000,StaticBind='No',PBNoCatalog='YES'"
connect using aTrx ;
if atrx.sqldbcode = 0 then
messagebox("","Connection success to database")
else
messagebox("Error code: "+string(atrx.sqlcode),atrx.sqlerrtext+ " DB Code Error: "+string(atrx.sqldbcode))
end if
// do stuff...
destroy atrx

Resources