pyodbc bulk Insert - pyodbc

With SQL Server ODBC Driver 17, Object nulls is not an issue. But it gives error below on dates and integers
Invalid character value for cast specification (0) (SQLExecute)')
Process works fine when cursor.fast_executemany = False, or when default values (0, 1900-01-01) set for the null dates and integers.
Is there a way to bulk insert keeping the Nulls in place?

Related

Issue with replacing NULL sqlite3 database column values with other types in Python 3?

I've run into a problem with the sqlite3 module in Python 3, where I can't seem to figure out how to replace NULL values from the database with other ones, mainly strings and integers.
This command doesn't do the job, but also raises no exceptions:
UPDATE table SET animal='cat' WHERE animal=NULL AND id=32
The database table column "animal" is of type TEXT and gets filled with NULLs where no other value has been specified.
The column "id" is primary keyed and thus features only unique integer row indices.
If the column "animal" is defined, not NULL, the above command works flawlessly.
I can replace existing strings, integers, and floats with it.
What am I overlooking here?
Thanks.
The NULL value in SQL is special, and to compare values against it you need to use the IS and IS NOT operators. So your query should be this:
UPDATE table
SET animal = 'cat'
WHERE animal IS NULL AND id = 32;
NULL by definition means "unknown" in SQL, and so comparing a column directly against it with = also produces an unknown result.

dBWriteTable cannot write NULLs into SQL Server table for date fields

I cannot insert NULL (NA) values into an SQL Server Date column field using dbWriteTable.
dbWriteTable(con, "BIOutput", data[Cdate == date,], append = TRUE);
I get the error:
Error in result_insert_dataframe(rs#ptr, values) :
nanodbc/nanodbc.cpp:1587: 22007: [Microsoft][ODBC SQL Server Driver]
[SQL Server]Conversion failed when converting date and/or time from
character string.
I understand this works for numeric fields, but why doesn't it work for Dates? What workaround can I use?

Inserting a Datetime value into SQL Server CE in Webmatrix 3

I want to insert a datetime value into a database using SQL Server Compact Edition in Microsoft Webmatrix 3.
I tried the following query:
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', CONVERT(DATETIME, '07-23-08', 110));
And I got the following error message:
The conversion is not supported. [ Type to convert from (if known) = datetime, Type to convert to (if known) = float ]
Try with
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', CONVERT(DATETIME, '07-23-08', 10));
If you set the style value to 10 the input format must be mm-dd-yy, if you add 100 to the style value the expected format has a four digit year (110 --> mm-dd-yyyy).
For an exhaustive table of the style values look at CAST and CONVERT (SQL Server Compact).
By the way, you could take advantage of an implicit conversion using a date format like yyyymmdd or yyyy-mm-dd:
INSERT INTO Tutorials ([Tutorial], [StartDate])
VALUES ('3d', '20080723');

Returning varchar(max) Output parameter from stored procedure truncating to 4000 characters

I've got a classic ASP appln with a SQL2012 database. I recently changed a table column from varchar(8000) to varchar(max) as it wasn't big enough to store the required data.
I can update the column with all of the data I need to store, but the SP I use to return the column data as an output parameter is only returning 4000 characters (at least that is what the result of the following code is giving me:
Len(cmd.Parameters("#detail").Value)
I'm using the following parameter declaration as part of the call to the SP:
cmd.Parameters.Append cmd.CreateParameter("#detail", 8, 2, -1, strDetail)
8 being the value for adBStr. I've tried changing the 8 to 200, 201 and 203 but this gives the following error:
Error: 800a0e7c
Description:
Parameter object is improperly defined. Inconsistent or incomplete information was provided.
I thought updating the data would be the hard bit, but I just cant work out how to retrieve the entire contents of the column.
I'm returning the DATALENGTH of the column and it says it is 10,536 in length but I'm only getting 4,000 characters including spaces returned via the output parameter. I can see all of the data (10k chars) from Visual Studio so I know its in there.
My connection string Provider=SQLOLEDB.1. Could this be an issue? Should I be using the newer SQL Server Native Client 11.0 OLE DB Provider - SQLNCLI11??
Anyone got any ideas?
Cheers,
Mike.
Your assumption about the connection string is spot on
You need to the use the SQL Server Native Client instead of SQLOLEDB.1 to support the VARCHAR(MAX) and NVARCHAR(MAX) data types otherwise they will be truncated back to there SQLOLEDB equivalents.
You then want to be using the following parameter definitions
'For varchar(max) OUTPUT use;
Call cmd.Parameters.Append(cmd.CreateParameter("#detail", adLongVarChar, adParamOutput, -1, strDetail))
'For nvarchar(max) OUTPUT use;
Call cmd.Parameters.Append(cmd.CreateParameter("#detail", adLongVarWChar, adParamOutput, -1, strDetail))
'** Constants **
' adLongVarChar = 201
' adLongVarWChar = 203
' adParamOutput = 2

RS-DBI driver warning: (unrecognized MySQL field type 7 in column 1 imported as character)

I'm trying to run a simple query that works with MySQL or other MySQL connector API's,
SELECT * FROM `table` WHERE type = 'farmer'
I've tried various methods using the RMySQL package and they all get the same error
RS-DBI driver warning: (unrecognized MySQL field type 7 in column 1 imported as character)
Type = 'farmer'
(Query<-paste0("SELECT * FROM `table` WHERE type = '%",Type,"%'"))
res<-dbGetQuery(con, Query)
Query<-paste("SELECT * FROM `table` WHERE type = \'farmer\'")
Query<-paste("SELECT * FROM `table` WHERE type = 'farmer'")
What am I doing wrong?
"type" is a keyword in MYSQL. Surround the it with backticks to escape field names.
SELECT * FROM `table` WHERE `type` = 'farmer'
Also you probably have a time stamp column in your table. R is known to not recognize that column type. Convert it to a unix time stamp in the portion of the SQL statement.
Looks like the db schema has something in column which is of type 7 -- and that type appears to be unknown to the RMySQL driver.
I try to exclude column one in the query, or cast it at the select * ... level eg via something like
select foo as character, bar, bim, bom from 'table' where ...
To be clear, when I encountered this error message, it was because my data field was a time stamp.
I verified this by changing my query to SELECT created_at FROM ... which caused the error. I also verified this by changing the query not to include the column names that were timestamps, then I had no errors.
Note too, that the error message counts columns starting from 0 (instead of 1 as R does)
IMHO, the answer is you aren't doing anything wrong, but it's something that needs to be fixed in RMySQL.
The workaround is after you read in your data, you need to call one of the several possible character to datetime conversion functions. (Which one depends on what you want to do with the time stamp exactly.)

Resources