MySql INSERT Failure - mysql

Why does the following query fails on MySql?
NHibernate: INSERT INTO UserSetting (userId, key, value) VALUES (?p0, ?p1, ?p2);
?p0 = ccda78da-689d-4d86-ba72-d65eaf281edf [Type: Guid (0)], ?p1 = 'Hello' [Type: String (5)], ?p2 = 'World' [Type: String (5)]
With this error:
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 'key, value) VALUES ('ccda78da-689d-4d86-ba72-d65eaf281edf', 'Hello', 'World')' at line 1"}

key is a reserved word.
Try this:
INSERT INTO UserSetting (userId, `key`, value) VALUES (?p0, ?p1, ?p2)

Key is a reserved word, use ticks, example in link
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

Related

Inserting a substring value into an already existing entry

Currently I have a database with the following parameters and entry:
Now I want to fill the column status_from in this particular entry with the id 1 with a substring of the action column. Which is basically I want to fill my status_from column with a word that comes after from and before to of the action column. To achieve this I've tried using the following statement:
INSERT INTO crm_logs2(status_from) SELECT status_from WHERE id='1' VALUES (substring_index(substring_index(action, 'from', -1),'to', 1))
But doing this gave me the following error:
#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 'VALUES (substring_index(substring_index(action, 'from', -1), ...' at line 1
You need to use the UPDATE statement
UPDATE crm_logs2
SET status_from = (SUBSTRING_INDEX(SUBSTRING_INDEX(action, "from", -1), "to", 1))
WHERE id = 1
update crm_logs2
set action=(select SUBSTRING_INDEX(SUBSTRING_INDEX(action, ' ', 6), ' ', -1) from
crm_logs2 where id=1)where id=1
Try this and let us know if it works

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax;

sql = ("INSERT INTO {0} "
"(id, timestamp, status, priority, client, group, capacity, level, mail_id) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)".format(TABLE_NAME_MAIL))
values = ('NULL', report['timestamp'], 'succeeded', report['priority'], c.strip(), report['group'], 'NULL', 'NULL', ref_mailid)
cursor.execute(sql, values)
#cursor.execute('INSERT INTO %s VALUES (NULL,"%s","%s","%s","%s","%s",NULL,NULL,"%s") ' % (TABLE_NAME_REPORT, report['timestamp'], 'succeeded', report['priority'], c.strip(), report['group'], ref_mailid))
The commented out cursor.execute works, the uncommented throws an error:
_mysql_exceptions.ProgrammingError: (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 'group, capacity, level, mail_id) VALUES ('NULL', '2014-12-05 23:46:56', 'succeed' at line 1")
Column 'id' has AUTO_INCREMENT
Why am I getting this error?
group is a reserved keyword in MySQL. Use backticks to escape the name
INSERT INTO {0} (id, timestamp, status, priority, client, `group`, capacity ...
here--------------------------------^-----^
or even better use a different column name.
And don't use 'NULL' as parameter. Either use NULL without the quotes or remove it completely from the query:
INSERT INTO {0} (timestamp, ...
^----------no ID
When using an auto increment column, you have two options:
Insert a value in there (ie: Override the auto increment)
Do not set the column at all
1:
In case of setting a value, and you want to set "NULL" the column has to allow a NULL. This is rarely the case with an auto increment column since it would defeat its purpose.
So this is allowed:
INSERT INTO tab (id, sometext) VALUES (10,"Hi");
2:
Alternative (most used) is not to name the column at all:
INSERT INTO tab (sometext) VALUES ("Hi");
which would lead to mysql assigning a value to id in your table.

I can't seem to do a POST to MySQL...with the ORMs I created

create: function(tableInput, colname1, colname2, colname3, colname4, colname5, val1, val2, val3, val4, val5, callback) {
var queryString = "INSERT INTO books ?? (?? ?? ?? ?? ??) VALUES (? ? ? ? ?)";
console.log(queryString);
connection.query(queryString, [tableInput, colname1, colname2, colname3, colname4, colname5, val1, val2, val3, val4, val5], function(err, result) {
if (err) {
throw err;
}
callback(result);
});
},
Currently, stuck with this error message. The values is posting to the router and showing up in console.log... however there must be something wrong either with the parameter or querystring because it's not taking in any input.
-----------------------------
posting to crud
sakl k k lkl klk
INSERT INTO books ?? (?? ?? ?? ?? ??) VALUES (? ? ? ? ?)
C:\Users\nguye\documents\projects\mlbt\node_modules\mysql\lib\protocol\Parser.js:79
throw err; // Rethrow non-MySQL errors
^
Error: ER_PARSE_ERROR: 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 '`books` (`title` `author` `edition` `ISBN` `publisher`) VALUES ('sakl' 'k' 'k' '' at line 1
Two things that stick out:
The double question marks. Shouldn't they be also single ones?
You already have a table in your query string and then add another one via parameters (books). Remove the hardcoded one from the query string.

INSERT INTO not working on newest xampp

$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf) VALUES ($plan_name, $plan_nr, $anz)";
echo "<br />".$query."<br />";
if (!mysql_query($query) && !$error) {
die (mysql_error());
}
mysql-error tells me:
INSERT INTO 'p_stats' ('name', 'name_ID', 'anz_aufruf') VALUES ('Laptop 1', '1', '95')
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 ''p_stats' ('name', 'name_ID', 'anz_aufruf') VALUES ('Laptop 1', '1', '95')' at line 1
Where is the code wrong here?
the error is the wrapping of single quotes around table name.
Table names as well as column names are identifiers. They should be wrap if a name is a reserved keyword with backtick (optional if not). Single quotes are for string literal.
MySQL - when to use single quotes, double quotes, and backticks?
Use this.
$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf)
VALUES ('".$plan_name."', '".$plan_nr."', '".$anz."')";
$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf)
VALUES ('".$plan_name."', '".$plan_nr."', '".$anz."')";

