What does the '#>' symbol mean in postgresql? - java

I'm working on a project that uses a PostgreSQL database.
There's a few locations in the project which build queries like this:
query += " AND " + String + " #> " + String;
I'm not familiar with the #> symbol, and neither is anyone currently working on the project. Also googling it doesn't work, presumably as it's an odd symbol.
Also, I'm not sure if this symbol is a postgresql thing or a sql thing.
P.S. The application in written in java.

It's "contains".
If the column is array and use #> the column should contain the value.
More here: https://www.postgresql.org/docs/current/static/functions-array.html

Related

IntelliJ IDEA not recognizing SQL dialect

We use IntelliJ IDEA actively and we have our wrappers for work with DB (PostgreSQL). The thing is that when we use placeholders, SQL is stopped being highlighted.
"select * from " + schema + ".users where id = " + id + ";";
This code is not recognised as SQL, so highlighting doesn't work.
I agree with Jesper, you should try using preparedStatements, not only would your Statements get more secure, also the problem which you are currently having should be solved by it.

Why does this SQL-Statement lead to an error?

I am getting quite angry with this, so I seek help from the crowd ;)
What I want to do: We have a Unity learning game which shall implement a login window. The entered credentials are then hashed (the pw is) and sent to the server, who then should check this against a database.
I have the following table:
xy.users_confirms with the following colums:
id username email password hashcode created
Why does my code
String sql = "SELECT " + "xy.users_confirms.password as pwhash, "
+"FROM xy.users_confirms " +"WHERE xy.users_confirms.username = " +"\"userNameToGetHashFor\"";
lead me to the SQLException "Parameter index out of range (1 > number of parameters, which is 0)"
?
Thanks, any input is much appreciated!
Try this:
String parameter = "'"+ strNameToGetHashFor + "'";
String sql = "SELECT " + "xy.users_confirms.password as pwhash, "
+"FROM xy.users_confirms "
+"WHERE xy.users_confirms.username ="+ parameter;
You are using varchar value as a parameter so it's need to be quot like this.'username'. or you can use Stored Procedure.
Personally, I would try getting a working query using the custom query box directly in phpmyadmin. Once you have a working query you can re-write it in java.
And I would try writing the syntax like this into the phpmyadmin query box:
SELECT password as pwhash
FROM xy.users_confirms
WHERE username ='userNameToGetHashFor'
Using the above syntax I don't see anyway your error could persist.
Phpmyadmin screen cap showing custom query box: http://screencast.com/t/9h8anH0Aj
(the 2 empty text boxes in screen cap are just me hiding my database info)
The comma after pwhash is one potential cause:
+ "xy.users_confirms.password as pwhash*!*,*!* "
Depending on the DBMS, you may also need to use single quotes instead of double quotes like this:
+ "'userNameToGetHashFor'";
Also this code is potentially vulnerable to a SQL Injection attack so you may want to make the userNameToGetHashFor a parameter rather than concatenating the string into the SQL statement.

Derby SQL Syntax error: Encountered "." when copying from one database to another

I'm using Derby DB and trying to copy data from a table in one database to a table with the same table definition in another database.
What is the correct syntax to specify the database.scheme.table in Derby SQL?
I've tried using the following select statement
statement = "INSERT INTO CH003..MHALL..CH_COMPANY SELECT * FROM CH001..MHALL..CH_COMPANY WHERE COMPANY_NUMBER = '" + companyNumber + "'";
but get the error Caused by: java.sql.SQLException: Syntax error: Encountered "." at line 1, column 22.
Changing the code to
statement = "INSERT INTO CH003.MHALL.CH_COMPANY SELECT * FROM CH001.MHALL.CH_COMPANY WHERE COMPANY_NUMBER = '" + companyNumber + "'";
I get the error SQLSyntaxErrorException: Syntax error: Encountered "." at line 1, column 27.
Is it possible to do this in Derby? Do you know the correct syntax?
Thank you for your help.
A single Derby JDBC connection is connected to exactly one Derby database, so this is not possible in the way that you have attempted it.
However, there are other methods that you could use.
For example, you could use SYSCS_UTIL.SYSCS_EXPORT_TABLE to export the data from the table into a CSV file, then you could open a connection to the target database and use the SYSCS_UTIL.SYSCS_IMPORT_DATA to import that data. See https://db.apache.org/derby/docs/10.11/ref/rrefexportproc.html and http://db.apache.org/derby/docs/10.11/ref/rrefimportdataproc.html.
Or, you could read that data into, say, a data structure in your Java program, like an ArrayList, and then open a connection to your target database and write it out.
Or, you could use a tool like Apache DdlUtils to copy the data from one table to another: http://db.apache.org/derby/integrate/db_ddlutils.html
I'm sure there are other possibilities, but since you didn't give a lot of background about the underlying goal you're trying to accomplish, I won't try to speculate about why one approach might be better or worse than another.

