· 7 years ago · Dec 22, 2018, 05:54 AM
1package com.jn.inventorykepper;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8
9import java.io.StringBufferInputStream;
10import java.util.ArrayList;
11
12public class DatabaseHelper extends SQLiteOpenHelper {
13
14 private static final String TAG = "DatabaseHelper";
15
16 private static final String DATABASE_NAME = "inventory.db";
17
18 private static final String COLLECTION_TABLE = "collection";
19 private static final String COLLECTION_COL1 = "collectionid";
20 private static final String COLLECTION_COL2 = "collectionname";
21
22 private static final String ITEM_TABLE = "item";
23 private static final String ITEM_COL1 = "itemid";
24 private static final String ITEM_COL2 = "collectionid";
25 private static final String ITEM__COL3 = "description";
26 private static final String ITEM__COL4 = "quantity";
27
28 public DatabaseHelper(Context context){
29 super(context, DATABASE_NAME, null, 1);
30 }
31
32 @Override
33 public void onCreate(SQLiteDatabase db){
34 String CREATE_CONTACTS_TABLE = "CREATE TABLE " + COLLECTION_TABLE +
35 "(" + COLLECTION_COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLLECTION_COL2 + " TEXT NOT NULL" + ")";
36 db.execSQL(CREATE_CONTACTS_TABLE);
37 }
38
39 @Override
40 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
41 db.execSQL("DROP TABLE IF EXISTS contacts");
42 onCreate(db);
43 }
44
45 public boolean insertCollection(String table, String name){
46 SQLiteDatabase db = this.getWritableDatabase();
47 ContentValues contentValues = new ContentValues();
48 contentValues.put("collectionname", name);
49 db.insert(table, null, contentValues);
50 return true;
51 }
52
53 // Get the whole collection table
54 public ArrayList<String> getAllContacts() {
55 ArrayList<String> array_list = new ArrayList<String>();
56
57 //hp = new HashMap();
58 SQLiteDatabase db = this.getReadableDatabase();
59 Cursor res = db.rawQuery( "select * from collection", null );
60 res.moveToFirst();
61
62 while(res.isAfterLast() == false){
63 array_list.add(res.getString(res.getColumnIndex(COLLECTION_COL2)));
64 res.moveToNext();
65 }
66 return array_list;
67 }
68
69
70
71 // Add new collection
72
73 // Delete a collection
74
75 // Add item to item
76
77 // Delete item from item
78
79 // Alter quantity of item
80
81 // Alter name (description) of item
82
83 // Get the whole "table" for input id
84 public Cursor getItemData(int id){
85 SQLiteDatabase db = this.getReadableDatabase();
86 Cursor res = db.rawQuery("select * from item where id="+id+"", null);
87 return res;
88 }
89}