0

以下は、私が問題を抱えているコードのスニペットです。目的は、データベース内の重複するエントリをチェックし、trueまたはfalseの場合はブール値で「h」を返すことです。テストの目的で、「h」の真のブール値を返していますが、alert(duplicate_count);までに。行が実行されます。duplicate_countはまだ0です。+1のアラートが実行されても。

私には、関数updateUserFieldsの実行に時間がかかっているように見えるため、アラートに到達するまでに完了するのに時間がかかります。

何かアイデアや提案はありますか?ありがとう!

var duplicate_count = 0

for (var i = 0; i < skill_id.length; i++) {

    function updateUserFields(h) {
        if(h) {
            duplicate_count++;
            alert("count +1");
        } else {
            alert("none found");
        }
    }

    var g = new cfc_mentoring_find_mentor();
    g.setCallbackHandler(updateUserFields);
    g.is_relationship_duplicate(resource_id, mentee_id, section_id[i], skill_id[i], active_ind,table);
};

alert(duplicate_count);
4

1 に答える 1

1

There is no reason whatsoever to use client-side JavaScript/jQuery to remove duplicates from your database. Security concerns aside (and there are a lot of those), there is a much easier way to make sure the entries in your database are unique: use SQL.

SQL is capable of expressing the requirement that there be no duplicates in a table column, and the database engine will enforce that for you, never letting you insert a duplicate entry in the first place. The syntax varies very slightly by database engine, but whenever you create the table you can specify that a column must be unique.

Let's use SQLite as our example database engine. The relevant part of your problem is right now probably expressed with tables something like this:

CREATE TABLE Person(
    id INTEGER PRIMARY KEY ASC,
    -- Other fields here
);
CREATE TABLE MentorRelationship(
    id INTEGER PRIMARY KEY ASC,
    mentorID INTEGER,
    menteeID INTEGER,
    FOREIGN KEY (mentorID) REFERENCES Person(id),
    FOREIGN KEY (menteeID) REFERENCES Person(id)
);

However, you can make enforce uniqueness i.e. require that any (mentorID, menteeID) pair is unique, by changing the pair (mentorID, menteeID) to be the primary key. This works because you are only allowed one copy of each primary key. Then, the MentorRelationship table becomes

CREATE TABLE MentorRelationship(
    mentorID INTEGER,
    menteeID INTEGER,
    PRIMARY KEY (mentorID, menteeID),
    FOREIGN KEY (mentorID) REFERENCES Person(id),
    FOREIGN KEY (menteeID) REFERENCES Person(id)
);

EDIT: As per the comment, alerting the user to duplicates but not actually removing them

This is still much better with SQL than with JavaScript. When you do this in JavaScript, you read one database row at a time, send it over the network, wait for it to come to your page, process it, throw it away, and then request the next one. With SQL, all the hard work is done by the database engine, and you don't lose time by transferring unnecessary data over the network. Using the first set of table definitions above, you could write

SELECT mentorID, menteeID
FROM MentorRelationship
GROUP BY mentorID, menteeID
HAVING COUNT(*) > 1;

which will return all the (mentorID, menteeID) pairs that occur more than once.

Once you have a query like this working on the server (and are also pulling out all the information you want to show to the user, which is presumably more than just a pair of IDs), you need to send this over the network to the user's web browser. Essentially, on the server side you map a URL to return this information in some convenient form (JSON, XML, etc.), and on the client side you read this information by contacting that URL with an AJAX call (see jQuery's website for some code examples), and then display that information to the user. No need to write in JavaScript what a database engine will execute orders of magnitude faster.


EDIT 2: As per the second comment, checking whether an item is already in the database

Almost everything I said in the first edit applies, except for two changes: the schema and the query. The schema should become the second of the two schemas I posted, since you don't want the database engine to allow duplicates. Also, the query should be simply

SELECT COUNT(*) > 0
FROM MentorRelationship
WHERE mentorID = @mentorID AND menteeID = @menteeID;

where @mentorID and @menteeID are the items that the user selected, and are inserted into the query by a query builder library and not by string concatenation. Then, the server will get a true value if the item is already in the database, and a false value otherwise. The server can send that back to the client via AJAX as before, and the client (that's your JavaScript page) can alert the user if the item is already in the database.

于 2012-04-29T00:23:51.870 に答える