· 7 years ago · Jan 24, 2019, 02:46 PM
1package com.example.iannis.myapplication4;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteOpenHelper;
7import android.util.Log;
8
9import java.text.SimpleDateFormat;
10import java.util.Date;
11
12public class DatabaseHelper extends SQLiteOpenHelper {
13 private static final String TABLE_NAME="cars_table";
14 private static final String COL1 = "_id";
15 private static final String COL2 = "carNr";
16 private static final String COL3 = "date";
17 private static final String COL4 = "position";
18 private static final String COL5 = "isPayed";
19
20
21 public DatabaseHelper(Context context) {
22 super(context, TABLE_NAME, null, 1);
23 }
24
25 @Override
26 public void onCreate(SQLiteDatabase sqLiteDatabase) {
27 String createtable = "create table "+TABLE_NAME + " ("+COL1+" integer primary key autoincrement, " + COL2+" integer not null, " + COL3+" text not null, " + COL4+" integer not null, " +COL5+" integer not null)";
28 sqLiteDatabase.execSQL(createtable);
29 }
30
31 @Override
32 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
33 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
34 onCreate(sqLiteDatabase);
35 }
36
37 public boolean addData(int carNr, Date date, int position, Boolean isPayed){
38 SQLiteDatabase db = this.getWritableDatabase();
39 ContentValues contentValues = new ContentValues();
40 contentValues.put(COL2, carNr);
41 SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy hh:mm");
42 contentValues.put(COL3, format.format(date));
43 contentValues.put(COL4, position);
44
45 int bool =0;
46 if(isPayed == true){
47 bool=1;
48 }
49 contentValues.put(COL5, bool);
50
51 Log.d("VALUESS",String.valueOf(contentValues));
52
53 long result = db.insert(TABLE_NAME, null, contentValues);
54
55 Log.d("RESULTATE",String.valueOf(result));
56
57 if(result == -1){
58 return false;
59 }else{
60 return true;
61 }
62
63 }
64}