Android中的INTEGER PRIMARY KEY AUTOINCREMENT无法正常工作

时间:2016-04-15 22:33:13

标签: android sqlite android-sqlite auto-increment

我有以下代码:

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始终在load方法中显示id = 0。我也尝试删除自动增量,但它不起作用,因为每次我尝试从SQLite数据库打印数据时,它总是打印最后一个条目。请帮忙。

3 个答案:

答案 0 :(得分:0)

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

更改这些变量:

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)";

答案 1 :(得分:0)

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

以下是来自Autoincrement In SQLite

的AUTOINCREMENT摘要
  

摘要

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

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
     

有符号整数。

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
     

未使用的整数,通常比当前最大的ROWID多一个   使用。无论AUTOINCREMENT是否正确,都是如此   使用了关键字。

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

在数据库的生命周期内重用ROWID。换句话说,   AUTOINCREMENT的目的是防止重用ROWID   以前删除的行。

答案 2 :(得分:0)

您没有在对象_id -

中设置d

for loop -

中添加此行
d.set_id(c.getInt(c.getColumnIndex("_ID")));