“Insert Into” statement causing errors due to “Parameter 7 (”“): The supplied value is not a valid...
I'm loading a batch of CSV files into a SQL Server table using Python one row at a time. The files each contain a number of free text fields and erroneous data which I trim and rename before attempting to insert.
In general (about 95% of the time), the code seems to work however exceptions appear with the error message described below.
I'm confused as a) I only have four columns in my table, and can't understand why it would be looking for Parameter 7, and b) the text columns are being loaded into nvarchar(max) formatted columns, so I wouldn't expect a data type error.
I've checked the source files to see which rows threw an error, there seems to be no discernible difference between the problem rows and others that are successfully loaded.
I've trimmed the process right back to only insert the JobID (as a bigint) and it works without issue, but as soon as I bring in the text fields, it causes an error.
I'm using Python 3.7.0 and loading into SQL Server 14.0
import numpy as np
import pyodbc
import os
import glob
import pandas as pd
import csv
import config
import urllib
import shutil
import codecs
path = "C:\myFilePath"
allFiles = glob.glob(os.path.join(path, "*.csv"))
for file_ in allFiles:
df = pd.concat((pd.read_csv(f, encoding='utf8') for f in allFiles))
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
"Server=myServer;"
"Database=myDatabase;"
"Trusted_Connection=yes;"
"SelectMethod=cursor;"
)
df2 = df[['JobID', 'NPS_score', 'Obtuse_Column_Name_1', 'Obtuse_Column_Name_2']].copy()
df2.columns = ['JobID', 'Score','Q1', 'Q2']
cursor = cnxn.cursor()
for index,row in df2.iterrows():
try:
counter = counter + 1
cursor.execute("""insert into [myDB].[dbo].[input_test]( [JobID], [Score], [Q1], [Q2]) VALUES (?, ?, ?, ?)""", row['JobID'],row['Score'],row['Q1'], row['Q2'])
cursor.commit()
print(counter)
except Exception as e:
print(e)
continue
cursor.close()
cnxn.close()
I expect the data to be loaded but on some lines get the following error code:
('42000', '[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL
Server]The incoming tabular data stream (TDS) remote procedure call
(RPC) protocol stream is incorrect. Parameter 7 (""): The supplied
value is not a valid instance of data type float. Check the source
data for invalid values. An example of an invalid value is data of
numeric type with scale greater than precision. (8023)
(SQLExecDirectW)')
python sql-server
|
show 3 more comments
I'm loading a batch of CSV files into a SQL Server table using Python one row at a time. The files each contain a number of free text fields and erroneous data which I trim and rename before attempting to insert.
In general (about 95% of the time), the code seems to work however exceptions appear with the error message described below.
I'm confused as a) I only have four columns in my table, and can't understand why it would be looking for Parameter 7, and b) the text columns are being loaded into nvarchar(max) formatted columns, so I wouldn't expect a data type error.
I've checked the source files to see which rows threw an error, there seems to be no discernible difference between the problem rows and others that are successfully loaded.
I've trimmed the process right back to only insert the JobID (as a bigint) and it works without issue, but as soon as I bring in the text fields, it causes an error.
I'm using Python 3.7.0 and loading into SQL Server 14.0
import numpy as np
import pyodbc
import os
import glob
import pandas as pd
import csv
import config
import urllib
import shutil
import codecs
path = "C:\myFilePath"
allFiles = glob.glob(os.path.join(path, "*.csv"))
for file_ in allFiles:
df = pd.concat((pd.read_csv(f, encoding='utf8') for f in allFiles))
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
"Server=myServer;"
"Database=myDatabase;"
"Trusted_Connection=yes;"
"SelectMethod=cursor;"
)
df2 = df[['JobID', 'NPS_score', 'Obtuse_Column_Name_1', 'Obtuse_Column_Name_2']].copy()
df2.columns = ['JobID', 'Score','Q1', 'Q2']
cursor = cnxn.cursor()
for index,row in df2.iterrows():
try:
counter = counter + 1
cursor.execute("""insert into [myDB].[dbo].[input_test]( [JobID], [Score], [Q1], [Q2]) VALUES (?, ?, ?, ?)""", row['JobID'],row['Score'],row['Q1'], row['Q2'])
cursor.commit()
print(counter)
except Exception as e:
print(e)
continue
cursor.close()
cnxn.close()
I expect the data to be loaded but on some lines get the following error code:
('42000', '[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL
Server]The incoming tabular data stream (TDS) remote procedure call
(RPC) protocol stream is incorrect. Parameter 7 (""): The supplied
value is not a valid instance of data type float. Check the source
data for invalid values. An example of an invalid value is data of
numeric type with scale greater than precision. (8023)
(SQLExecDirectW)')
python sql-server
I would first try to debug by printing the insert statement. It seems that the Q2 value is empty string and is illegal given the table definition. BTW why not use pandas to sql? pandas.pydata.org/pandas-docs/stable/generated/…
– Tom Ron
Dec 31 '18 at 11:18
Thanks @TomRon, I did check to see if Q2 was blank but it wasn't. Also, the table should allow for nulls in those columns (as many people just give a score without question text). I also did try to_sql first but ran into a problem when it went over 1000 rows. I thought going line by line would be a suitable alternative
– Ron Sibayan
Dec 31 '18 at 11:29
1
Hi @Ron Sibayan and welcome! Since the problem only occurs for some rows in your data, I think it might help if you could identify that row and rewrite your example to include it (or an obfuscated example that shows the same behaviour). It is often difficult to get help on non-reproducible questions.
– steinar
Dec 31 '18 at 11:29
Thank you @steinar, I'll try and pull out a few examples right now
– Ron Sibayan
Dec 31 '18 at 11:30
what are the type of columns in the database?
– Salman A
Dec 31 '18 at 12:05
|
show 3 more comments
I'm loading a batch of CSV files into a SQL Server table using Python one row at a time. The files each contain a number of free text fields and erroneous data which I trim and rename before attempting to insert.
In general (about 95% of the time), the code seems to work however exceptions appear with the error message described below.
I'm confused as a) I only have four columns in my table, and can't understand why it would be looking for Parameter 7, and b) the text columns are being loaded into nvarchar(max) formatted columns, so I wouldn't expect a data type error.
I've checked the source files to see which rows threw an error, there seems to be no discernible difference between the problem rows and others that are successfully loaded.
I've trimmed the process right back to only insert the JobID (as a bigint) and it works without issue, but as soon as I bring in the text fields, it causes an error.
I'm using Python 3.7.0 and loading into SQL Server 14.0
import numpy as np
import pyodbc
import os
import glob
import pandas as pd
import csv
import config
import urllib
import shutil
import codecs
path = "C:\myFilePath"
allFiles = glob.glob(os.path.join(path, "*.csv"))
for file_ in allFiles:
df = pd.concat((pd.read_csv(f, encoding='utf8') for f in allFiles))
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
"Server=myServer;"
"Database=myDatabase;"
"Trusted_Connection=yes;"
"SelectMethod=cursor;"
)
df2 = df[['JobID', 'NPS_score', 'Obtuse_Column_Name_1', 'Obtuse_Column_Name_2']].copy()
df2.columns = ['JobID', 'Score','Q1', 'Q2']
cursor = cnxn.cursor()
for index,row in df2.iterrows():
try:
counter = counter + 1
cursor.execute("""insert into [myDB].[dbo].[input_test]( [JobID], [Score], [Q1], [Q2]) VALUES (?, ?, ?, ?)""", row['JobID'],row['Score'],row['Q1'], row['Q2'])
cursor.commit()
print(counter)
except Exception as e:
print(e)
continue
cursor.close()
cnxn.close()
I expect the data to be loaded but on some lines get the following error code:
('42000', '[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL
Server]The incoming tabular data stream (TDS) remote procedure call
(RPC) protocol stream is incorrect. Parameter 7 (""): The supplied
value is not a valid instance of data type float. Check the source
data for invalid values. An example of an invalid value is data of
numeric type with scale greater than precision. (8023)
(SQLExecDirectW)')
python sql-server
I'm loading a batch of CSV files into a SQL Server table using Python one row at a time. The files each contain a number of free text fields and erroneous data which I trim and rename before attempting to insert.
In general (about 95% of the time), the code seems to work however exceptions appear with the error message described below.
I'm confused as a) I only have four columns in my table, and can't understand why it would be looking for Parameter 7, and b) the text columns are being loaded into nvarchar(max) formatted columns, so I wouldn't expect a data type error.
I've checked the source files to see which rows threw an error, there seems to be no discernible difference between the problem rows and others that are successfully loaded.
I've trimmed the process right back to only insert the JobID (as a bigint) and it works without issue, but as soon as I bring in the text fields, it causes an error.
I'm using Python 3.7.0 and loading into SQL Server 14.0
import numpy as np
import pyodbc
import os
import glob
import pandas as pd
import csv
import config
import urllib
import shutil
import codecs
path = "C:\myFilePath"
allFiles = glob.glob(os.path.join(path, "*.csv"))
for file_ in allFiles:
df = pd.concat((pd.read_csv(f, encoding='utf8') for f in allFiles))
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
"Server=myServer;"
"Database=myDatabase;"
"Trusted_Connection=yes;"
"SelectMethod=cursor;"
)
df2 = df[['JobID', 'NPS_score', 'Obtuse_Column_Name_1', 'Obtuse_Column_Name_2']].copy()
df2.columns = ['JobID', 'Score','Q1', 'Q2']
cursor = cnxn.cursor()
for index,row in df2.iterrows():
try:
counter = counter + 1
cursor.execute("""insert into [myDB].[dbo].[input_test]( [JobID], [Score], [Q1], [Q2]) VALUES (?, ?, ?, ?)""", row['JobID'],row['Score'],row['Q1'], row['Q2'])
cursor.commit()
print(counter)
except Exception as e:
print(e)
continue
cursor.close()
cnxn.close()
I expect the data to be loaded but on some lines get the following error code:
('42000', '[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL
Server]The incoming tabular data stream (TDS) remote procedure call
(RPC) protocol stream is incorrect. Parameter 7 (""): The supplied
value is not a valid instance of data type float. Check the source
data for invalid values. An example of an invalid value is data of
numeric type with scale greater than precision. (8023)
(SQLExecDirectW)')
python sql-server
python sql-server
edited Jan 5 at 22:50
anothermh
3,28831632
3,28831632
asked Dec 31 '18 at 11:06
Ron SibayanRon Sibayan
14
14
I would first try to debug by printing the insert statement. It seems that the Q2 value is empty string and is illegal given the table definition. BTW why not use pandas to sql? pandas.pydata.org/pandas-docs/stable/generated/…
– Tom Ron
Dec 31 '18 at 11:18
Thanks @TomRon, I did check to see if Q2 was blank but it wasn't. Also, the table should allow for nulls in those columns (as many people just give a score without question text). I also did try to_sql first but ran into a problem when it went over 1000 rows. I thought going line by line would be a suitable alternative
– Ron Sibayan
Dec 31 '18 at 11:29
1
Hi @Ron Sibayan and welcome! Since the problem only occurs for some rows in your data, I think it might help if you could identify that row and rewrite your example to include it (or an obfuscated example that shows the same behaviour). It is often difficult to get help on non-reproducible questions.
– steinar
Dec 31 '18 at 11:29
Thank you @steinar, I'll try and pull out a few examples right now
– Ron Sibayan
Dec 31 '18 at 11:30
what are the type of columns in the database?
– Salman A
Dec 31 '18 at 12:05
|
show 3 more comments
I would first try to debug by printing the insert statement. It seems that the Q2 value is empty string and is illegal given the table definition. BTW why not use pandas to sql? pandas.pydata.org/pandas-docs/stable/generated/…
– Tom Ron
Dec 31 '18 at 11:18
Thanks @TomRon, I did check to see if Q2 was blank but it wasn't. Also, the table should allow for nulls in those columns (as many people just give a score without question text). I also did try to_sql first but ran into a problem when it went over 1000 rows. I thought going line by line would be a suitable alternative
– Ron Sibayan
Dec 31 '18 at 11:29
1
Hi @Ron Sibayan and welcome! Since the problem only occurs for some rows in your data, I think it might help if you could identify that row and rewrite your example to include it (or an obfuscated example that shows the same behaviour). It is often difficult to get help on non-reproducible questions.
– steinar
Dec 31 '18 at 11:29
Thank you @steinar, I'll try and pull out a few examples right now
– Ron Sibayan
Dec 31 '18 at 11:30
what are the type of columns in the database?
– Salman A
Dec 31 '18 at 12:05
I would first try to debug by printing the insert statement. It seems that the Q2 value is empty string and is illegal given the table definition. BTW why not use pandas to sql? pandas.pydata.org/pandas-docs/stable/generated/…
– Tom Ron
Dec 31 '18 at 11:18
I would first try to debug by printing the insert statement. It seems that the Q2 value is empty string and is illegal given the table definition. BTW why not use pandas to sql? pandas.pydata.org/pandas-docs/stable/generated/…
– Tom Ron
Dec 31 '18 at 11:18
Thanks @TomRon, I did check to see if Q2 was blank but it wasn't. Also, the table should allow for nulls in those columns (as many people just give a score without question text). I also did try to_sql first but ran into a problem when it went over 1000 rows. I thought going line by line would be a suitable alternative
– Ron Sibayan
Dec 31 '18 at 11:29
Thanks @TomRon, I did check to see if Q2 was blank but it wasn't. Also, the table should allow for nulls in those columns (as many people just give a score without question text). I also did try to_sql first but ran into a problem when it went over 1000 rows. I thought going line by line would be a suitable alternative
– Ron Sibayan
Dec 31 '18 at 11:29
1
1
Hi @Ron Sibayan and welcome! Since the problem only occurs for some rows in your data, I think it might help if you could identify that row and rewrite your example to include it (or an obfuscated example that shows the same behaviour). It is often difficult to get help on non-reproducible questions.
– steinar
Dec 31 '18 at 11:29
Hi @Ron Sibayan and welcome! Since the problem only occurs for some rows in your data, I think it might help if you could identify that row and rewrite your example to include it (or an obfuscated example that shows the same behaviour). It is often difficult to get help on non-reproducible questions.
– steinar
Dec 31 '18 at 11:29
Thank you @steinar, I'll try and pull out a few examples right now
– Ron Sibayan
Dec 31 '18 at 11:30
Thank you @steinar, I'll try and pull out a few examples right now
– Ron Sibayan
Dec 31 '18 at 11:30
what are the type of columns in the database?
– Salman A
Dec 31 '18 at 12:05
what are the type of columns in the database?
– Salman A
Dec 31 '18 at 12:05
|
show 3 more comments
1 Answer
1
active
oldest
votes
I have found and solved the issue. Following @TomRon's advise, I printed the insert statement into the error block. This showed that despite me assuming Q2 was populated (as it was in the CSV) when I loaded it into a dataframe, for some reason it was NaN.
I simply included the function to replace NaN with 0 and it now loads all rows succesfully using the following line of code right after I create the dataframe I want to load (df2)
df2 = df2.fillna(value=0)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986727%2finsert-into-statement-causing-errors-due-to-parameter-7-the-supplied-va%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have found and solved the issue. Following @TomRon's advise, I printed the insert statement into the error block. This showed that despite me assuming Q2 was populated (as it was in the CSV) when I loaded it into a dataframe, for some reason it was NaN.
I simply included the function to replace NaN with 0 and it now loads all rows succesfully using the following line of code right after I create the dataframe I want to load (df2)
df2 = df2.fillna(value=0)
add a comment |
I have found and solved the issue. Following @TomRon's advise, I printed the insert statement into the error block. This showed that despite me assuming Q2 was populated (as it was in the CSV) when I loaded it into a dataframe, for some reason it was NaN.
I simply included the function to replace NaN with 0 and it now loads all rows succesfully using the following line of code right after I create the dataframe I want to load (df2)
df2 = df2.fillna(value=0)
add a comment |
I have found and solved the issue. Following @TomRon's advise, I printed the insert statement into the error block. This showed that despite me assuming Q2 was populated (as it was in the CSV) when I loaded it into a dataframe, for some reason it was NaN.
I simply included the function to replace NaN with 0 and it now loads all rows succesfully using the following line of code right after I create the dataframe I want to load (df2)
df2 = df2.fillna(value=0)
I have found and solved the issue. Following @TomRon's advise, I printed the insert statement into the error block. This showed that despite me assuming Q2 was populated (as it was in the CSV) when I loaded it into a dataframe, for some reason it was NaN.
I simply included the function to replace NaN with 0 and it now loads all rows succesfully using the following line of code right after I create the dataframe I want to load (df2)
df2 = df2.fillna(value=0)
answered Dec 31 '18 at 13:12
Ron SibayanRon Sibayan
14
14
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986727%2finsert-into-statement-causing-errors-due-to-parameter-7-the-supplied-va%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I would first try to debug by printing the insert statement. It seems that the Q2 value is empty string and is illegal given the table definition. BTW why not use pandas to sql? pandas.pydata.org/pandas-docs/stable/generated/…
– Tom Ron
Dec 31 '18 at 11:18
Thanks @TomRon, I did check to see if Q2 was blank but it wasn't. Also, the table should allow for nulls in those columns (as many people just give a score without question text). I also did try to_sql first but ran into a problem when it went over 1000 rows. I thought going line by line would be a suitable alternative
– Ron Sibayan
Dec 31 '18 at 11:29
1
Hi @Ron Sibayan and welcome! Since the problem only occurs for some rows in your data, I think it might help if you could identify that row and rewrite your example to include it (or an obfuscated example that shows the same behaviour). It is often difficult to get help on non-reproducible questions.
– steinar
Dec 31 '18 at 11:29
Thank you @steinar, I'll try and pull out a few examples right now
– Ron Sibayan
Dec 31 '18 at 11:30
what are the type of columns in the database?
– Salman A
Dec 31 '18 at 12:05