0% found this document useful (0 votes)
16 views

Sqlite Docs

Uploaded by

Good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Sqlite Docs

Uploaded by

Good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CREATE TABLE expense(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT

NOT NULL, date TEXT NOT NULL, amount REAL NOT NULL, categoryId INTEGER,
categoryName TEXT NOT NULL, is_deleted INTEGER DEFAULT 0);

CREATE TABLE category(id INTEGER PRIMARY KEY AUTOINCREMENT, category_name


TEXT NOT NULL, is_deleted INTEGER DEFAULT 0);

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "ExpenseManager.db";


// Table Names
private static final String TABLE_EXPENSE = "expense";
private static final String TABLE_CATEGORY = "category";

// Common Column
private static final String COLUMN_IS_DELETED = "is_deleted";

// Category Table Columns


private static final String COLUMN_CATEGORY_ID = "id";
private static final String COLUMN_CATEGORY_NAME = "category_name";

// Expense Table Columns


private static final String COLUMN_EXPENSE_ID = "id";
private static final String COLUMN_EXPENSE_NAME = "name";
private static final String COLUMN_EXPENSE_DATE = "date";
private static final String COLUMN_EXPENSE_AMOUNT = "amount";
private static final String COLUMN_EXPENSE_CATEGORY_ID = "categoryId";
private static final String COLUMN_EXPENSE_CATEGORY_NAME = "categoryName";

private static final String CREATE_TABLE_CATEGORY = "CREATE TABLE " +


TABLE_CATEGORY + "("
+ COLUMN_CATEGORY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_CATEGORY_NAME + " TEXT NOT NULL, "
+ COLUMN_IS_DELETED + " INTEGER DEFAULT 0);";

// Create Expense Table


private static final String CREATE_TABLE_EXPENSE = "CREATE TABLE " +
TABLE_EXPENSE + "("
+ COLUMN_EXPENSE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_EXPENSE_NAME + " TEXT NOT NULL, "
+ COLUMN_EXPENSE_DATE + " TEXT NOT NULL, "
+ COLUMN_EXPENSE_AMOUNT + " REAL NOT NULL, "
+ COLUMN_EXPENSE_CATEGORY_ID + " INTEGER, "
+ COLUMN_EXPENSE_CATEGORY_NAME + " TEXT NOT NULL, "
+ COLUMN_IS_DELETED + " INTEGER DEFAULT 0"
+ ");";

db.execSQL("DROP TABLE IF EXISTS " + TABLE_EXPENSE);


db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORY);

db.execSQL(CREATE_TABLE_EXPENSE);
db.execSQL(CREATE_TABLE_CATEGORY);

public long insertExpense(Expense expense) {


SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_EXPENSE_CATEGORY_NAME,
expense.getCategoryName());
contentValues.put(COLUMN_EXPENSE_CATEGORY_ID, expense.getCategoryId());
contentValues.put(COLUMN_EXPENSE_AMOUNT, expense.getAmount());
contentValues.put(COLUMN_EXPENSE_NAME, expense.getName());
contentValues.put(COLUMN_EXPENSE_DATE, expense.getDate());
long id = sqLiteDatabase.insert(TABLE_EXPENSE, null, contentValues);
return id;
}

public ArrayList<Expense> getAllExpenses() {


ArrayList<Expense> list = new ArrayList<>();
SQLiteDatabase database = this.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + TABLE_EXPENSE + "
WHERE " + COLUMN_IS_DELETED + "=0 ORDER BY id DESC ", null);
if (cursor.moveToFirst()) {
do {
Expense expense = new Expense();

expense.setName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_EXPENSE
_NAME)));

expense.setDate(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_EXPENSE
_DATE)));
expense.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_EXPENSE_ID))
);

expense.setAmount(cursor.getDouble(cursor.getColumnIndexOrThrow(COLUMN_EXPEN
SE_AMOUNT)));

expense.setCategoryId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_CATE
GORY_ID)));

expense.setCategoryName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN
_CATEGORY_NAME)));
list.add(expense);
} while (cursor.moveToNext());
}
Log.d("mytag", "" + list.size());
return list;
}

public ArrayList<Category> getAllCategories() {


ArrayList<Category> list = new ArrayList<>();
SQLiteDatabase database = this.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + TABLE_CATEGORY + "
WHERE " + COLUMN_IS_DELETED + "=0 ORDER BY id DESC ", null);
if (cursor.moveToFirst()) {
do {
Category category = new Category();

category.setName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CATEGO
RY_NAME)));

category.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_CATEGORY_ID
)));
list.add(category);
} while (cursor.moveToNext());
}
Log.d("mytag", "" + list.size());
return list;
}

https://developer.android.com/training/data-storage/sqlite

You might also like