Unable to insert u'\U0001f381' in sql server using python - sql-server

Hi,
I am trying to push some data in sql server database using python 2.7.13 from ubuntu 14.04 LTS. I have one unicode object i.e. u'\U0001f381' in my data. Which is actually 🎁(u'\U0001f381') symbol. When I am trying to insert, it is giving me following error:
Python script:
import pymssql
conn = pymssql.connect(host=HOST, user=USER, password=PASSWORD,
database=DATABASE)
cursor = conn.cursor()
field_lst = [u'2017-04-09', u'\U0001f381', 1.0, 0.0, 0.0, 3.0]
placeholder = '%s,' * len(field_lst)
query = 'INSERT INTO my_table VALUES (' + placeholder.strip(',') + ')'
cursor.execute(query, tuple(field_lst))
conn.commit()
Error:
OperationalError(105, "Unclosed
quotation mark after the character string ''.DB-Lib error message
20018, severity 15:\nGeneral SQL Server error: Check messages from the
SQL Server\nDB-Lib error message 20018, severity 15:\nGeneral SQL
Server error: Check messages from the SQL Server\n"),
When I tried to push same data from windows, using pycharm, it is getting inserted into database without any error.
Any help would be appreciated.
Thanks.

Related

Perl DBI / MS ODBC Driver (LinuxL:RHEL) / SQL-Server: How to insert/update BLOB varbinary(max) data?

New to SQL-Server. I'm attempting to load a pdf to a SQL-Server table (data type varbinary(max)) via PERL/MS ODBC driver/DBD::ODBC using the following (simplified) code:
use DBI qw(:sql_types);
open my $pdfFH, "test.pdf";
my #pdf = <$pdfFH>; close $pdfFH;
my $pdfStr = join('', #pdf);
my $dbh = <...valid db-handle ...>;
my $sth = $dbh->prepare(qq(
insert into
TestTable(Report)
values
(?)));
$sth->bind_param(1,$pdfStr,DBI::SQL_VARBINARY);
$sth->execute;
Error:
DBD::ODBC::st bind_param failed: [Microsoft][ODBC Driver 17 for SQL Server]Invalid precision value (SQL-HY104) at ./t_sqlserver.pl line 37.
DBD::ODBC::st execute failed: [Microsoft][ODBC Driver 17 for SQL Server]COUNT field incorrect or syntax error (SQL-07002) at ./t_sqlserver.pl line 38.
I am able to successfully load other data types. An alternative is to load the pdf locally from the file system using OPENROWSET(BULK...) but I would prefer to load directly to avoid moving the file from Linux to Windows.
The driver should be clever enough to guess the correct type most of the times. Try binding the parameter without specifying the type at all.

Perl, error when attempting to run a stored procedure in SQL Server

I'm trying to execute a stored procedure that has 3 columns of type nvarchar(max) on the table. However, I'm getting the following error:
Caught exception in H2::Controller::AP->apge "DBD::Sybase::st fetchrow_hashref failed: Server message number=4004 severity=16 state=1 line=48 server=DEV procedure=sp_List text=Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. at /AP.pm line 361."
I've tried to have the columns cast as varchar(max) and text, yet it still fails.
Running the SQL will retrieve the data just fine, but calling the stored procedure it errors out.
A temp table has been created that has the type of text for the three columns.
Which fails with error:
Caught exception in H2::Controller::AP->apge "DBD::Sybase::st fetchrow_hashref failed: Server message number=4004 severity=16 state=1 line=47 server=DEV procedure=sp_List text=Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. at /AP.pm line 361."
The database connection is:
has 'dbh' => ( is => 'rw', isa => 'DBI::db', default => sub{ return DATABASE::connect( system => 'xx', database => 'xxx', user => 'xxx' ); } );
The call to the stored procedure:
my $sql ="exec tskMngmnt.dbo.sp_List ";
if (defined $a && $a ne "" && $a ne "NONE") {
$sql .= "\#e = $a ";
} else {
$sql .= "\#e = NULL ";
}
Here is the versions of DBI that we have installed:
perl -MDBI -e 'DBI-> installed_versions;'
Perl : 5.018002 (x86_64-linux-thread-multi)
OS : linux (4.4.92-31-default)
DBI : 1.628
DBD::mysql : 4.021
DBD::Sybase : 1.15
DBD::Sponge : 12.010003
DBD::SQLite : 1.50
DBD::Proxy : 0.2004
DBD::Gofer : 0.015327
DBD::File : 0.42
DBD::ExampleP : 12.014311
DBD::DBM : 0.08
The stored procedure should return 1 row of the columns I'm trying to retrieve.
It looks from the error message your Perl DBD driver for Sybase has been compiled using "DBLib" which hasn't really been maintained for years. It was replaced with CTLib in the 1990s. DBLib didn't support multi-byte character sets.
You will probably want to upgrade/recompile the DBD Driver or replace it with an ODBC driver from Microsoft who provide such a driver for Linux alongside their MS SQL for Linux offering.
MS SQL Sever ODBC Driver on Linux
If you're using FreeTDS, you might have some mileage by changing the protocol version number as mentioned in this answer.
Change TDS Protocol Version

Bulk Load in SQL Server failed to load float values

I have to transfer data from one database server to a SQL Server. I'm using SQLServerBulkCopy to do that:
// connection1 is with the source system and
// connection2 is with the destination SQL Server
Statement statement = connnection1.createStatement();
ResultSet resultSet = statement.executeQuery("select * from db.table");
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(connection2);
bulkCopy.setDestinationTableName("tableName");
bulkCopy.writeToServer(resultSet);
I'm getting following error while doing that:
com.microsoft.sqlserver.jdbc.SQLServerException: Data type float is not supported in bulk copy.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:226)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.getDestTypeFromSrcType(SQLServerBulkCopy.java:1443)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.createInsertBulkCommand(SQLServerBulkCopy.java:1464)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.sendBulkCopyCommand(SQLServerBulkCopy.java:1611)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.doInsertBulk(SQLServerBulkCopy.java:1553)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.access$200(SQLServerBulkCopy.java:63)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy$1InsertBulk.doExecute(SQLServerBulkCopy.java:705)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7240)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2869)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.sendBulkLoadBCP(SQLServerBulkCopy.java:733)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeToServer(SQLServerBulkCopy.java:1669)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeResultSet(SQLServerBulkCopy.java:641)
at com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeToServer(SQLServerBulkCopy.java:579)
Is there anyway to get around this issue?
Try creating SSIS package in SSDT. Or , use "Import and Export Data" tool if it's just basic import.

