Android 中的 INTEGER PRIMARY KEY AUTOINCREMENT 不起作用

INTEGER PRIMARY KEY AUTOINCREMENT in Android not working

我有以下代码:

package com.example.mayur.myapplication;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;
import android.util.Log;

import java.util.ArrayList;


public class dbhandler extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME="database1.db";
    private static final String TABLE_NAME= "Database";
    private static final String _ID="_id";
    private static final String COLUMN_NAME="Name";
    private static final String COLUMN_TITLE="Title";




    public dbhandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_NAME +" (_ID  INTEGER PRIMARY KEY AUTOINCREMENT,COLUMN_Name TEXT, COLUMN_Title TEXT)";
        db.execSQL(query);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS TABLE_NAME");
            onCreate(db);
    }
    public void addproduct(Database database)
    {
        ContentValues values=new ContentValues();
        values.put(COLUMN_TITLE,database.get_title());
        values.put(COLUMN_NAME,database.get_name());        
        SQLiteDatabase db=this.getWritableDatabase();
        db.insert(TABLE_NAME, null, values);
        db.close();

    }
    public void deleteproduct(String name)
    {
        SQLiteDatabase db =getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + COLUMN_NAME + "=\"" + name + "\"; ");
    }

    public ArrayList<Database> load()
    {
        Database d=new Database();
        ArrayList<Database> ret = new ArrayList<>();
        SQLiteDatabase db=getWritableDatabase();
        String query="SELECT * FROM " +  TABLE_NAME + " WHERE 1";
        Cursor c=db.rawQuery(query,null);
        c.moveToFirst();
        for(int i=0;i<c.getCount();i++)
        {
            d.set_name(c.getString(c.getColumnIndex("Name")));
            d.set_title(c.getString(c.getColumnIndex("Title")));
            ret.add(d);
            c.moveToNext();

                Log.d("id", "load: "+ ret.get(i).get_id());
        }
        db.close();

        return rSQLt;
    }
}

logd 在所有迭代的加载方法中始终显示 id=0。我也尝试过删除自动增量,但它不起作用,因为每次我尝试从 SQLite 数据库打印数据时,它总是打印最后一个条目。请帮忙

问题是您的列名在您创建数据库和插入数据库时​​不匹配。所以有两个解决方案:

要么更改这些变量:

private static final String COLUMN_NAME="COLUMN_Name";
private static final String COLUMN_TITLE="COLUMN_Title";

或更改 onCreate 中的代码:

String query = "CREATE TABLE " + TABLE_NAME +" (_ID  INTEGER PRIMARY KEY AUTOINCREMENT,"+COLUMN_Name+" TEXT, "+COLUMN_Title+" TEXT)";

这不是你的问题,但 AUTOINCREMENT 不需要具有自动递增字段。事实上,在大多数情况下,编码 AUTOINCREMENT 是不利的,因为它引入了不必要的开销。 AUTOINCREMENT 实际上意味着使该列在整个数据库中是唯一的。

这是来自 Autoincrement In SQLite

的 AUTOINCREMENT 摘要

Summary

The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It

is usually not needed.

In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID (except in WITHOUT ROWID tables) which is always a 64-bit

signed integer.

On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an

unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used.

If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the

reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.

您没有在对象中设置 _id d -

将此行添加到您的 for loop -

d.set_id(c.getInt(c.getColumnIndex("_ID")));