HSQLDB Statement and Java NaN doubles

I'm currently using HSQLDB to save java data. Within theses datas, there are some Double, and some of them can be of values of NaN (described as 0.0/0.0 in the javadoc). HSQLDB know how to handle these values in setDouble and setFloat of PreparedStatement. The thing is, I have to use a Statement object, not a precompiled stored procedure, and I just can't find a way to make it work.
If you had the tinyest hint, it would be most welcome :)
Thanks.
EDIT : Here's the bunch of code I'm using :
stmt.executeUpdate("insert into Mesh(Id, name, dimension, meshtype, totalVolume, NumberOfCoarseCell) values (identity(), "
+ "'" + name_ + "',"
+ dimension_ + "," // this value can be NaN
+ "'" + type_.toString() + "',"
+ totalVolume_ + "," // this value can be NaN
+ numberOfCoarseCells_ + ")");
You mean you need a way to write a NaN within a SQL statement? The following works for the H2 database:
select sqrt(-1) from dual
However, it doesn't work for Apache Derby and PostgreSQL (I didn't test other databases). For HSQLDB, it no longer works in version 2.1 and newer, unless you set SET DATABASE SQL DOUBLE NAN FALSE.
With HSQLDB 1.8.x you can use (0.0e1/0.0e1) as an expression that returns NaN.
For example:
create table t (d double)
insert into t values (0.0e1/0.0e1)
For HSQLDB 2.1 and above, an property must be specified with an SQL statement:
SET DATABASE SQL DOUBLE NAN FALSE
Or as a connection property:
hsqldb.double_nan=false

Different results from .mdb vs .odb, why?

I use the following query to retrieve data from a .mdb file through JDBC, however when I try it on an .odb file it goes does not throw any exceptions but there are no results at all. I am wondering is .odb case sensitive where .mdb is not or is there something else I am missing?
"SELECT DISTINCT column-one + ':' + column-two As ResultColumn FROM datatable;"
How can I go about creating one statement that will work on both these file types?
They would be differnt because they are two differnt products written by two differnt companies and the programmers made different choices as to how to handle things.
Have you tried using a column alias you specify, perhaps something more descriptive than Expr1000?
SELECT DISTINCT column-one + ":" + column-two As Expr1000 FROM datatable
That's how I would write it in SQL Server, check your database to see if this would work.
Some suggestions:
1) Try using single quotes in place of double quotes e.g.
SELECT DISTINCT column-one + ':' + column-two As ResultColumn FROM datatable;
2) Perhaps the .odb source's SQL syntax handles concatenation differently e.g. with the .mdb the '+' NULLs will propagate meaning that if at least one the the column's values is NULL then the result will be NULL; the '&' concatenation symbol will ignore NULL values e.g. this in .mdb land
SELECT DISTINCT column-one & ':' & column-two As ResultColumn FROM datatable;
is equivalent to this in Standard ANSI/ISO SQL-92 (which isn't supported in .mdb land)
SELECT DISTINCT COALESCE(column-one, '') + ':' + COALESCE(column-two, '') As ResultColumn FROM datatable;
3) If the two sources do not support the same syntax, can you use the .mdb's linked table functionality to link the tables(s) from the .odb source in the .mdb and only use the SQL code in the .mdb?

Categories