Matlab - [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt

I am running several SQL scripts from Matlab. For the last couple of months, there were no problems with it. However, since this week, I am starting to receive the following error:
Error using database.jdbc.connection/exec (line 53) [Microsoft][ODBC
SQL Server Driver]Connection is busy with results for another hstmt
I have installed my ODBC driver on Matlab correctly, and I run the SQL queries as follows:
% connect
conn = database('connectionName','user','password');
% I actually alter between two queries, but put the same here as an example
for i=1:2
querynew = "SELECT * FROM tbl";
results = exec(conn, querynew); % the error is triggered here
results = fetch(results);
Data = results.Data;
end
close(conn)
Try to close the cursor, using close(results):
for i=1:2
querynew = "SELECT * FROM tbl";
results = exec(conn, querynew); % the error is triggered here
results = fetch(results);
Data = results.Data;
close(results);
end
https://ch.mathworks.com/help/database/ug/close.html?requestedDomain=true

R: odbcDriverConnect Server name includes escape character

I am successfully querying what I need from the first Server with the following:
library(RODBC)
library(data.table)
library(sqldf)
library(zoo)
library(forecast)
library(plyr)
library(parallel)
myconn <- odbcDriverConnect("SERVER=SQL12p4;DRIVER=SQL Server")
ManagementHours <- sqlQuery(myconn, " SELECT .......")
However when I try to connect to a different server SQL12P4\VSQL1 I am unable to connect. I know that the \ is an escape character and I believe that is my problem. I am wondering if there is a way to get around this? Below is the R command I am currently using.
myconn1 <- odbcDriverConnect("SERVER=SQL12P4\VSQL1;DRIVER= SQL SERVER")
> myconn1A <-odbcDriverConnect("SERVER=SQL12P4\VSQL1;DRIVER= SQL SERVER")
Error: '\V' is an unrecognized escape in character string starting ""SERVER=SQL12P4\V"
> myconn1B <-odbcDriverConnect("SERVER=SQL12P4\\VSQL1;DRIVER= SQL SERVER")
Warning messages:
1: In odbcDriverConnect("SERVER=SQL12P4\\VSQL1;DRIVER= SQL SERVER") :
[RODBC] ERROR: state IM002, code 0, message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
2: In odbcDriverConnect("SERVER=SQL12P4\\VSQL1;DRIVER= SQL SERVER") :
ODBC connection failed

Resources