-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHelper.java
More file actions
159 lines (121 loc) · 5.35 KB
/
DatabaseHelper.java
File metadata and controls
159 lines (121 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.example.myinventoryapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "inventory_manager";
// Table Names
private static final String TABLE_USERS = "users";
private static final String TABLE_INVENTORY = "inventory";
// Common column names
private static final String KEY_ID = "id";
// USERS Table - column names
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
// INVENTORY Table - column names
private static final String KEY_ITEM_NAME = "item_name";
private static final String KEY_QUANTITY = "quantity";
private static final String KEY_THRESHOLD = "threshold";
// Table Create Statements
// Users table create statement
private static final String CREATE_TABLE_USERS = "CREATE TABLE " + TABLE_USERS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_USERNAME + " TEXT UNIQUE NOT NULL,"
+ KEY_PASSWORD + " TEXT NOT NULL" + ")";
// Inventory table create statement
private static final String CREATE_TABLE_INVENTORY = "CREATE TABLE " + TABLE_INVENTORY + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_ITEM_NAME + " TEXT UNIQUE NOT NULL,"
+ KEY_QUANTITY + " INTEGER NOT NULL,"
+ KEY_THRESHOLD + " INTEGER NOT NULL DEFAULT 5" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Creating required tables
db.execSQL(CREATE_TABLE_USERS);
db.execSQL(CREATE_TABLE_INVENTORY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// On upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_INVENTORY);
// Create new tables
onCreate(db);
}
// ========== USER METHODS ==========
// Check if user exists
public boolean checkUser(String username, String password) {
SQLiteDatabase db = this.getReadableDatabase();
String selection = KEY_USERNAME + " = ? AND " + KEY_PASSWORD + " = ?";
String[] selectionArgs = {username, password};
Cursor cursor = db.query(TABLE_USERS, null, selection, selectionArgs, null, null, null);
int count = cursor.getCount();
cursor.close();
return count > 0;
}
// Add new user
public long addUser(String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USERNAME, username);
values.put(KEY_PASSWORD, password);
// Insert row
long id = db.insert(TABLE_USERS, null, values);
return id;
}
// ========== INVENTORY METHODS ==========
// Add new inventory item
public long addInventoryItem(String itemName, int quantity, int threshold) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ITEM_NAME, itemName);
values.put(KEY_QUANTITY, quantity);
values.put(KEY_THRESHOLD, threshold);
// Insert row
long id = db.insert(TABLE_INVENTORY, null, values);
return id;
}
// Update inventory item quantity
public int updateInventoryItem(long id, int quantity) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUANTITY, quantity);
// Updating row
return db.update(TABLE_INVENTORY, values, KEY_ID + " = ?", new String[]{String.valueOf(id)});
}
// Update inventory item completely
public int updateInventoryItem(long id, String itemName, int quantity, int threshold) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ITEM_NAME, itemName);
values.put(KEY_QUANTITY, quantity);
values.put(KEY_THRESHOLD, threshold);
// Updating row
return db.update(TABLE_INVENTORY, values, KEY_ID + " = ?", new String[]{String.valueOf(id)});
}
// Delete inventory item
public void deleteInventoryItem(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_INVENTORY, KEY_ID + " = ?", new String[]{String.valueOf(id)});
}
// Get all inventory items
public Cursor getAllInventoryItems() {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_INVENTORY;
return db.rawQuery(selectQuery, null);
}
// Get inventory items below threshold for notifications
public Cursor getLowInventoryItems() {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_INVENTORY + " WHERE " + KEY_QUANTITY + " <= " + KEY_THRESHOLD;
return db.rawQuery(selectQuery, null);
}
}