INSERT INTO "order" SET `user_id` = 5, `week_id` = 1, `box_settings_id` = '1', `delivery_day` = 'Thursday', `delivery_time` = 'Morning 6am - 9am', `notes` = '', `recipes` = '70, 71, 72, 74';
What is wrong with my SQL query? I'm getting this error in PHPMyAdmin:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '"order" SET `user_id` = 5, `week_id` = 1, `box_settings_id` = '1', `delivery_day' at line 1
While the SQL debugger isn't showing any error.
Issue likely is due to use of double quotes which MySQL and MariaDB diverge from the ANSI SQL standard. Instead of double quotes (synonymous to single quotes for string literals), MariaDB and MySQL would use backticks. With that said, you can adjust SQL mode to non-default ANSI or ANSI_QUOTES to support double quotes for identifiers:
ANSI_QUOTES
Changes " to be treated as `, the identifier quote character. This may break old MariaDB applications which assume that " is used as a string quote character.
Therefore, simply use backticks on order table like you do for columns. Generally, any identifier can use backticks.
INSERT INTO `order`
SET `user_id` = 5,
`week_id` = 1,
`box_settings_id` = '1',
`delivery_day` = 'Thursday',
`delivery_time` = 'Morning 6am - 9am',
`notes` = '',
`recipes` = '70, 71, 72, 74';
Related
I have this Query which i am trying to execute
$other = $this->electricityConnections->select('category_id')
->from('building_category_settings')
->where('building_id', '=', 52)
->where('hide_from_electricity_widget', '=', 1)
->groupBy('category_id')
->orderBy('kwh_used', 'desc');
$electCategory = $this->electricityConnections
->addselect(('MIN(IF(category.description IS NOT NULL,
category.description, your_electricity_yesterday_category.cat_desc)
as cat_desc'),
('SUM(kwh_used) as kwh_used'), ('SUM(cost) as cost'),
'your_electricity_yesterday_category.category_id')
->leftJoin('category as category',
'your_electricity_yesterday_category.category_id', '=', 'category.id')
->where('your_electricity_yesterday_category.category_id', '=', 11)
->where('your_electricity_yesterday_category.building_id', '=', 52)
->whereNotIn('your_electricity_yesterday_category.category_id', $other)
->get();
dd($electCategory);
But i keep getting this error
"message": "SQLSTATE[42000]: Syntax error or access violation: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.cat_desc) as cat_desc, SUM(kwh_used) as kwh_used,
SUM(cost) as cost' at line 1 (SQL: select
MIN(IF(category.description IS NOT NULL, category.description, your_electricity_yesterday_category.cat_desc) as cat_desc,
SUM(kwh_used) as kwh_used, SUM(cost) as cost,
your_electricity_yesterday_category.category_id from
your_electricity_yesterday_category left join category as
category on your_electricity_yesterday_category.category_id =
category.id where
your_electricity_yesterday_category.category_id = 11 and
your_electricity_yesterday_category.building_id = 52 and
your_electricity_yesterday_category.category_id not in (select
category_id from building_category_settings where building_id =
52 and hide_from_electricity_widget = 1 group by category_id order
by kwh_used desc))",
When I do toSql() to the code above I get the following below
"select `MIN(IF(category`.`description IS NOT NULL, category`.`description, your_electricity_yesterday_category`.`cat_desc)` as `cat_desc`, `SUM(kwh_used)` as `kwh_used`, `SUM(cost)` as `cost`, `your_electricity_yesterday_category`.`category_id` from `your_electricity_yesterday_category` left join `category` as `category` on `your_electricity_yesterday_category`.`category_id` = `category`.`id` where `your_electricity_yesterday_category`.`category_id` = ? and `your_electricity_yesterday_category`.`building_id` = ? and `your_electricity_yesterday_category`.`category_id` not in (select `category_id` from `building_category_settings` where `building_id` = ? and `hide_from_electricity_widget` = ? group by `category_id` order by `kwh_used` desc)"
What am i doing wrong?
I think the main problem is the addSelect part. You're missing a closing ) for MIN. Try using DB::raw when the selected columns include sql functions.
addselect(
DB::raw('MIN(IF(category.description IS NOT NULL, category.description, your_electricity_yesterday_category.cat_desc)) as cat_desc'),
DB::raw('SUM(kwh_used) as kwh_used'),
DB::raw('SUM(cost) as cost'),
'your_electricity_yesterday_category.category_id'
)
you are missing parentheses from end of below line
MIN(IF(category.description IS NOT NULL, category.description, your_electricity_yesterday_category.cat_desc)) as cat_desc
you need to close bracket before as cat_desc, this seems to be syntax issue.
You Can Use selectRaw, check docs: https://laravel.com/docs/9.x/queries#selectraw
->selectRaw("
MIN(IF(category.description IS NOT NULL, category.description, your_electricity_yesterday_category.cat_desc)) as cat_desc),
SUM(kwh_used) as kwh_used,
SUM(cost) as cost),
your_electricity_yesterday_category.category_id
")
. is a keyword in SQL. You may not used it as a column name without quoting it. In MySQL, things like column names are quoted using backticks, i.e. . with ``
Personally, I wouldn't bother; I'd just rename the column.
I have yaml file that contains the data that needs to go into the table, i need to convert each yaml object to sql. can anyone tell me what I can use to convert it to sql statement
for example,
- model: ss
pk: 2
fields: {created_by: xxx, created_date: !!timestamp '2018-09-13
17:50:30.821769+00:00',
modified_by: null, modified_date: null, record_version: 0, team_name:
privat, team_type: abc}
In python
data_loaded = None
with open("data.yaml", 'r') as stream:
data_loaded = yaml.load(stream)
if data_loaded:
cols, vals = data_loaded["fields"].items()
table = data_loaded["model"]
sql = "INSERT INTO %s %s VALUES %s;" % ( table, tuple(cols), tuple(vals))
Depending with SQL dialect you might need take additional care of literal, types (e.g. replacing double quotes with singles, timestamp format etc)
I'm trying to use the sqlSave function to insert the rows of a data.frame into a table in a Microsoft Access database. First of all, if I don't specify fast = FALSE I get a fatal error and R and RStudio crash completely with no error messages given. Changing fast to FALSE fixes this, but I get the error
unable to append to table ‘archiverapp_fcsfiles’
sqlSave conn matched archiverapp_fcsfiles TRUE TRUE FALSE TRUE FALSE
By setting verbose = TRUE, I can see what it is trying to do:
Query: INSERT INTO "archiverapp_fcsfiles" ( "experiment",
"fcsfilename",
"celltype",
"donor",
"time",
"readout",
"treat1",
"treat1conc",
"treat2",
"treat2conc",
"treat3",
"treat3conc" )
VALUES ( 2, 'Specimen_001_F10_F10_JUN s63_60 minutes.fcs', 'Donor 1',
'THP1', TNFa, '50', '60', NULL, NULL, NULL, NULL, JUN s63 )
sqlwrite returned
42000 -3100 [Microsoft][ODBC Microsoft Access Driver]
Syntax error (missing operator) in query expression 'JUN s63'.
[RODBC] ERROR: Could not SQLExecDirect
'INSERT INTO "archiverapp_fcsfiles" ( "experiment", "fcsfilename", "celltype",
"donor", "time", "readout", "treat1", "treat1conc", "treat2", "treat2conc",
"treat3", "treat3conc" )
VALUES ( 2, 'Specimen_001_F10_F10_JUN s63_60 minutes.fcs', 'Donor 1',
'THP1', TNFa, '50', '60', NULL, NULL, NULL, NULL, JUN s63 )'
As you can see, it is not adding quotes around the strings 'TNFa' and 'JUN s63'.
I'm not having problems like this with any of my other queries, but then again I'm not using fast = FALSE with them so it's probably using a different method.
Any idea on how to avoid this?
C# keeps coming up with an error:
Additional information: You have an error in your SQL syntax
My SQL statement is at the bottom in the picture.
Sql statement:
Update amazon_ordred_items
Set OrderItemId = 666854391288218,
SellerSKu = ..,
Title = 2pcs Fashion Cool Universal Black Real Original Car Headlight Eyelashes Sticker,
QuantityOrdered = 1,
QuantityShipped = 1,
CurrencyCode = USD,
Amount = 0.50,
ScheduledDeliveryEndDate = ..,
ScheduledDeliveryStartDate = ..,
PromotionIds = .,
Where ASIN = B00EISTU74
One thing which would make this a lot better is to add quotes around the text strings in the query:
update table
set sellerSK='.',
Title='2pcs Fashion...',
(etc)
Can't tell from the screenshot if this is the problem, but it certainly looks like it is an issue.
The problem is you are not using single quotes for any of your string values in the update statement. Put quotes on every value that is not an integer and the sql statement will work.
Also you have values of .. for some date fields. Assuming those are of an SQL data type DateTime or similar, this will not work. If you don't have a date, use Null instead.
Update amazon_ordred_items
Set OrderItemId = 666854391288218,
SellerSKu = Null,
Title = '2pcs Fashion Cool Universal Black Real Original Car Headlight Eyelashes Sticker',
QuantityOrdered = 1,
QuantityShipped = 1,
CurrencyCode = 'USD',
Amount = 0.50,
ScheduledDeliveryEndDate = Null,
ScheduledDeliveryStartDate = Null,
PromotionIds = Null,
Where ASIN = 'B00EISTU74'
Note also there's no need to update the ASIN field since you're not changing it's value and it is in your Where clause.
INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
($title, $introtext, $fulltext, 1, 1, $catid, 5)
Query is made up of php script. Derivation of variables shows that they are not empty.
At the request of phpMyAdmin shows the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
You need to use single quotes to indicate strings to SQL - use:
INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('". $title ."', '". $introtext ."', '". $fulltext ."', 1, 1, $catid, 5)
A safer means would be to use:
$query = sprintf("INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('%s', '%s', '%s', 1, 1, %d, 5)",
mysql_real_escape_string($title),
mysql_real_escape_string($introtext),
mysql_real_escape_string($fulltext),
$catid);
Reference:
SPRINTF
MYSQL_REAL_ESCAPE_STRING
You need to surround your PHP variables that are being inserted into string-type fields with quotes:
INSERT INTO `jos1_content`
(`title`, `introtext`, `fulltext`, `state`, `sectionid`, `catid`, `attribs`)
VALUES
('$title', '$introtext', '$fulltext', 1, 1, $catid, 5)
Read about prepared statements in the manual of your programming language or use mysql_escape_string.