Why can't I insert this into MySQL?

I probably made a stupid error on my syntax for the query, but I can't seam to fix it. Here is the query my program tries to execute:
INSERT INTO filez (
filename,
uploadedby,
dateuploaded,
public,
FileSize,
FileTransferSize,
key,
bytes
)
VALUES(
'tacct/tesABCscdsdasdasdD.testtest',
'tacct',
'%27 %December %2012, %7:%32:%15%AM',
1,
7,
7,
'`',
'TestDoc'
)
And here's the mysql_error:
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 'key, bytes) VALUES('tacct/tesABCscdsdasdasdD.testtest', 'tacct', '%27 %D' at line 1
I did mysql_real_escape_string() on EVERYTHING except the FileSize and FileTransferSize in the query. Could you tell me what I am doing wrong? Thanks!
The error tells you that the error happens at the text key, and it's quite correct: key is a reserved word in MySQL.
Where you use it as a field name, you must enclose it in backticks (`); it's a good general practice anyway.
So:
INSERT INTO `filez` (
`filename`,
`uploadedby`,
`dateuploaded`,
`public`,
`FileSize`,
`FileTransferSize`,
`key`,
`bytes`
)
VALUES(
'tommy3244/tesABCscdsdasdasdD.testtest',
'tommy3244',
'%27 %December %2012, %7:%32:%15%AM',
1,
7,
7,
'`',
'TestDoc'
)
key is a reserved word and you can not use it in column name without using backtics. try this
INSERT INTO filez (`filename`, `uploadedby`, `dateuploaded`, `public`, `FileSize`, `FileTransferSize`, `key`, `bytes`) VALUES('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244', '%27 %December %2012, %7:%32:%15%AM', 1, 7, 7, '`', 'TestDoc')
try using following query
INSERT INTO filez (filename, uploadedby, dateuploaded, public, FileSize, FileTransferSize, `key`, bytes) VALUES
('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244', '%27 %December %2012, %7:%32:%15%AM',
1, 7, 7, '`', 'TestDoc')
key is keyword so you have to use backticks as i used above.
For more info check following question too
Select a column with a keyword name
INSERT INTO `filez` (`filename`, `uploadedby`, `dateuploaded`,
`public`, `FileSize`, `FileTransferSize`,
`key`, `bytes`)
VALUES('tommy3244/tesABCscdsdasdasdD.testtest', 'tommy3244',
'%27 %December %2012, %7:%32:%15%AM', 1, 7, 7, '`', 'TestDoc')
ok,
syntax to use near 'key, bytes) both are reserve words, Good practice is always use ` sign for user defined identifiers.
KEY is reserved key word for mysql
try other column name or make backticks like that ``
The issue is key is the reserved keyword of mysql , you can't use it as column name, if you really want then enclose it inside backtick character
key