52
public void onCreate(SQLiteDatabase db) {
// TODO
Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(ToDoDatabase.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS todos");
onCreate(db);
}
public long createNewTodo(String category,
String summary,
String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues initialValues = createContentValues(category, summary,
description);
long row = db.insert(DATABASE_TABLE, null, initialValues);
db.close();
return row;
}
/**
* ro‘yxatni yangilaydi
*/
public boolean updateTodo(long rowId, String category, String summary,
String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updateValues = createContentValues(category, summary,
description);
return db.update(DATABASE_TABLE, updateValues, COLUMN_ID + "=" +
rowId,
null) > 0;
}
/**
* ro‘yxat elementlarini o‘chiradi
*/
public void deleteTodo(long rowId) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DATABASE_TABLE, COLUMN_ID + "=" + rowId, null);
db.close();
}
/**
*
53
* @return hamma satrlar
kursorlarini qaytaradi
*/
public Cursor getAllTodos() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor
mCursor
=
db.query(DATABASE_TABLE,
new
String[]
{
COLUMN_ID,
COLUMN_CATEGORY,
COLUMN_SUMMARY,
COLUMN_DESCRIPTION }, COLUMN_CATEGORY + "='Dushanba'",
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Ko‘rsatilgan satrdagi kursor qiymatini qaytaradi
*/
public Cursor getTodo(long rowId) throws SQLException {
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = db.query(true, DATABASE_TABLE,
new
String[]
{
COLUMN_ID,
COLUMN_CATEGORY,
COLUMN_SUMMARY,
COLUMN_DESCRIPTION }, COLUMN_ID + "="
+ rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/*
* Qiymatlarni bazaga yozib qo‘yish
*/
private ContentValues createContentValues(String category, String summary,
String description) {
ContentValues values = new ContentValues();
values.put(COLUMN_CATEGORY, category);
values.put(COLUMN_SUMMARY, summary);
values.put(COLUMN_DESCRIPTION, description);
return values;
}
}