Sqlite Auto Increment Not Working
ok this isn't spamming and it's supposed to be simple I don't know why it's not working this is my code: gamesdatabase = openOrCreateDatabase('GamesDatabase', MODE_PRIVATE, null);
Solution 1:
The primary key for SQLite tables is called _id. It is auto incrementing, and you should not be trying to insert values into it.
gamesdatabase = openOrCreateDatabase("GamesDatabase", MODE_PRIVATE, null);
gamesdatabase.execSQL("CREATE TABLE IF NOT EXISTS Games (_id INTEGER PRIMARY KEY, Name
VARACHAR, NPlayers INT(1), NRounds INT(2), WinScore INT(2));");
gamesdatabase.execSQL("INSERT INTO Games
(Name, NPlayers, NRounds, WinScore ) VALUES ('TAWLA',2,0,0 );");
gamesdatabase.execSQL("INSERT INTO Games
(Name, NPlayers, NRounds, WinScore ) VALUES ('DOMANA',4,0,0 );");
Cursor c = gamesdatabase.rawQuery("SELECT * FROM Games", null);
c.moveToFirst();
while (c.isAfterLast() == false) {
Log.d("BEZRA", String.valueOf(c.getInt(c.getColumnIndex("_id"))));
c.moveToNext();
}
Solution 2:
What worked for me was renaming my create type from INT to INTEGER and it started working.
From this:
CREATETABLE IF NOTEXISTS foo (id INTPRIMARY KEY, bar INT)
to this:
CREATETABLE IF NOTEXISTS foo (id INTEGERPRIMARY KEY, bar INT)
Solution 3:
c.getColumnIndex("ID")
gets the index of the column, which ID is 0 indexed column, Name is 1 etc
you want
c.getInt(c.getColumnIndex("ID"))
Solution 4:
You can see http://alvinalexander.com/android/sqlite-autoincrement-serial-identity-primary-keyExample CRETAE_TABLE
CREATETABLE salespeople (
id INTEGERPRIMARY KEY,
first_name TEXT NOTNULL,
last_name TEXT NOTNULL,
commission_rate REALNOTNULL
);
INSERT_DATA
INSERTINTO salespeople VALUES (null, 'Fred', 'Flinstone', 10.0);
---OR---
INSERTINTO salespeople (first_name, last_name, commission_rate) VALUES ('Fred', 'Flinstone', 10.0);
Solution 5:
You want to tell it that the column is auto incrementing.
ID INTEGERPRIMARY KEY AUTO_INCREMENT NOTNULL
instead.
Post a Comment for "Sqlite Auto Increment Not Working"