I am trying to use mysql library to request my database.
I need to use the syntax with query placeholders, so I tried a simple request:
connection.query('DROP DATABASE IF EXISTS ?;', ['mydb']);
But this leads to :
'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 \'\'mydb\'\' at line 1'
The query is indeed:
'DROP DATABASE IF EXISTS \'mydb\';'
So how is this supposed to work actually ?
The MySQL notation for this is:
DROP DATABASE IF EXISTS `mydb`
Since you're escaping it as a string, that's not a database reference it can drop.
Some drivers support alternate placeholders for this very reason:
DROP DATABASE IF EXISTS ??
Normally you can't use placeholder values for things like databases, columns or tables as these are treated differently. This is a limitation of the driver.
You just need to be careful on user-supplied values and names with irregular characters in them. These need to be escaped according to MySQL schema identifier rules.
Related
I am having problems getting the SQL "use" command to work in MySQL 5.7.
I have a database 'mydb' with a 'character' table. (At the moment, the table is empty, but I don't think that should matter here).
Explicitly using "use" gives me an error:
use mydb;
select count(*) from character;
Error Code: 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 'character' at line 1 0.000 sec
However, the following line, which does not use "use", works okay:
select count(*) from mydb.character;
Does anyone know what I am doing wrong? Admittedly, I am quite new to SQL. The full output is shown below:
character is a reserved keyword in MySQL. Hence you get the error when you select from that table. Escape with `` to avoid this. Or use names other than reserved keywords for your tablenames.
Reserved Keywords in MySQL
I find myself in a peculiar situation while building a query builder in .net of needing to know this information so I can setup the appropriate structured classes.
What is the schema information for
SHOW FIELDS FROM `foo`;
I have tried
SHOW FIELDS FROM (SHOW FIELDS FROM `foo`) tbl1
... however that yields a sql error
[Err] 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 '(SHOW FIELDS FROM foo) tbl1' at line 1
I need to know the information about the columns returned. I could cheat and make them all strings, but I would much rather to use the ACTUAL structure (and if it truly is all strings, then damnnnnnnnn).
Thanks in advance.
You can use:
SHOW COLUMNS FROM tablename;
This will give you back structured information about fields.
More info on that:
http://dev.mysql.com/doc/refman/5.5/en/show-columns.html
For schema information you need to run:
DESCRIBE information_schema.columns
More about schema can be found here: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
I am using Sybase Power Designer to create a database from a physical data model (Sybase creates an SQL file) . When i import the SQL file with phpMyAdmin I have the following 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 'if exists(select 1 from sys.sysforeignkey where role='FK_ARTWORK_CREATES_ARTIST'' at line 7 .
Any ideas? Could this error appear due to errors on the physical model or there is another problem?
This is the code :
if exists(select 1 from sys.sysforeignkey where role='FK_ARTWORK_HAS_BUY') then
alter table artwork
delete foreign key FK_ARTWORK_HAS_BUY
end if;
The error you are getting is from MySQL. Regardless of the tool used to generate SQL, the database seems to be MySQL (or something is terribly wrong with your systems if they are confused and think they are MySQL).
The MySQL if statement (documented here) has to be inside a stored program. That means that this code only compiles inside a stored procedure, user defined function, or trigger. It doesn't "just work" on its own.
In addition, MySQL doesn't have sys tables. It uses information_schema tables. My strongest suggestion is to use tools appropriate for your actual database. If you are using a tool to generate Sybase, then use Sybase as the destination database. If you are using MySQL, then use a tool to generate MySQL code. Or, better yet, learn how to write the commands yourself.
Finally, if you intend to use Sybase, then connect to the correct database and your problem should be fixed.
As I can't post here, and sqlfiddle.com temporary unavailable, I decided to post code with correct syntax for Sybase on the PasteBin
I am trying to create a GUI with a database attached, my SQL table is :
CREATE TABLE IF NOT EXISTS imagerecord (
PId int,
PName varchar(50) not null,
Photo varbinary(max)
)
Clearly I am trying to include an image in the database (atleast, that's what I think I'm doing), however my system is not identifying the 'varbinary' datatype, showing this error :
Error code 1064, SQL state 42000: 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 'max) )' at line 4
Please tell me what to do, is there some extra software do I have to install?
You're mixing Microsoft SQL Server syntax for VARBINARY(MAX) with MySQL syntax for CREATE TABLE IF NOT EXISTS. Neither RDBMS implementation supports both of these features.
Microsoft SQL Server supports MAX as a length instead of a specific number, but standard SQL and most other brands don't support this option.
MySQL supports IF NOT EXISTS as an optional modifier, but this is also not standard SQL, and Microsoft doesn't support this extension.
You need to figure out which brand you're using. The fact that MAX gave an error but IF NOT EXISTS did not suggests you are using MySQL. Here are links to the respective documentation pages:
Microsoft SQL Server 2012:
CREATE TABLE
BINARY and VARBINARY
MySQL 5.6:
CREATE TABLE
BINARY and VARBINARY
The maximum length of a VARBINARY in MySQL is 65535. If you need a column that can accept a longer string, you can use MEDIUMBLOB which allows up to 16MB, or LONGBLOB which allows up to 4GB.
When I use the MySQL client to create database, if I type below command:
create database nice-day;
Then it tells me that:
ERROR 1064 (42000): 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 '-day' at line 1
But when I use phpAdmin tool, I can create the database named nice-day. What is the problem?
The - character is not considered to be part of an identifier in SQL, so the database name must be quoted:
CREATE DATABASE `nice-day`
In general, though, it is advisable to use underscores (_) instead of dashes in database names to avoid this issue.
Please use backticks "`" (the key before the 1 on a standard US 101 keyboard).
as per http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
The identifier quote character is the backtick (“`”):