30/09/2018, 19:59

ID không tự động tăng trong SQLite

em đang có một database:
e đã add đối tượng vào nhưng khi getall rồi đổ vào listview thì id của 2 đối tượng đều bằng 0
bác nào xem code e sai chỗ nào chỉ e với

package app.nghia.com.duan.database;

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

import java.util.ArrayList;
import java.util.List;

import app.nghia.com.duan.Words.WordsEnglish;

/**
 * Created by nghia on 3/17/2016.
 */
public class Database extends SQLiteOpenHelper {
    public static final String TAG="tag";
    public static final String DATABASE_NAME="DATABASE_ALRM";
    public static final String TABLE_NAME="TABLE_CLOCK";
    public static final String COL_TU="COLUMN_TU";
    public static final String COL_NGHIA="COLUMN_NGHIA";
    public static final String ID="id";
    public static final int VERSION=1;
    public static final String TAO_BANG="CREATE TABLE "+TABLE_NAME+"("+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ COL_TU +" TEXT, "+ COL_NGHIA +" TEXT "+")";
    public Database(Context context) {
        super(context, DATABASE_NAME, null, VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TAO_BANG);
        Log.d(TAG, "onCreating........");
        WordsEnglish english=new WordsEnglish();
        Log.d(TAG,english.getId()+"" );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(" DROP TABLE IF EXISTS "+TABLE_NAME);
        Log.d(TAG, "upGrade........");
        onCreate(db);
    }
    public void addTu(WordsEnglish wordsEnglish){
        SQLiteDatabase database=this.getWritableDatabase();
        ContentValues values=new ContentValues();

        values.put(COL_TU,wordsEnglish.getTu());
        values.put(COL_NGHIA, wordsEnglish.getNghia());
        database.insert(TABLE_NAME, null, values);
        database.close();
        Log.d(TAG, "ADD...");
    }
    public List<WordsEnglish> getAllTu(){
        ArrayList<WordsEnglish>arrTu=new ArrayList<>();
        SQLiteDatabase database=this.getReadableDatabase();
        String truyvan="SELECT * FROM "+TABLE_NAME;
        Cursor cursor=database.rawQuery(truyvan,null);
        if (cursor.getCount()>0){
            while (cursor.moveToNext()){
                int id_tu=cursor.getInt(Integer.parseInt(cursor.getString(0)));
                String tu=cursor.getString(1);
                String nghia=cursor.getString(2);
                WordsEnglish english=new WordsEnglish();
                english.setId(id_tu);
                english.setNghia(nghia);
                english.setTu(tu);
                arrTu.add(english);

            }
            cursor.close();
        }
        Log.d(TAG,"getALl.....");

        return arrTu;
    }
    public void deleteAll(){
        SQLiteDatabase database=this.getWritableDatabase();
        database.delete(TABLE_NAME,null,null);

    }
}
Bài liên quan
0