Cannot insert ' into my MySQL database in java [duplicate] - mysql

This question already has answers here:
How to escape single quotes in MySQL
(19 answers)
Closed 5 years ago.
I have designed java application & mysql database. Textfield also allow to accept whole characters. MySQL database data collation is set to utf8-default & MySQL server version is 5.7. I can type ' character in textfield. But I cannot execute sql syntax with ' in my query.
ex. Name's is not working, but names is working.

Try using an extra ' character
If the name is O'Brain , use it as O''Brain
For example
Select * from employees where name like 'O''Brain'

Make sure you gave same name to your database column name, where you parsing value from the android app.
or
specify your code so we can get more idea.

Related

MySQL throws an error when receiving my query [duplicate]

This question already has an answer here:
How to escape value keyword in mysql while using Select statement
(1 answer)
Closed 3 years ago.
I have to access a MySQL database that looks like this:
LOG_ID KEY TARGET CREATOR
1 okaytest 297d09d5-55fe faec09c0-159e
I can do the following query:
SELECT * FROM DATABASE WHERE LOG_ID=1
This would return me the column correctly.
But I can't do the following query.
SELECT * FROM DATABASE WHERE KEY='okaytest'
I believe that there might be a problem with the word KEY being a reserved keyword in MySQL, but I have to access that specific database, I can't change it's name and I must select it from the key
try like below by using the backtick `
SELECT * FROM DATABASE WHERE `KEY`='okaytest'
actually for reserve word you have to use this backtick otherwise it will thorow error here is the reserve word list
another options better not to use reserve word or incase of necessary you have use it by using backtick

Error inserting data into varchar that contains apostrophe? [duplicate]

This question already has an answer here:
Escaping a single quotation within SQL query
(1 answer)
Closed 7 years ago.
I have ruby script that includes a mysql insert that is working fine until it gets to a row that contains data containing an apostrophe. This row is also being populated using a variable and Im unsure how to escape the character so the insert will work successfully.
Any ideas?
Use the quote method on the connection object:
quote(value, column = nil)
API Documentation Link
Quotes the column value to help prevent SQL injection attacks.
Example:
my_name = ActiveRecord::Base.connection.quote("John O'Neil")
my_address = ActiveRecord::Base.connection.quote("R'lyeh")
query = "INSERT INTO companies (name,address) VALUES (#{my_name}, #{my_address})"
ActiveRecord::Base.connection.execute(query);
Original Post:
See this post: Escaping a single quotation within SQL query

why won't mySQL let me SET return=1? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
It makes no sense. I have a TINYINT field that is either 0 or 1 and I'm trying to set it like this:
UPDATE db_products SET return='1' WHERE product_id=342343434
But it tells me there's a mySQL syntax error. What am I doing wrong here? Is the problem that the field is called return? If so, how do I get around it without changing it?
In most cases, you can get around reserved word problems by enclosing with "ticks"; the quote-like thing that usually shares the ~ key.
UPDATE db_products SET `return`='1' WHERE product_id=342343434

Is there any function in MySQL for Regex Replace? [duplicate]

This question already has answers here:
How to do a regular expression replace in MySQL?
(13 answers)
Closed 6 years ago.
I have a table which needs stores some name and i need to replace a few characters before comparing them with another string
For instance, My table data is
abc
ghi:dki
ioe dsa
i read a string from user, which is of the form abc, ghi-dki, ioe-dsa. ie, all blankspaces, multiples spaces and symbols are converted to a hyphon(-). Now i need to compare. something like
SELECT MYCOLUMN FROM MYTABLE WHERE {Converted MYCOLUMN} = 'ghi-dki'
Can someone help me for figuring out which MySQL function can do it?
You can't do a regex replace in MySQL, but you can do a match.
SELECT mycolumn FROM tablename WHERE mycolumn REGEXP Replace('ghi-dki', '-', '^[\s:_-]*$');
Note: I didn't completely fill out the symbols character set, you'll have to add whatever you're using.

Retrving data from MySql with case sensitivity [duplicate]

This question already has answers here:
How can I make SQL case sensitive string comparison on MySQL?
(12 answers)
Closed 8 years ago.
I have a Login table in MySql database . . In table there is a column by cname and one of the value is 'Raghu'. My question is when i write the query as
Select *from Login where cname='raghu';
Then it is retrieving the record which contains 'Raghu' . I want it to retrieve according to case . How can I retrieve with case sensitively, values in the data of tables.
Use: 10.1.7.7. The BINARY Operator
The BINARY operator casts the string following it to a binary string. This is an easy way to force a comparison to be done byte by byte rather than character by character.
Select * from Login where binary cname='raghu';
SELECT * from Login WHERE STRCMP(cname,'Raghu')=0;
Can you try this, you can use LOWER FUNCTION in either column name LOWER('cname') or in value LOWER('raghu');
Select *from Login where LOWER(`cname`) = 'raghu';