#I am translating input from a user into a MySQL command but it says i have the syntax wrong and Idk how I can fix it. Essentially I made a program in python (not a problem with the python code) that has the ability to create MySQL tables and translates user input into a MySQL command to create the table, but I think i have worded the command wrong and need someone to help explain what I did wrong and how I can fix it or another way to do it (no new python packages just different MySQL command).
#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 ''c1', 'c2') VALUES [('r1', 'r2')]' at line 1
#here is what i was collecting in my attempt to fix the bug
'''
ROW SYNTAX: [('r1', 'r2')]
COLUMN SYNTAX: ('c1', 'c2')
MySQL COMMAND: CREATE TABLE test ('c1', 'c2') VALUES [('r1', 'r2')]
'''
#just the values none of the ROW SYNTAX or COLUMN SYNTAX is actually put into the mysql command
I was expecting it to create the table with:
2 columns: c1 and c2
1 row with 2 values: r1 and r2
Yet as I have explained above... it doesn't. I don't work with MySQL just python really so I'm probably missing something really easy or big that I just don't understand.
i've seen some example syntax similar to this but again I don't understand it.
UPDATE
'''
ROW SYNTAX: [('r1', 'r3'), ('r2', 'r4')]
COLUMN SYNTAX TCOLN: (c1, c2)
COLUMN SYNTAX COLNAMES: (c1 VCHAR(255), c2 VCHAR(255))
CREATE TABLE COMMAND: CREATE TABLE test (c1 VCHAR(255), c2 VCHAR(255));
INSERT TABLE COMMAND: INSERT INTO test (c1, c2) VALUES [('r1', 'r3'), ('r2', 'r4')];
'''
'''
Traceback (most recent call last):
line 472, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: 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 'VCHAR(255), c2 VCHAR(255))' at line 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
line 558, in ctable
cursor.execute(sqlCol) #slqCol is CREATE TABLE test (c1 VCHAR(255), c2 VCHAR(255));
line 266, in execute
raw_as_string=self._raw_as_string)
line 475, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 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 'VCHAR(255), c2 VCHAR(255))' at line 1
'''
Thanks to your suggestions i am using VARCHAR now but i still seem to be running into close to the same problem
Thanks to #Martin for explaining what varchar is ect, it also turns out VCHAR is not the same as VARCHAR (thanks #robsiemb). Now the table is creating BUT not putting the data values in? (this might be a python problem, hope not)
'''
INSERT TABLE COMMAND: INSERT INTO test (c1, c2) VALUES ('r1', 'r2'),('rr1', 'rr2');
'''
# LATEST UPDATE
New Error when inserting values
```python
#CREATE TABLE COMMAND: CREATE TABLE test (c1 VARCHAR(255), c2 VARCHAR(255));
#INSERT TABLE COMMAND: INSERT INTO test (c1, c2) VALUES (r1,r3),(r2,r4);
'''
Traceback (most recent call last):
line 472, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: Unknown column 'r1' in 'field list'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
line 563, in ctable
cursor.execute(sqlVal)
line 266, in execute
raw_as_string=self._raw_as_string)
line 475, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'r1' in 'field list'
'''
Python problem, thanks for answering my question!
The problem with what you are doing is in this statement:
CREATE TABLE test ('c1', 'c2') VALUES [('r1', 'r2')]
This is not a valid CREATE TABLE statement.
It looks like you're trying to create the table, and then insert some values into it. Is that the case?
If so, this should be broken into two statements. First create the table:
CREATE TABLE test (c1 DATATYPE, c2 DATATYPE);
Replace DATATYPE with the type of the column you are creating (i.e. INT, VARCHAR(10), etc.).
Second, insert your values into the table:
INSERT INTO test (c1, c2) VALUES (r1, r2);
You should review the syntax and example for how to create a MySQL table.
There are 2 issues with your CREATE TABLE statement.
You need to specify the types of the columns, something like:
CREATE TABLE test (c1 INTEGER, c2 INTEGER);
You can't supply the values at the same time as create, but you can follow up with an insert statement:
INSERT INTO test (c1, c2) VALUES (1, 3),(2, 4);
The above gives a table with two rows, like this:
mysql> SELECT * FROM test;
+------+------+
| c1 | c2 |
+------+------+
| 1 | 3 |
| 2 | 4 |
+------+------+
Related
An error(PL/SQL: ORA-25137: Data value out of range ) was reported when I executed the following code in Oracle 21c.
create table t(id int ,j json);
insert into t values(1,'{"key":"valus"}');
DECLARE
b varchar2(100);
BEGIN
select cast(j as varchar2(100)) into b from t where id=1;
DBMS_OUTPUT.PUT_LINE(b);
END;
The error message is
ERROR at line 4:
ORA-06550: line 4, column 15:
PL/SQL: ORA-25137: Data value out of range
ORA-06550: line 4, column 3:
PL/SQL: SQL Statement ignored
The oracle version I used is
Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0
I can't understand why this error was reported.
I think the cast target size I set is enough, but "ORA-25137: Data value out of range" was reported.
Can anyone explain why?
The data is stored in the table in binary format so you should use JSON_SERIALIZE function to convert JSON from any supported type into text. It is available since 19c:
Select t.id, json_serialize(t.j) From tbl t;
id j
---- -------------------
1 {"key":"valus"}
or, for previous versions use some of json functions like:
Select t.id, json_value(t.j, '$.key') as json_key From tbl t;
id json_key
---- -------------------
1 valus
There are more functions and options to get your data using sql with json datatype (json_query(), json_table(), ...)
You can find more about it -- https://oracle-base.com/articles/21c/json-data-type-21c
When I run a stored-procedure ranking_long() by mentioning call ranking_long() in a stored-procedure, the result is as follows.
product_id plg_count rank
11 6962271 1
10 2705517 2
379 1955067 3
378 196865 4
...........
Now I need to upsert(update when there is the same product_id or insert when there is not the same product_id) the result above into the table called dtb_ranking that has the same structure with the above result (product_id, plg_count, rank) by using a stored-procedure. So I tried the stored-procedure below,
INSERT INTO dtb_ranking (`product_id`,`plg_count`,`rank`) VALUES (CALL `ranking_long`()) ON DUPLICATE KEY UPDATE plg_count = VALUES(plg_count), rank = VALUES(rank);
And I had an error message like below.
One or more errors have occurred while processing your request:
Failed to execute a query.:
CREATE DEFINER=xxxxxxx#% PROCEDURE call_ranking_long()
NOT DETERMINISTIC
NO SQL
SQL SECURITY DEFINER
INSERT INTO dtb_ranking
(product_id,plg_count,rank)
VALUES (CALL ranking_long())
ON DUPLICATE KEY UPDATE plg_count = VALUES(plg_count), rank = VALUES(rank);
MySQL's message: #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 'CALL ranking_long()) ON DUPLICATE KEY UPDATE
plg_count = VALUES(plg_count), ra' at line 1
I would appreciate if anyone could tell me what I should do with the script above(INSERT INTO dtb_ranking...).
I am trying to write an insert query, where I want to add checksum of another table.
My Engine is InnoDb not MyIsam .
So checksum in INFORMATION_SCHEMA.TABLES are all null.
But when I run query like
CHECKSUM TABLE TABLE_NAME
it gives me checksum of that table. I wanted to know How I can store that value.
Or Is there any way I can find changes to table.
Triggers is a way. But when there are changes they are more than one so, I drop and run my scripts and they create all tables. (all tables are created from csv excel sheet and they are changing. )
So far I have tried this :
INSERT INTO `XYZ` (`TABLE_NAME`, `ABC`, `CHECKSUM`)
VALUES
('X',
1,
(SELECT Checksum FROM CHECKSUM TABLE 'X')
);
But It is giving me 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 'TABLE 'X') )' at line 1
Checksum of a table can be calculated using query in following link:
https://dev.mysql.com/doc/refman/5.7/en/checksum-table.html
Can anybody please help me? Thanks .
I have a table "EvMetadata" with column "Metadata" that has a check constraint of "IS JSON". Note that the table and its columns are created with DOUBLE QUOTES by design.
Following SQL works where I'm not specifying any JSON work to be done by Oracle.
select
m."Metadata"
from "EvMetadata" m
As you can see below, the Metadata column simply displays its content which happens to be JSON data.
However, I get error if I were to issue a json query as follows.
select
m."Metadata"."FileName"
from "EvMetadata" m
I just added "FileName" using dot notation. As you can see above, "FileName" is a valid json field. So why the error?
Error is
ORA-00904: "M"."Metadata"."FileName": invalid identifier 00904. 00000 - "%s: invalid identifier" *Cause: *Action: Error at Line: 2 Column: 3
Could this be a bug with Oracle's JSON query support using the dot notation under a specific scenario where database objects are declared with double quotes? The reason I suspect that may be true is that the following equivalent query, not using the dot notation, works.
select
JSON_VALUE(m."Metadata", '$.FileName')
from "EvMetadata" m
You need to have an "IS JSON" check constraint on the column for dot notation to work:
Here's an excerpt from the documentation:
Each json_key must be a valid SQL identifier, and the column must have an is json check constraint, which ensures that it contains well-formed JSON data. If either of these rules is not respected then an error is raised at query compile time. (The check constraint must be present to avoid raising an error; however, it need not be active. If you deactivate the constraint then this error is not raised.)
Here's a test example I did to verify this is how it's working:
--create a table to put stuff in
create table foo (
json varchar2(4000)
);
--------------------------------
Table FOO created.
--insert test value
insert into foo(json) values('{"attr1":5,"attr2":"yes"}');
commit;
--------------------------------
1 row inserted.
Commit complete.
--try some selects
--no table alias, no constraint, borked
select json.attr1 from foo;
--------------------------------
Error starting at line : 12 in command -
select json.attr1 from foo
Error at Command Line : 12 Column : 8
Error report -
SQL Error: ORA-00904: "JSON"."ATTR1": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
--with table alias, no constraint, borked
select a.json.attr1 from foo a;
--------------------------------
Error starting at line : 15 in command -
select a.json.attr1 from foo a
Error at Command Line : 15 Column : 8
Error report -
SQL Error: ORA-00904: "A"."JSON"."ATTR1": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
--add our constraint
alter table foo add constraint json_isjson check (json is json);
--------------------------------
Table FOO altered.
--no table alias, with constraint, borked
select json.attr1 from foo;
--------------------------------
Error starting at line : 21 in command -
select json.attr1 from foo
Error at Command Line : 21 Column : 8
Error report -
SQL Error: ORA-00904: "JSON"."ATTR1": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
--table alias and constraint, works!
select a.json.attr1 from foo a;
--------------------------------
ATTR1
--------------------------------------------------------------------------------
5
In case anyone else gets this issue, its documented in Oracle Support under note 2192052.1
Basically, it's a bug whereby Dot Notation doesn't work on a column which is created with a NOT NULL constraint, i.e.
If you do:
CREATE TABLE foo.bar (id NUMBER NOT NULL, json_doc CLOB NOT NULL CHECK (json_doc IS JSON));
you'll get the error when you run:
SELECT a.json_doc.elementName FROM foo.bar a;
but if you do:
CREATE TABLE foo.bar (id NUMBER NOT NULL, json_doc CLOB CHECK (json_doc IS JSON));
ALTER TABLE bar MODIFY (json_doc NOT NULL);
the Dot notation will work.
You do not need quotes, this shall work:
select m.Metadata.FileName from EvMetadata m
Please refer to the example of official documentation:
SELECT po.po_document.PONumber FROM j_purchaseorder po;
SELECT json_value(po_document, '$.PONumber') FROM j_purchaseorder;
I'm trying to make a backup of my table in MySql but I get this 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 'table `zbackup_oc_t_city` from `oc_t_city` LIMIT 0, 30' at line 1
This is the code that I'm using to backup
SELECT * INTO TABLE `zbackup_oc_t_city` FROM `oc_t_city`
Here is my oc_t_city table:
Here is zbackup_oc_t_city
I have tried it on numerous tables and it keeps throwing me the same error... any ideas?
Thanks
If you want to create your backup table and do the backup in just one statement use
CREATE TABLE `zbackup_oc_t_city` SELECT * FROM `oc_t_city`;
CREATE TABLE ... SELECT Syntax
You can create one table from another by adding a SELECT statement at
the end of the CREATE TABLE statement:
CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;
With MySQL you can't use SELECT ... INTO to select into a new table:
SELECT ... INTO Syntax
The SELECT ... INTO form of SELECT enables a
query result to be stored in variables or written to a file:
SELECT ... INTO var_list selects column values and stores them into
variables.
SELECT ... INTO OUTFILE writes the selected rows to a file. Column and
line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILE writes a single row to a file without any
formatting.
I do remember having similar troubles while working with SQL myself. One cause of error I found was the use of citation marks... try removing the citation marks like this:
SELECT * INTO zbackup_oc_t_city FROM oc_t_city;
I'm not sure this fixes your problem (but I can't see anything else wrong with your query). I hope it does though. :)