Error reading an .sql file

3

Good evening Community.

I have a program in java, which I pass as an argument the authors.sql file (database in postgresql) which contains a single line:

INSERT INTO authors VALUES ('CEVA', 'K.', 'Cherteston')

At the moment of reading the instruction and executing the INSERT INTO statement, I get the following exception:

  

org.postgresql.util.PSQLException: ERROR: syntax error at or near   "INSERT" Position: 1

Does anyone see something wrong with the contents of the authors.sql file?

I have attached an image that shows the line where the exception occurs as well as the string containing the sql INSERT INTO statement, I hope it helps.

Thanks for your time

    
asked by David Leandro Heinze Müller 29.10.2016 в 01:52
source

2 answers

2

When I see your comments, I understand that your problem only happens when you read the sentence of your file authors.sql .

If this is the case, it is very likely that the problem is due to the fact that an invisible character is being introduced when you read the statement in the file.

For example, your file may be in the UTF-8 format, and it may include a BOM (byte order mark) at the beginning of the file.

In this case, your variable line would start with the character U+FEFF And since it is an invisible character, it is easy not to realize that it is there. If you want to confirm your presence, evaluate the following expression:

(int)line.charAt(0); // contiene 65279?

If you return 65279 , you know that's the problem.

To solve the problem, you can edit your file to remove the BOM at the beginning. Several editors allow you to do this.

Or, you can delete it in your code using String.replace :

stmt.execute(line.replace("\uFEFF", ""));
    
answered by 30.10.2016 / 03:43
source
0

The correct syntax is:

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);

in your case it would be something like this:

INSERT INTO authors (nombre, apellidoPaterno,apellidoMaterno) VALUES ('CEVA', 'K.', 'Cherteston')

Or if you have only 1 field:

INSERT INTO authors (nombre) VALUES ('CEVA');
INSERT INTO authors (nombre) VALUES ('K.');
INSERT INTO authors (nombre) VALUES ('Cherteston');

Recommended tutorial for learning PostgreSQL: link

    
answered by 29.10.2016 в 02:22