0

I am stuck on a really weird bug. I have a table followers, and when I insert datas, it works except for th me first entry.

The first time I call the function, nothing is thrown and the insert function return as expected 1. But when I call the select all function, the table is empty.

However when I call the function an other time with different parameters, the data are really inserted and the table shows one entry (this one) but with 2 as id.

I am using Sqldelight.

public long insertFollower(String name, String wca_id, long created_at) {

    try {
        SQLiteDatabase db = openDatabase();

        return db.insert(Follower.TABLE_NAME, null, Follower.FACTORY.marshal()
                .name(name)
                .wca_id(wca_id)
                .created_at(created_at)
                .asContentValues());
    }

    catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        closeDatabase();
    }

    return 0;
}

private static AtomicInteger openCount = new AtomicInteger();
private static SQLiteDatabase database;

public static final String DATABASE_NAME = "database.db";
public static final int DATABASE_VERSION = 12;

private static DatabaseHelper instance;

public static synchronized DatabaseHelper getInstance(Context context) {
    if(instance == null) {
        instance = new DatabaseHelper(context);
    }

    return instance;
}

public synchronized SQLiteDatabase openDatabase() {
    if(openCount.incrementAndGet() == 1) {
        database = getWritableDatabase();
    }

    return database;
}

public synchronized void closeDatabase() {
    if(openCount.decrementAndGet() == 0) {
        database.close();
    }
}


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(Follower.CREATE_TABLE);
    db.execSQL(Record.CREATE_TABLE);

}

And the Follower.CREATE_TABLE who was generated by sqldelight

  String CREATE_TABLE = ""
  + "CREATE TABLE follower (\r\n"
  + "    _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\r\n"
  + "    name TEXT NOT NULL,\r\n"
  + "    wca_id TEXT NOT NULL\r\n"
  + ")";

EDIT :

I noticed something strange that may help:

This works only if the table is empty

I insert a follower, that doesn't works. But when I insert an other follower with the same ID as the first one (there is a primary key autoincrement). I get a UNIQUE exception but the table is still empty.

Do not hesitate to ask more information.

Thank you.

  • Can you use a tool like SQLiteDebugger to verify that the table is empty (as opposed to your coded select statement) – Al Lelopath Sep 21 '16 at 20:43
  • Hi Al Lelopath, thanks for your fast answer. I think I won't be able to use SqliteDebugger because my phone isn't rooted and my computer cannot emulate a phone. :/ – Herel Adrastel Sep 21 '16 at 20:48
  • Do the parameters matter? E.g. if you switch the parameters of the first and second insert, do you still get the same error? What about a third insert, what does it do? It may be that the 2nd insert is overwriting the first. – Al Lelopath Sep 21 '16 at 20:55
  • The parameter doesn't change. If you switch, the first line won't be written. And if you call twice with the same parameters for the first time. The two lines won't be written. And if you call a third time, it's the same result as the second time, the line is written provided that it doesn't have the same parameters as the first line. I know this is a really weird bug. I used the Android debugger and everything seems to be OK. – Herel Adrastel Sep 21 '16 at 21:01
  • I'm not clear, in the question you say "different parameters" and in your comment you say that the parameter doesn't change. Which is it? – Al Lelopath Sep 21 '16 at 21:18
  • Can you share what your CREATE TABLE statement looks like? And also what openDatabase() and closeDatabase() do? – Anstrong Sep 22 '16 at 15:25
  • Hey, Anstrong I added the code. And to anwser your question Al Lelopath, I wasn't clear, with differents parameters, the problem is still there – Herel Adrastel Sep 22 '16 at 15:46
  • I added others things in the edit, don't hesitate to check – Herel Adrastel Sep 22 '16 at 17:52
  • What are you doing to query the table? Just SELECT * FROM follower? What happens if you do `database.rawQuery("SELECT * FROM follower", new String[0]).getCount();` right after the insert, what does it print after each insert? – Anstrong Sep 22 '16 at 19:05

1 Answers1

0

I finally find the answer thanks to @anstrong: to select the follower I used cursor.moveToFirst() and the while(cursor.moveToNext()) was called just after