I have two tables in my database, info and comment and their structures are as follow:
info (id(int(10)), name(varchar(80)), ...19 other columns.., phone(int(16)));
comment (id(int(10)), name(varchar(80)), comment(varchar(80)), phone(int(16)));
When I execute these SQL queries:
INSERT INTO info (name, ...19 other columns.., phone) VALUES ('asa', ....., 123456)
It works perfectly fine.
But when it comes to INSERT INTO comment (name, address, phone) VALUES ('asa', 'asa', 123456), it reports SQL syntax error unless I surrounded phone the backtick like this `phone`
Honestly, I can get all the things done by adding the backtick, but I would like to why it works. Thanks in advance!
I would imagine there is a comma or something in the address that you are trying to insert and it is therefore trying to insert everything beyond that comma into the phone field.
Table structure name,comment, phone
Your sql command name,address, phone (address and comment are different)
Check that all columns and "insert into" names match between them at all.
The error message is important. Please post it if possible. It is also important to show the whole table column names and the whole "insert into" statement.
Related
I have created a database in MySQL and am trying to insert values into a table called Artist but keep receiving the SQL error in the title. This is the query I am trying to execute
insert Artist (ArtistId, name, genre, social media, G_id)
Values ('911', 'Alessandro Torlonia', 'Ancient Greek/Roman', '', '734')
I have also tried executing INSERT INTO before my table name and have run out of ideas to make it work. Do I need to update MySQL workbench or any other suggestions?
The standard insert syntax goes like:
insert into Artist (ArtistId, name, genre, social_media, G_id)
values (911, 'Alessandro Torlonia', 'Ancient Greek/Roman', NULL, 734)
Notes:
presumably, ArtistId and G_id are of a numeric datatype (such as INT), not strings (VARCHAR or the-like); if so, I would recommend not surrounding the values with single quotes. Using strings won't raise errors (the database handles the conversion for you under the hood) but it is good practice to use the proper datatype
social media probably needs an underscore in between; or, if you do have a space in the column name, you need to surround the column name with backticks. Note that I assigned a NULL value to instead of the empty string; this is usually the preferred way to represent the absence of data (and it is valid for all datatypes, unlike the empty string, that only string-like datatypes support)
Basically I have a large MySQL database table with a lot of city names, 90% of them are valid entries, but some of them are written in a ... not valid way.
For example the valid way is juste "CITYNAME" but some of them are like "(CITY NAME)(COUNTRY)" or just "(CITY NAME)" so I just wanna SELECT all the entries that are not written the valid way.
I don't know if that's specific enough don't hesitate to ask me for some precise elements.
And please help I have no idea how to build my SQL query.
CREATE TABLE cities(
name VARCHAR(50) NOT NULL PRIMARY KEY
);
INSERT INTO cities(name) VALUES ('ORLANDO');
INSERT INTO cities(name) VALUES ('(CHARLOTTE)');
INSERT INTO cities(name) VALUES ('(PHOENIX)(USA)');
INSERT INTO cities(name) VALUES ('AUSTIN(USA)');
INSERT INTO cities(name) VALUES ('TENNESSEE NASHVILLE');
So here are some examples of the different kinds of entries that I have to deal with.
I don't know how to describe the desired output, that'd just be a list of odd syntaxes, with or without the brackets.
The whole point is to delete those odd entries, but I have to SELECT them before doing so. And I also won't be the one deleting them, just gotta SELECT.
Possible solutions with output:
select * from cities where instr(name,')') or instr(name,'(');
(CHARLOTTE)
(PHOENIX)(USA)
This looks for anything which contains "(" or ")" anywhere.
Why wouldn't you try something like
Select * from cities where name like '(%';
If the problem is the parenthesis.. then everything that starts with a parenthesis will be selected
EDIT:
Select * from cities where name like '%(%' or name like '%)%';
It gives every line that contains a ( OR a )
EDIT2: some explanation
The character '%' replaces any string. So if you put like '%randomthing%', it will look for everything that look like anystring_randomthing_anystring. I hope i made this clear
You are using MySql, so you can do:
select *
from cities
where name not regexp '^[A-Z-]+$';
The not regexp '^[A-Z-]+$' means all the city names that don't respect the format "AAAA" or "AAAA-AAAAA" (where A represent an uppercase letter) is considered as invalid.
Now I would like to insert symbols to my MySQL table
I've spent a few hours over the net searching for an answer but I might be stupid or not because I could not find what I wanted
Now I insert my data to the database using command line
How can I insert symbols like "#;#,%,$,&,',..." into the table
Table collation is set to UTF8_unicode_CI
command I use
INSERT INTO table (name) VALUES ( "Assassin's Creed® 3" )
Now it shows up as
AssassinтАЩs Creed┬о 3
your query is right only the way you show it u must use htmlentities() or htmlspecialchars()
This first function is identical to
htmlspecialchars() in all ways, except
with htmlentities(), all characters
which have HTML character entity
equivalents are translated into these
entities.
ex: echo htmlspecialchars($row['name']) ;
be sure to pass values to mysql by mysql_real_escape_string() to avoid any SQL Injection problems.
I've been given an MS-Access application to maintain and being more acquainted with Oracle as dbms I bump into issues now and then..
Today it looks like MS-Access has problems when a hyphen is used for a column name...
The following insert statement was coupled to the NotInList event to add an extra entry to a listbox.
INSERT INTO tblProductInfo ( ProductInfo-Product )
"SELECT """ & NewData & """ AS ProductInfo-Product;"
But it's not working (anymore? first time the issue is reported, not sure if the original developer tested it out).
I've tested it out with a single-record append query and it looks like the hyphen is the culprit and I just cannot find a way to escape that..
INSERT INTO tblProductInfo ( ProductInfo-Product ) VALUES ("myData")
The error given is "Syntax error in INSERT INTO statement"
There does not seem any other way to specify the MS-Access fieldname, is it? (square brackets are only used for SELECT statements,
So... I'm calling for the wisdom of the Stackoverflow gods and am hoping someone knows how to solve this...
Thanks in advance !!
You need square brackets on that:
"INSERT INTO tblProductInfo ([ProductInfo-Product]) Values (""" & NewData & """)"
Or better yet, avoid odd characters and spaces in field and table names.
Square brackets are used in any sql statement where the field or table name is problematical:
It is a reserved word
It contains a space
It includes a special character
You can even use them with DDL:
Create Table tblProductInfo ( [ProductInfo-Product] Text(50))
I cannot figure this one out. All the variables are ok. Printed out the sql statement before executing in php... This is the statement exactly as it is sent to be ran by php
INSERT INTO 'images' ('filename', 'creator', 'date', 'notes') VALUES ('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs')
Thank You!
You're enclosing the table/field names in single-quotes. You need to do it with back-ticks (or nothing, depending on the name).
INSERT INTO `images` (`filename`, `creator`, `date`, `notes`) VALUES ('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs')
Just in case you're interested, a list of reserved names (which must be quoted if used as a table or column name) can be found here: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html.
Also, to find out what characters may be included in an unquoted name and which can appear only as part of a quoted name, see here: http://dev.mysql.com/doc/refman/5.5/en/identifiers.html.
N.B. the relevant MySQL version.
Your table and field names should either be in backquotes (`) or unquoted.
INSERT INTO `images` (`filename`, `creator`, `date`, `notes`) VALUES
('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs');
Try editing single quotes to backquotes for table name and field names