brunoribeiro / sqljet

Automatically exported from code.google.com/p/sqljet
0 stars 0 forks source link

update() with next() #186

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I want to update entries after a selection with lookup().
The problem is that it updates only the first one.

Here is my code :

long userId;
long machineId;
long duration;
long date;

db.beginTransaction(SqlJetTransactionMode.WRITE);
try
{
    // Get the unsent usages
    ISqlJetTable usage = db.getTable(USAGE_TABLE);
    ISqlJetCursor cursor = usage.lookup(SENT_INDEX, 0);
    if (!cursor.eof())
    {
        do
        {
            // Try to send the usage event to the REST server
            userId = cursor.getInteger(USER_ID_FIELD);
            machineId = cursor.getInteger(MACHINE_ID_FIELD);
            duration = cursor.getInteger(DURATION_FIELD);
            date = cursor.getInteger(DATE_FIELD);
            JSONObject response = rest.sendEvent(REST.CONSUM_EVENT, userId, machineId, duration, date);

            // If it succeed
            if (response != null)
            {
                        System.out.println(response.toString());

                // Update the usage sent state
                cursor.update(
                    cursor.getInteger(ID_FIELD),
                    userId,
                    machineId,
                    duration,
                    cursor.getFloat(COST_FIELD),
                    date,
                    1
                );
            }

            Thread.sleep(500);
        }
        while (cursor.next());
    }
    cursor.close();
}
catch (InterruptedException e)
{
    ErrorHandler.log(e);
}
finally
{
    db.commit();
}

Any idea?

Original issue reported on code.google.com by eadress_...@yahoo.fr on 20 Apr 2015 at 12:43

GoogleCodeExporter commented 8 years ago
next() return false after update().

Original comment by eadress_...@yahoo.fr on 20 Apr 2015 at 12:50

GoogleCodeExporter commented 8 years ago
Until there is a solution, I fixed my code like this :

db.beginTransaction(SqlJetTransactionMode.WRITE);
try
{
    // Get the unsent usages
    ISqlJetTable usage = db.getTable(USAGE_TABLE);
    ISqlJetCursor cursor = usage.open();
    if (!cursor.eof())
    {
        do
        {
            // Fix a conflit between lookup() and update()
            if (cursor.getInteger(SENT_INDEX) == 0)
            {
                // Try to send the usage event to the REST server
                userId = cursor.getInteger(USER_ID_FIELD);
                machineId = cursor.getInteger(MACHINE_ID_FIELD);
                duration = cursor.getInteger(DURATION_FIELD);
                date = cursor.getInteger(DATE_FIELD);
                JSONObject response = rest.sendEvent(REST.CONSUM_EVENT, userId, machineId, duration, false, date);

                // If it succeed
                if (response != null)
                {
                    // Update the usage sent state
                    cursor.update(
                        cursor.getValue(ID_FIELD),
                        userId,
                        machineId,
                        duration,
                        cursor.getValue(COST_FIELD),
                        date,
                        1
                    );
                }

                Thread.sleep(500);
            }
        }
        while (cursor.next());
    }
    cursor.close();
}
catch (InterruptedException e)
{
    ErrorHandler.log(e);
}
finally
{
    db.commit();
}

Original comment by eadress_...@yahoo.fr on 8 May 2015 at 8:28