I am using the below code to get the record from a access database. Now i got the below error Syntax error (missing operator) in query expression.
I can't able to understand this issue. Please help me to fix this issue.
cmd = new OleDbCommand(#"Select * from tbl_men_schedule where fld_mem_id=" + 0 + " and fld_startdate=" + Convert.ToDateTime(txt_startDate.Text) + " and fld_enddate=" + Convert.ToDateTime(txt_enddate.Text) + "", con);
da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Syntax error (missing operator) in query expression 'fld_mem_id=0 and fld_startdate=1/4/2013 12:00:00 AM and fld_enddate=4/11/2013 12:00:00 AM'.
If fld_startdate and fld_enddate are Date/Time data type, enclose those date values with the # delimiter.
where
fld_mem_id=0
and fld_startdate=#1/4/2013 12:00:00 AM#
and fld_enddate=#4/11/2013 12:00:00 AM#
If they are text data type, enclose them with quotes.
where
fld_mem_id=0
and fld_startdate='1/4/2013 12:00:00 AM'
and fld_enddate='4/11/2013 12:00:00 AM'
However if you use a parameter query instead, you wouldn't need to delimit those values.
Your statement should be -
"Select * from tbl_men_schedule where fld_mem_id=" + 0 + " and fld_startdate='" + Convert.ToDateTime(txt_startDate.Text) + "' and fld_enddate='" + Convert.ToDateTime(txt_enddate.Text) + "'"
Hope this helps you
If you hardcoding "0"... and you're missing single quotes.
"Select * from tbl_men_schedule where fld_mem_id=0 and fld_startdate='" +
txt_startDate.Text + "' and fld_enddate='" +
txt_enddate.Text + "'"
Related
I am trying to multiply a row by a variable (calculated amount):
double servingsMultiplier = 1;
double servingSizeMultiplier = 1;
Calculate the values for "servingsMultiplier" and "servingSizeMultiplier".
String selectQry5 = ("SELECT ci_id, cr_id, ci_ingedient, (ci_amount*servingsMultiplier) AS ci_amount, " +
" (ci_unit*servingSizeMultiplier) AS ci_unit " +
" FROM at_cat_ingredient " +
" WHERE cr_id = ? " +
" ORDER BY ci_ingedient;");
The above works when I use a constant (e.g., 2); however, not when I use a variable. I get the error message:
"SQLException in recipePDF:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown
column 'servingsMultiplier' in 'field list'.
An identifier like servingsMultiplier inside the sql statement is not recognized as the value of the variable but as a column name, which of course does not exist.
Use ? placeholders for servingsMultiplier and servingSizeMultiplier in the statement and pass their values just like you pass the parameter in the WHERE clause:
String selectQry5 =
"SELECT ci_id, cr_id, ci_ingedient, " +
"(ci_amount * ?) AS ci_amount, " +
"(ci_unit * ?) AS ci_unit " +
"FROM at_cat_ingredient " +
"WHERE cr_id = ? " +
"ORDER BY ci_ingedient;";
If you want to use mysql variables, then add # before variable name.
SELECT ci_id, cr_id, ci_ingedient, (ci_amount*#servingsMultiplier) AS ci_amount,
(ci_unit*#servingSizeMultiplier) AS ci_unit
FROM at_cat_ingredient
WHERE cr_id = #id
ORDER BY ci_ingedient;
Get an error when writing to the database
The function for it:
var newMsg = { payload: msg.payload };
newMsg.topic="insert into MyTable (a,b,c,d,e,f,g) values (newMsg.payload)"
The incoming payload debug shows
payload: "B0:AC:A2:AC:07:F4","Ready","893901","860990","online","876","333"
The error I get from the database node (nore-red-node-mysql) is
"Error: ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value
count at row 1"
The strange thing to me is that if I try a
newMsg.topic="insert into MyTable (a,b,c,d,e,f,g) values (\"B0:AC:A2:AC:07:F4\",\"Ready\",\"893901\",\"860990\",\"online\",\"876\",\"333\")"
it works perfectly...
Where is the trick?
There is no trick.
This is because the node-red-node-mysql and node-red-contrib-sqldbs nodes do not do any query substitution.
This means that what gets sent to the database is exactly what is in the msg.topic field. In this case that would have been:
insert into MyTable (a,b,c,d,e,f,g) values (newMsg.payload)
Which mysql will read as trying to pass a single value to a query expecting 7 values.
You will have to build the full query (and do your own variable escaping if needed) in a function node before passing the message to the database node.
at the end I solved it this way:
var data = msg.payload.split(",");
msg.payload = {};
msg.payload.a=data[0];
msg.payload.b=data[1];
msg.payload.c=data[2];
msg.payload.d=data[3];
msg.payload.e=data[4];
msg.payload.f=data[5];
msg.payload.g=data[6];
insert into MyTable (a,b,c,d,e,f,g) values ('" + data[0] + "','" + data[1] + "','" + data[2] + "','" + data[3] + "','" + data[4] + "','" + data[5] + "','" + data[6] + "')";
return msg;
In my code I understand query getting null values and it throws this error. But since my query is little complex I don't understand how do I check for null values and avoid this error. Please help me to correct this query.
SELECT (SUM(charges) + SUM(behaviour) + SUM(admission) + SUM(properInformation) + SUM(hygine) + SUM(treatment))/(count(doctorID) * 6) AverageRating, COUNT(ID) RatingCount from ratings where doctorID = '" + doctorID + "'
If you want the query to not return NULL, you can just surround the expression with IFNULL to convert a possible NULL to 0, something like;
SELECT IFNULL((SUM(charges) + SUM(behaviour) + SUM(admission) +
SUM(properInformation) + SUM(hygine) + SUM(treatment))
/(count(doctorID) * 6), 0) AverageRating,
COUNT(ID) RatingCount
FROM ratings
WHERE doctorID = '" + doctorID + "'
If you definitely know your query returning null value correctly, then you can use try-catch block as below:
Try
Dim dt As DataTable = Me.GetData("SELECT (SUM(charges) + SUM(behaviour) + SUM(admission) + SUM(properInformation)
Catch ex As Exception
MsgBox("Error while fetching data" & vbCrLf & ex.Message)
End Try
I have a problem in this query:
string sqlString = "DELETE FROM [upload_news] WHERE (SELECT TOP " + no_of_recordss + " * FROM [upload_news] WHERE [country]='" + countryy.Text + "')";
Error Message :
Error: {"An expression of non-boolean type specified in a context
where a condition is expected, near ')'."}
How can i fix this ?
In the where clause you need a boolean expression.
Moreover, mysql doesn't support select top, you have to use limit instead and you can use it directly on delete
So your query should be:
delete from upload_news
where country=<SOME_COUNTRY> limit <NO_OF_RECORDS>
You have to replace values within "<>" with your desired values.
Or in your "strange" syntax:
string sqlString = "DELETE FROM [upload_news] WHERE [country]='" + countryy.Text + "' limit "+no_of_recordss;
I've written following code:
Dim date1 As Date
Dim date2 As Date
date1 = Convert.ToDateTime(DatePickerFromDate.Text)
date2 = Convert.ToDateTime(DatePickerToDate.Text)
Dim cnd As New OleDbCommand("SELECT * FROM Sales WHERE Invoice_Date BETWEEN " + date1 + " AND " + date2 + "", om)
om.Open()
Dim da As OleDbDataReader = cnd.ExecuteReader
While da.Read()
ComboBox1.Items.Add(da(0))
End While
da.Close()
om.Close()
I want to retrieve data between two dates that are been taken from two datepickers.
I tried BETWEEN, also i tried >= =< but result was empty though database contains data. Please help where I'm getting wrong
Your code is probably generating an error. When doing this type of querying, you should store the query string after substitution and print it out. You seem to be missing delimiters around the dates. So this may work in your specific case.
New OleDbCommand("SELECT * FROM Sales WHERE Invoice_Date BETWEEN '" + date1 + "' AND '" + date2 + "'", om)
However, you then need to be careful about the format of the dates. The application layer and the database might use different formats. If you are substituting directly into the query string, then use the format YYYY-MM-DD -- it is the ISO standard date format and generally understood.
Even better is to learn how to parameterize queries so you can actually pass in the date values as date parameters.
If you're using MS Access, this should be the syntax...
SELECT * FROM Sales WHERE Invoice_Date>=#" + date1 + "# and Invoice_Date<=#" + date2 + "#"
If you're using MS SQL Server or MySQL, then do something like this...
SELECT * FROM Sales WHERE Invoice_Date>='" + date1 + "' and Invoice_Date<='" + date2 + "'"