· 10 years ago · Sep 14, 2016, 07:50 AM
1package se.mah.ae3209.project1finance;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.util.Log;
9
10/**
11 * Created by Tim on 2016-09-13.
12 */
13public class DBHelper extends SQLiteOpenHelper {
14 public static final String TABLE_NAME = "incomes";
15 public static final String COLUMN_ID = "id";
16 public static final String COLUMN_LABEL = "label";
17 public static final String COLUMN_VALUE = "value";
18
19 private static final String DATABASE_NAME = "income.db";
20 private static final int DATABASE_VERSION = 1;
21
22 private static final String DATABASE_CREATE =
23 "create table " + TABLE_NAME + "(" +
24 COLUMN_ID + " text not null primary key, " +
25 COLUMN_LABEL + " text not null, " +
26 COLUMN_VALUE + " integer);";
27
28
29
30 // DATABASE_VERSION ska ändras varje gång man designar om tabellen,
31 // t.ex. Från 1 till 2 till 3 osv
32 public DBHelper(Context context)
33 {super(context, DATABASE_NAME, null, DATABASE_VERSION);
34
35 }
36
37 @Override
38 public void onCreate(SQLiteDatabase db)
39 {
40 db.execSQL(DATABASE_CREATE);
41 }
42
43 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
44 Log.w(DBHelper.class.getName(),
45 "Upgrading database from version " + oldVersion + " to "
46 + newVersion + ", which will destroy all old data");
47 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
48 onCreate(db);
49 }
50
51
52 public void addIncome(DBIncome income)
53 {
54 SQLiteDatabase db = getWritableDatabase();
55 ContentValues values = new ContentValues();
56 values.put(DBHelper.COLUMN_ID, income.getID());
57 values.put(DBHelper.COLUMN_LABEL, income.getLabel());
58 values.put(DBHelper.COLUMN_VALUE, income.getIncome());
59 db.insert(DBHelper.TABLE_NAME,"",values);
60 }
61 private void updateIncome(DBIncome income)
62 {
63 SQLiteDatabase db = getWritableDatabase();
64 ContentValues values = new ContentValues();
65 values.put(DBHelper.COLUMN_LABEL, income.getLabel());
66 values.put(DBHelper.COLUMN_VALUE, income.getIncome());
67 db.update(DBHelper.TABLE_NAME, values, DBHelper.COLUMN_ID+"="+income.getID()+"", null);
68
69 }
70
71 public DBIncome[] getAllIncome(){
72 int idIndex, nameIndex, pointsIndex;
73
74 SQLiteDatabase db = getReadableDatabase();
75 Cursor cursor = db.rawQuery("SELECT * FROM " + DBHelper.TABLE_NAME
76 + " ORDER BY " + DBHelper.COLUMN_VALUE, null);
77
78 DBIncome[] incomes = new DBIncome[cursor.getCount()];
79 idIndex = cursor.getColumnIndex(DBHelper.COLUMN_ID);
80 nameIndex = cursor.getColumnIndex(DBHelper.COLUMN_LABEL);
81 pointsIndex = cursor.getColumnIndex(DBHelper.COLUMN_VALUE);
82
83 for(int i=0; i<incomes.length; i++)
84 {
85 cursor.moveToPosition(i);
86 incomes[i] = new DBIncome(cursor.getInt(idIndex),
87 cursor.getString(nameIndex),
88 cursor.getInt(pointsIndex));
89 }
90 return incomes;
91
92 }
93
94
95 //gör inget lmao
96 public int getValues(DBIncome AYY)
97 {
98 int returint;
99 returint = AYY.getIncome();
100
101 return returint;
102 }
103
104 public void insert(int id, String label, int value)
105 {
106 addIncome(new DBIncome(id, label, value));
107 }
108
109}
110
111Och sen i main
112private void initSystem()
113 {
114
115
116 ///....\\\\\
117
118 db.insert(0, "lön", 12000);
119
120
121
122
123 }
124
125
126}