SQLite的自动递增不工作 [英] SQLite auto increment not working
本文介绍了SQLite的自动递增不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
确定这是不是垃圾邮件,它应该是简单的,我不知道为什么它不工作
这是我的code:
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);
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
(ID, Name, NPlayers, NRounds, WinScore ) VALUES ( NULL, 'TAWLA',2,0,0 );");
gamesdatabase.execSQL("INSERT INTO Games
(ID, Name, NPlayers, NRounds, WinScore ) VALUES ( NULL, '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();
}
这有什么错呢?所有记录的日志显示0
what's wrong with this ? the log displays 0 for all records
推荐答案
SQLite的表的主键被称为_id。它是自动递增,你不应该试图插入值到它。
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();
}
这篇关于SQLite的自动递增不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文