· 8 years ago · Jan 08, 2018, 11:58 PM
1package com.example.andrei.gymhelp;
2
3import android.content.Context;
4import android.database.SQLException;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteOpenHelper;
7import android.util.Log;
8
9
10public class DatabaseHelper extends SQLiteOpenHelper {
11
12private static final String TAG = "SQLMessage";
13private static final String DATABASE_NAME = "StoredResults.db";
14private static final int DATABASE_VERSION = 2;
15private static DatabaseHelper mDatabaseInstance = null;
16private Context mContext;
17
18
19public static DatabaseHelper newInstance(Context context){
20 //first check to see if the database helper
21 //member data is null
22 //create a new one if it is null
23
24 if (mDatabaseInstance == null){
25 mDatabaseInstance = new DatabaseHelper(context.getApplicationContext());
26 }
27
28 //either way we have to always return an instance of
29 //our database class each time this method is called
30 return mDatabaseInstance;
31}
32
33
34
35public DatabaseHelper(Context context) {
36
37 super(context, DATABASE_NAME, null, DATABASE_VERSION);
38 mContext = context;
39}
40private static final String CREATE_TABLE_LAPS = "create table results_table "
41 + "("
42 + "_id" + " integer primary key autoincrement, "
43 + "timestamp" + " text not null, "
44 + "laps" + " text not null, "
45 + ")";
46 //private static final String CREATE_TABLE_LAPS =
47// "CREATE TABLE " + Constants.LAPS_TABLE + "("
48// + Constants.COLUMN_ID + " INTEGER PRIMARY KEY
49AUTOINCREMENT,"
50// + Constants.COLUMN_TIMESTAMP + " TEXT NOT NULL, "
51// + Constants.COLUMN_LAPS + " TEXT NOT NULL, "
52// + ")";
53
54@Override
55public void onCreate(SQLiteDatabase db) {
56 try{
57 db.execSQL(CREATE_TABLE_LAPS);
58 Log.d(TAG, "TABLE WAS CREATED ");
59 } catch (SQLException e) {
60 Log.d(TAG, " Error create database " + e.getMessage());
61 }
62}
63
64@Override
65public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
66 db.execSQL("DROP TABLE IF EXISTS " + Constants.LAPS_TABLE);
67 onCreate(db);
68
69 }
70}
71
72package com.example.andrei.gymhelp;
73
74import android.content.ContentValues;
75import android.content.Context;
76import android.database.Cursor;
77import android.database.SQLException;
78import android.database.sqlite.SQLiteDatabase;
79import android.support.v7.app.AppCompatActivity;
80import android.os.Bundle;
81import android.text.format.DateFormat;
82import android.util.Log;
83import android.view.View;
84import android.widget.Button;
85import android.widget.ScrollView;
86import android.widget.TextView;
87import android.widget.Toast;
88
89import java.util.Date;
90import java.util.Hashtable;
91
92public class TimerActivity extends AppCompatActivity {
93
94private Context context;
95private TextView timer;
96private Button btnStart;
97private Button btnLap;
98private Button btnStop;
99private Button btnSave;
100private TextView laps;
101private int lapsCounter = 1;
102private ScrollView scrollLaps;
103private boolean saved = false;
104private String savedLaps = "";
105private static final String TAG = "MESSAGE";
106Date dateAndTime = new Date();
107private String sequenceTimestamp;
108
109
110private DatabaseHelper mDBHelper;
111private SQLiteDatabase mDatabase;
112
113private Chronometer mainChronometer;
114private Thread threadChrono;
115
116@Override
117protected void onCreate(Bundle savedInstanceState) {
118 super.onCreate(savedInstanceState);
119 setContentView(R.layout.layout_timer);
120
121
122 mDBHelper = new DatabaseHelper(this);
123 mDatabase = mDBHelper.getWritableDatabase();
124
125
126
127
128
129 btnSave.setOnClickListener(new View.OnClickListener() {
130 @Override
131 public void onClick(View v) {
132 if (mainChronometer != null) {
133
134 Context context = getApplicationContext();
135 CharSequence text = getString(R.string.chrono_not_stopped);
136 int duration = Toast.LENGTH_SHORT;
137
138 Toast toast = Toast.makeText(context, text, duration);
139 toast.show();
140
141 }else{
142 if (!saved) {
143
144 Log.v(TAG, "LAPS = " + savedLaps);
145 Log.v(TAG, String.valueOf(System.currentTimeMillis()));
146 saved = true;
147
148 Context context = getApplicationContext();
149 CharSequence text = getString(R.string.saved_toast);
150 int duration = Toast.LENGTH_SHORT;
151
152 Toast toast = Toast.makeText(context, text, duration);
153 toast.show();
154 sequenceTimestamp = (String) DateFormat.format("hh:mm:ss dd-MM-yyyy", dateAndTime.getTime());
155
156 saveResultsToDatabase(sequenceTimestamp, savedLaps);
157 Log.v(TAG, sequenceTimestamp);
158
159 } else {
160 Context context = getApplicationContext();
161 CharSequence text = getString(R.string.alreadysaved_toast);
162 int duration = Toast.LENGTH_SHORT;
163
164 Toast toast = Toast.makeText(context, text, duration);
165 toast.show();
166 }
167 }
168 }
169 });
170
171}
172
173public void updateTimer(final String time){
174 runOnUiThread(new Runnable() {
175 @Override
176 public void run() {
177 timer.setText(time);
178 }
179 });
180}
181public void saveResultsToDatabase(String timestamp,String stringOfLaps) {
182 //if (mDatabase != null) {
183 //prepare the transaction information that will be saved to the database
184 ContentValues values = new ContentValues();
185 values.put(Constants.COLUMN_TIMESTAMP, timestamp);
186 values.put(Constants.COLUMN_LAPS, stringOfLaps);
187
188 try {
189
190
191 Log.i(TAG,"SAVE RESULTS WAS CALLED");
192 mDatabase.insert(Constants.LAPS_TABLE, null, values);
193
194 mDatabase.close();
195 }catch (SQLException e){
196 Log.d(TAG,e.getMessage());
197 }
198 //}
199}
200
201}
202
20301-08 22:09:09.081 10656-10656/com.example.andrei.gymhelp E/SQLiteLog: (1)
20401-08 22:09:09.083 10656-10656/com.example.andrei.gymhelp E/SQLiteDatabase: Error inserting timestamp=10:09:04 08-01-2018 laps=Lap 1: 00:00:00:786|Lap 2: 00:00:01:283|Lap 3: 00:00:01:756|Lap 5: 00:00:02:177|
205 android.database.sqlite.SQLiteException: no such table: results_table (Sqlite code 1): , while compiling: INSERT INTO results_table(timestamp,laps) VALUES (?,?), (OS error - 2:No such file or directory)
206 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
207 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:910)
208 at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:521)
209 at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:603)
210 at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:63)
211 at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
212 at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1725)
213 at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1593)
214 at com.example.andrei.gymhelp.TimerActivity.saveResultsToDatabase(TimerActivity.java:186)
215 at com.example.andrei.gymhelp.TimerActivity$4.onClick(TimerActivity.java:150)
216 at android.view.View.performClick(View.java:5646)
217 at android.view.View$PerformClick.run(View.java:22459)
218 at android.os.Handler.handleCallback(Handler.java:761)
219 at android.os.Handler.dispatchMessage(Handler.java:98)
220 at android.os.Looper.loop(Looper.java:156)
221 at android.app.ActivityThread.main(ActivityThread.java:6523)
222 at java.lang.reflect.Method.invoke(Native Method)
223 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
224 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)