for some reas" />
B
B
beduin012016-07-18 16:06:15
PostgreSQL
beduin01, 2016-07-18 16:06:15

Hack preventing re-insertion of data in PostgreSQL?

There is a very crooked project. In it, a request is sent from a web form to insert data into the PostgreSQL database.
After execution: ` command.ExecuteNonQuery(); ` for some reason, there are two instances of data in the database, although the function is called once.

public void Start()
{
            NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=123;Database=test;");
            conn.Open();

            NpgsqlCommand command = new NpgsqlCommand("insert into zayav.\"REQUESTS\"  (time_beg, time_end, sol_angle_min, sol_angle_max, priority, fl_ready, prod_type_id, customer_id) values(" + "'" + Beg_Planir_s + "','" + End_planir_s + "','" + Sol_angl_min_s + "','" + Sol_angl_max_s + "','" + Priority_s + "','" + Fl_ready_s + "','" + Prod_type_id_s + "','" + Customer_id_s + "')", conn);
            command.ExecuteNonQuery();
                    
            conn.Close();
}

Why this happens is completely unclear.
Interested in any of the dirtiest hack that would help to send data only once. It is possible on the DB side, it is possible in Sharpe itself.
There are simply no ideas why this method does not work. You just need to somehow get around this problem.
PS I don't care about performance and so on. It is required stupidly for the report. There the whole project is such a curve. It's easier to remake from scratch. Those. options for any crutches are completely satisfied.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pi314, 2016-07-18
@beduin01

What exactly does "help" mean? There is most likely no unequivocal hack that will solve everything by itself. The dirtiest hack you can think of offhand is to add a constraint to the database for the uniqueness of the combination of all inserted fields, like:

CREATE TABLE example (
    a integer,
    b integer,
    c integer,
    UNIQUE (a, c)
);

Details: One - Two .
This will exclude the very possibility of re-inserting at the database level, however, it will greatly reduce the performance of inserts in general (and, at the same time, may break other crooked places, if any). Whether this will "help" is an ambiguous question, because if the problem is really in the re-insertion, in the code somewhere (most likely in the bowels of NpgsqlCommand), an Exception (constraint violation) will pop up. Perhaps this will help to find a crooked place :) At the same time, it will help to find out if there are already duplicates in the database, because. if there are, the constraint simply won't be added until it's cleared out.
And besides, "there are two instances of data in the database" itself requires clarification. If you look at the database with some independent tool, then this is one thing, but if with the help of the "crooked project" itself, then the problem can easily be not in the database, but in the query with which it selects data from the database, or in general in architecture (when the second instance appears not from the database, but from some internal list, where it is stupidly inserted). In this case, adding a constraint will not do anything, but, again, this will help to localize the problem.
UPD: In any case, the most correct thing is to use this (or similar) hack to find the problem, and then remove the hack! :)
UPD2: Judging by the way the field values ​​are passed for insertion (to the constructor), this "project" is an Eldorado for SQL Injection lovers. So, don't be surprised when one not so beautiful day hackers send you a greeting card :)

C
ComodoHacker, 2016-07-18
@ComodoHacker

I would advise not to look for dirty hacks, but to find the cause and eliminate it. First, insert the logging into the method.
PS What a terrible code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question