I have one table called test, which has 4 columns:
id INT
v_out INT
v_in INT
label CHARACTER
I'm trying to update the table with the following query:
String sql = "
update
test
set
v_out = temp.outV
, v_in = temp.inV
, label = temp.label
from (
values
(1,234,235,'[abc]') // these value are read from other places
,(2,234,5585,'[def]') //[abc] = object.toString();
) as temp (e_id, outV, inV, label)
where
id = temp.e_id;
When I execute it, I got the error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "["
before get this error, i already update over 3000 rows.
so whats the reason caused this? is it because "[" this character?
this is the original table:
create table edges(
id serial not null primary key,
vertex_out int,
vertex_in int,
label character varying(255),
constraint fk_vertex_out foreign key (vertex_out) references vertices(id) on delete cascade,
constraint fk_vertex_in foreign key (vertex_in) references vertices(id) on delete cascade
);
The most likely problem here is string interpolation in SQL query (it is a very bad practice).
Look at this example:
values
(1,234,235,'[abc]''),
(2,234,5585,'[def]')
The ' symbol in the name of the first object breaches the string boundaries causing ERROR: syntax error at or near "[": on the second line.
You can search for SQL Injection in the internet to get details about this problem.
Related
I have a PostgreSQL database hosted on Heroku which is throwing me this error that I can't wrap my head around.
CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT, age SMALLINT, right_handed BOOLEAN)
SELECT 1 id FROM people WHERE age IN (1) AND right_handed IN (1)
It gives me the error:
Error in query: ERROR: syntax error at or near "IN"
Don't know how to proceed, any help would be greatly appreciated.
AUTOINCREMENT is not a valid option for CREATE TABLE in Postgres
You can use SERIAL or BIGSERIAL:
ALTER TABLE myTable ADD COLUMN myColumn BIGSERIAL PRIMARY KEY;
Greetings with the following test table:
CREATE TEMP SEQUENCE pid start 10000;
SELECT nextval ('pid');
CREATE TEMP TABLE project_test (
id int DEFAULT nextval('pid') primary key,
project_id int UNIQUE NOT NULL
);
I am attempting to make a statement that either inserts a new value for project_id returning the id or returns the id if the project_id exists.
I have attempted the following:
INSERT INTO project_test(project_id) VALUES(50) RETURNING id
ON CONFLICT (project_id) DO UPDATE project_test SET project_id = 50 WHERE project_id = 50 RETURNING id;
I have read the documentation here:
https://www.postgresql.org/docs/13/sql-insert.html
I am on Postgres 13
I am getting a syntax error
ERROR: syntax error at or near "ON"
Thanks in advance for any tips
INSERT INTO project_test(project_id) VALUES(50)
ON CONFLICT (project_id) DO UPDATE SET project_id = 50 WHERE project_test.project_id = 50 RETURNING id;
The problem was the RETURNING id needs to go at the end, and then it thought column reference "project_id" was ambiguous, which is odd.
Thanks all
I am trying to define a new domain "rating_int" that I would be able to use in the table "Review". Anytime I try to execute this query, it returns this message:
ERROR: column "rating_int" does not exist
********** Error **********
ERROR: column "rating_int" does not exist
SQL state: 42703
I followed the exact same code format as the postgresql example and I do not know why my code is returning an error.
CREATE DOMAIN rating_int AS INTEGER
CHECK(rating_int > 0 AND rating_int <=10);
CREATE TABLE Review
(
paper_id INTEGER,
rv_email VARCHAR(55) NOT NULL,
reccomendation TEXT,
a_comments TEXT,
c_comments TEXT,
technical_merit rating_int,
readability rating_int,
orginality rating_int,
relevance rating_int,
PRIMARY KEY(paper_id, rv_email),
FOREIGN KEY(paper_id) REFERENCES Paper
ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY(rv_email) REFERENCES Reviewer
ON DELETE SET NULL ON UPDATE CASCADE
);
You use value, not the name of the domain. Like this:
CREATE DOMAIN rating_int AS INTEGER
CONSTRAINT chk_rating_int CHECK (value > 0 AND value <= 10);
You don't actually need to give the constraint a name -- your error was rating_int instead of value. So this works:
CREATE DOMAIN rating_int AS INTEGER
CHECK (value > 0 AND value <= 10);
However, I think it is a good idea to give constraints names.
Also, you might want to make the type a smallint or decimal(2), if you want to save on space.
I'm trying to execute this statement in onUpgrade method of database helper but I'm getting an error:
database.execSQL("CREATE TABLE VEHICLE_HOURS(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"MINUTES INTEGER, VEHICLE VARCHAR(255), ORDER INTEGER);");
ERROR
Caused by: android.database.sqlite.SQLiteException: near "ORDER":
syntax error (code 1): , while compiling: CREATE TABLE
VEHICLE_HOURS(ID INTEGER PRIMARY KEY AUTOINCREMENT, MINUTES INTEGER,
VEHICLE VARCHAR(255), ORDER INTEGER);
Thanks.
ORDER is a reserved word in SQL. You could of course suppress that error by taking the name into quotes, like:
database.execSQL("CREATE TABLE VEHICLE_HOURS(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"MINUTES INTEGER, VEHICLE VARCHAR(255), \"ORDER\" INTEGER);");
but better just pick another column name, then nobody (including your future self) will hate you while maintaining this code.
I am getting strange errors while trying to create a simple database using isql tools from the Firebird package.
The same code for creating a table works for other tables with other names.
I've tried with and without quotes surrounding fields and table names, no success.
It is Firebird 2.5 server version.
The code I'm trying to execute:
SET SQL DIALECT 3;
SET NAMES UTF8;
CREATE DATABASE 'localhost:C:\fuzzdb.fdb' user 'SYSDBA' password 'masterkey'
DEFAULT CHARACTER SET UTF8;
CREATE TABLE RULES (
RULE_ID INTEGER NOT NULL,
IF_FUZZY SMALLINT,
CONSTRAINT PK_RULE_ID
PRIMARY KEY (RULE_ID),
);
CREATE TABLE VARS (
VAR_ID INTEGER NOT NULL,
VRULE_ID INTEGER,
INPOUTP SMALLINT,
RANGE_STRT INTEGER,
RANGE_END INTEGER,
VAR_NAME VARCHAR(40),
FUZ_SET INTEGER,
CONSTRAINT PK_VAR_ID
PRIMARY KEY (VAR_ID)
);
CREATE TABLE FUZZSETS (
FS_ID INTEGER NOT NULL,
FS_NAME VARCHAR(40),
INPOUTP SMALLINT,
PAR1 FLOAT,
PAR2 FLOAT,
PAR3 FLOAT,
PAR4 FLOAT,
PAR5_HEDGE INTEGER,
FUZ_SET INTEGER,
CONSTRAINT PK_FS_ID
PRIMARY KEY (FS_ID)
);
CREATE TABLE FRULES (
FRULE_ID INTEGER NOT NULL,
RULE_ID INTEGER,
VAR_ID INTEGER,
FS_ID INTEGER,
INPOUTP SMALLINT,
CONSTRAINT PK_FRULE_ID
PRIMARY KEY (FRULE_ID)
);
CREATE TABLE LINK_RV (
LINK_RULES INTEGER,
LINK_VARS INTEGER,
CONSTRAINT FK_LINK_RV
PRIMARY KEY (LINK_RULES, LINK_VARS)
);
CREATE TABLE LINK_VARFS (
LINK_VRS INTEGER,
LINK_FS INTEGER,
CONSTRAINT FK_LINK_VARFS
PRIMARY KEY (LINK_VRS, LINK_FS)
);
CREATE TABLE LINK_RLVR (
LINK_RULE INTEGER NOT NULL,
LINK_VR INTEGER NOT NULL,
CONSTRAINT FK_LINK_RLVR
PRIMARY KEY (LINK_RULE, LINK_VR)
);
CREATE TABLE LINK_FRL_RL (
LINK_FRULE INTEGER NOT NULL,
LINK_RULE INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_RL
PRIMARY KEY (LINK_FRULE, LINK_RULE)
);
CREATE TABLE LINK_FRL_VAR (
LINK_FRULE INTEGER NOT NULL,
LINK_VAR INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_VAR
PRIMARY KEY (LINK_FRULE, LINK_VAR)
);
CREATE TABLE LINK_FRL_FS (
LINK_FSRULE INTEGER NOT NULL,
LINK_FS INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_FS
PRIMARY KEY (LINK_FRULE, LINK_FS)
);
ALTER TABLE LINK_FRL_FS
ADD CONSTRAINT FK_LINK_FSRULE
FOREIGN KEY(LINK_FSRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_FS
ADD CONSTRAINT FK_LINK_FS
FOREIGN KEY(LINK_FS)
REFERENCES FUZZSETS(FS_ID);
ALTER TABLE LINK_FRL_VAR
ADD CONSTRAINT FK_LINK_FRULE
FOREIGN KEY(LINK_FRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_VAR
ADD CONSTRAINT FK_LINK_VAR
FOREIGN KEY(LINK_VAR)
REFERENCES FUZZSETS(VAR_ID);
ALTER TABLE LINK_FRL_RL
ADD CONSTRAINT FK_LINK_FRULE
FOREIGN KEY(LINK_FRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_RL
ADD CONSTRAINT FK_LINK_RULE
FOREIGN KEY(LINK_RULE)
REFERENCES RULES(RULE_ID);
CREATE GENERATOR GEN_RULE_ID;
CREATE GENERATOR GEN_VAR_ID;
CREATE GENERATOR GEN_FS_ID;
CREATE GENERATOR GEN_FRULE_ID;
SET TERM ^ ;
CREATE TRIGGER BI_RULES FOR RULES
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.RULE_ID IS NULL) THEN
NEW.RULE_ID = GEN_ID(GEN_RULE_ID, 1);
END^
CREATE TRIGGER BI_VARS FOR VARS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.VAR_ID IS NULL) THEN
NEW.VAR_ID = GEN_ID(GEN_VAR_ID, 1);
END^
CREATE TRIGGER BI_FUZZSETS FOR FUZZSETS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.FS_ID IS NULL) THEN
NEW.FS_ID = GEN_ID(GEN_FS_ID, 1);
END^
CREATE TRIGGER BI_FRULES FOR FRULES
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.FRULE_ID IS NULL) THEN
NEW.FRULE_ID = GEN_ID(GEN_FRULE_ID, 1);
END^
SET TERM ; ^
COMMIT;
The output from the isql commmand:
Use CONNECT or CREATE DATABASE to specify a database
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Token unknown - line 6, column 3
-)
At line 10 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-Unknown columns in index FK_LINK_FRL_FS
After line 82 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-LINK_FRL_FS
-At line 1, column 13.
After line 89 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-LINK_FRL_FS
-At line 1, column 13.
After line 94 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-could not find UNIQUE or PRIMARY KEY constraint in table FUZZSETS with specifie
d columns
After line 104 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S11
unsuccessful metadata update
-Index FK_LINK_FRULE already exists
After line 109 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-Table RULES not found
After line 114 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-RULES
-At line 1, column 29
At line 130 in file c:\fdb.sql
I don't get why it's impossible to create the first table "RULES" although the commands are similar to other tables.
Even without all the triggers and foreign keys (alter table..) I am getting at leaast the last error.
it says "Unknown columns in index FK_LINK_FRL_FS" but no mention of other similar indexing tables.
I am just starting working with databases and it could be that I missed or mixed something,
but I tried to compile with too many changes and still getting errors.
I've found more or less similar code here
http://sergworks.wordpress.com/category/firebird/
and I was able to compile it without problems.
Could somebody point me in the right direction or show how it is done in another way?
You have an unnessesary comma in the end of the PK constraint:
CONSTRAINT PK_RULE_ID
PRIMARY KEY (RULE_ID),
So the parser expexts definition of field or constraint but it finds ")". Delete the comma and you should be OK.