last day (15 days later) » 

10:32 PM
2
A: PostgeSQL Insert multiple row if not exist

Volune(SELECT (?,NULL) FROM PRODUCTS_CAT) always exists (return a row) as soon as there is a row in PRODUCTS_CAT. To test if this id is already inserted, you should do: SELECT 1 FROM PRODUCTS_CAT WHERE PRODUCT = ? Full insert statement: INSERT INTO PRODUCTS_CAT(PRODUCT,CATORDER) SELECT ?,NULL WHE...

 
Yes it tests if that Id already exists but if I try to insert once again the same row plus others it returns an error and doesn't insert the other rows.
 
@Leyonce what error ?
 
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "products_cat_pkey" Detail: Key (product)=(138) already exists.
 
@Leyonce It may be that only one placeholder ? matches the id parameter. Try with something like .exec(prod.getID(), prod.getID()). What library do you use?
 
I'm using java.sql*.I tried the .exec(prod.getID(), prod.getID()).The app loads forever. I did not understand the one placeholder thing could I have some references?
 
10:32 PM
There is no StaticStatment nor SerializerWriteString in java.sql, so you're using something else. I can't show references of a library I don't know. The point was about the two question marks "?" in your INSERT request. It seems obvious that prod.getID() will "replace" the first question mark. But I suppose that null will replace the second question mark, leading to your problem. How did you actually wrote the .exec(prod.getID(), prod.getID()) thing? (And do you happen to use something called "mbpos" ?)
 
Nope I'm not using anything called "mbpos". Here are the libraries I'm using. It's actually StaticSentence, I made a silly mistake. com.openbravo.data.loader.SerializerWriteString com.openbravo.data.loader.PreparedSentence com.openbravo.data.loader.StaticSentence
I wrote exactly the .exec(prod.getID(), prod.getID()) thing
 
@Leyonce You have to adapt a little bit your code for two arguments. (SerializerWriteString.INSTANCE doesn't seem to allow two arguments.) I updated the answer with some code that should work, but I can't test it myself.
 
You didn't even test it but still it works. How did you do?
I mean what lead you to the answer. I'll appreciate if you orientate me towards anything I could read to better my understanding.
 
11:11 PM
I already worked a little with some other database drivers in some other languages. So when I saw the 2 question marks with only 1 argument (prod.getID()), I was suspicious
In general, working clean code tends to have some coherence, symmetry, corresponding types, ... For example, 2 bound values for 2 placeholders; closing two files in the opposite order they were created; summing quantities and quantities
So when you see incoherence (1 placeholder for 2 values, files opened and closed in same order, summing pries and quantities), you can be suspicious. And if it's normal, I suggest you to leave a comment so next people won't worry about it.
From that point, and when you told me the library package name, I checked on google and found the sources
Among other files, I read SerializerWriteString to see that it indeed bind only one value at Datas.STRING.setValue(dp, 1, obj);
And I read some other classes to understand how it works and provide you a (hopefully) working solution. (I got lots of experience in reading any kind of sources, so I can do that pretty quickly now.)
 

  last day (15 days later) »