samedi 11 avril 2015

Update SQlite ID

wat i need is to squentioal my ID after delete entry so my Id start as that ( 1 . 2 .3 ) if I deleted row 2 .. it will be like that ( 1 . 3 ) and wat i need is to be ( 1 .2 ) i know the ID is primary key so it can not be changed . but i have 2 ideas but i don't know how to implement them well first one is to delete the Db and then create it again second is make new column (Fake_ID) and make it consecutive to can display it to the user but i do not know to make it so this is my code and if someone can edit it and write the quries to do that because i'm stil beginner in android and sqlite , i will be aprreciate it thanks it andvance


MySQLiteHelper



public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_EMPLOYEES = "employees";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_EMPNAME = "empname";
public static final String COLUMN_AGE = "age";
private static final String DATABASE_NAME = "empdb.db";
private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_EMPLOYEES + "(" + COLUMN_ID
+ " integer primary key autoincrement not null , " + COLUMN_EMPNAME
+ " text not null , " + COLUMN_AGE
+ " integer not null);";

public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEES);
onCreate(db);

}


}


Employee



public class Employee {

private long id;
private int age;
private String empName;


public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return "Name: "+empName+" Age: "+age+" "+ "ID : "+id ;
}

}


EmployeesDataSource



public class EmployeesDataSource {


private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_EMPNAME, MySQLiteHelper.COLUMN_AGE , MySQLiteHelper.COLUMN_ID};

public EmployeesDataSource(Context context) {
// TODO Auto-generated constructor stub
dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public Employee createEmployee(String empname, int age ) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_EMPNAME, empname);
values.put(MySQLiteHelper.COLUMN_AGE, age);

long insertId = database.insert(MySQLiteHelper.TABLE_EMPLOYEES, null,values);

Cursor cursor = database.query(MySQLiteHelper.TABLE_EMPLOYEES,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Employee newComment = cursorToEmployee(cursor);
cursor.close();
return newComment;

}


public void deleteEmployee(long itemId){
open();
database.delete(MySQLiteHelper.TABLE_EMPLOYEES, "_id = ?", new String[]{Long.toString(itemId)});
close();
}







public ArrayList<Employee> getAllEmployees() {
ArrayList<Employee> employees = new ArrayList<Employee>();

Cursor cursor = database.query(MySQLiteHelper.TABLE_EMPLOYEES,
allColumns, null, null, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Employee employee = cursorToEmployee(cursor);
employees.add(employee);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return employees;
}

private Employee cursorToEmployee(Cursor cursor) {
Employee employee = new Employee();
employee.setId(cursor.getLong(0));
employee.setEmpName(cursor.getString(1));
employee.setAge(cursor.getInt(2));
return employee;
}

}

Aucun commentaire:

Enregistrer un commentaire