SQL syntax inserting into table [duplicate] - mysql

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 months ago.
This line of code is no working, and I cannot for the life of me figure out what is wrong with it.
INSERT INTO teacher (email,password,admin) VALUES (deborah68#example.org,d%6AsQPq7y,1);
this wont run and says the error is near the end of the line
any help is appreciated, the schema is called at3

Why are you not enclosing the string/char data in single quotes ? Assuming they are of datatype varchar, you simply enclose them in single quotes and the command will work.
INSERT INTO teacher (email,password,admin) VALUES ('deborah68#example.org','d%6AsQPq7y',1)

Assuming your email and password are datatype of varchar, try putting '' between the values, or better you can use parameterized value if it's any backend code, try:
INSERT INTO teacher (email,password,admin) VALUES ('deborah68#example.org','d%6AsQPq7y',1);

Related

Code to insert Rows to a Table in phpMyAdmin [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 years ago.
I am trying to add rows to my table using the code below. I keep getting an error. I doesn’t like the colons and if I take them out (although I need them in there) it then doesn’t like the 356’s. If I remove them it then doesn’t like something else.
I am using phpMyAdmin and thought I was using MySQL but the error messages mention MariaDB.
BTW, the book I am working through shows ‘ ‘ around the values to be inserted but I have found I can only insert values surrounded by “ “.
What do I need to change to get this working in phpMyAdmin?
INSERT INTO colours (ID, ColourCode)
VALUES
(“356-30-127”, “356-30-127 : Red”),
(“356-30-128”, “356-30-128 : White”);
For data insertion you have to use " or ' in string type
check below query it works
create table colours (ID varchar(200),ColourCode varchar(200));
INSERT INTO colours (ID, ColourCode)
VALUES ('356-30-127', '356-30-127 : Red'),
('356-30-128', '356-30-128 : White');
http://sqlfiddle.com/#!9/2a14a8/1

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

MYSQL - Set/Update Syntax [duplicate]

This question already has answers here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I have a table like this:
Why is this command not working:
UPDATE 'stitch' SET 'claim-time'='20' WHERE 'group'='010000'
I get 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 ''stitch' SET claim-time='20' WHERE group='010000'' at line 1
Everything in the table is text.
group is a reserved keyword in mysql so use backticks to escape it
`group`
Also you are selecting the string as column name, correct format is
UPDATE `stitch` SET `claim-time`='20' WHERE `group`='010000'
Try removing the single quotes from stitch, claim-time and group. Either leave them out or use backquote `. The comma is used for strings, not table and field names.
Also, I don't know what data type claim-time and group are. If they are numeric (int, bigint, etc) and not string (varchar, text, etc) then you'll need to remove the single quotes from those too.
update stitch set claim-time=20 where group='0100000'; # assuming group is a string data type
Try this.
UPDATE TableName SET claim-time='20' WHERE group='010000';
That is considering if claim-time is a varchar datatype. If it's a number just remove the quotes.
Remember to avoid reserved names such as field names such as name, password, group, user and stuff just to be safe. Make it user1, group1 instead or something like these.

How to select a column name with a space in MySQL [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
I am working on a project where another developer created a table with column names like 'Business Name'. That is a space between two words. If I run a SELECT statement with 'Business Name' it says there is no column with name 'Business'.
How can I solve this problem?
Generally the first step is to not do that in the first place, but if this is already done, then you need to resort to properly quoting your column names:
SELECT `Business Name` FROM annoying_table
Usually these sorts of things are created by people who have used something like Microsoft Access and always use a GUI to do their thing.
If double quotes does not work , try including the string within square brackets.
For eg:
SELECT "Business Name","Other Name" FROM your_Table
can be changed as
SELECT [Business Name],[Other Name] FROM your_Table
You need to use backtick instead of single quotes:
Single quote - 'Business Name' - Wrong
Backtick - `Business Name` - Correct
To each his own but the right way to code this is to rename the columns inserting underscore so there are no gaps. This will ensure zero errors when coding. When printing the column names for public display you could search-and-replace to replace the underscore with a space.
I got here with an MS Access problem.
Backticks are good for MySQL, but they create weird errors, like "Invalid Query Name: Query1" in MS Access, for MS Access only, use square brackets:
It should look like this
SELECT Customer.[Customer ID], Customer.[Full Name] ...
I think double quotes works too:
SELECT "Business Name","Other Name" FROM your_Table
But I only tested on SQL Server NOT mySQL in case someone work with MS SQL Server.

phpmyadmin sql apostrophe not working [duplicate]

This question already has answers here:
character for single quote
(1 answer)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
hey guys was hoping you could help me out,
Not sure if I always had this problem or if its new, in phpmyadmin in the sql tab, the apostrophe it recognizes is different from what i type, for example,
when i type, it is
SELECT * FROM 'table'
this gives me an error, so instead I have to like copy/paste the inverted commas of some prebuilt query so that it looks like
SELECT * FROM `table`
see how the apostrophes are different? any way I can fix this?
also, i have seen many queries on the web, and i think even queries i call from php dont require table names to have apostrophes. But when write it in phpmyadmin, I can do queries without table names having apostrophes?
thanks in advance.
In MYSQL, table is a reserved keyword. If you want to use reserved keywords in mysql in query, you have to enclose them in backtick(`).
As table is reserved keyword you query should be
SELECT * FROM `table`
Regarding single quote ('), in mysql, it represents string value.
SELECT *, 'table' FROM `table`;
Demo
You should only need to quote table names when they conflict with a reserved word.
Also:
` = Grave accent, or (because someone needed to invent a word) backtick
' = Apostrophe, or straight single quote
You dont need apostrophe on table name.
You should use ` in cases that your table/field name is a reserve word eg:
SELECT `distinct`, myfields FROM mytable
note that distinct is an sql command so you need to put the `.
SELECT * FROM `table`
table here should be inside `.
There are two different characters, the backtick and the single quote. Table and column names can be surrounded by the backtick, strings can be surrounded by quotes. There is nothign to fix :D