-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBAdapter.java
More file actions
executable file
·82 lines (67 loc) · 2.28 KB
/
DBAdapter.java
File metadata and controls
executable file
·82 lines (67 loc) · 2.28 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
package net.learn2develop.lbsreceiver_datalogging;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
static final String KEY_DATE = "date";
static final String KEY_LAT = "lat";
static final String KEY_LNG = "lng";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "MyDB";
static final String DATABASE_TABLE = "Locations";
static final int DATABASE_VERSION = 1;
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
Calendar currentDate;
SimpleDateFormat formatter;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
currentDate = Calendar.getInstance();
formatter = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a location into the database---
public long insertLocation(String lat, String lng)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, formatter.format(currentDate.getTime()));
initialValues.put(KEY_LAT, lat);
initialValues.put(KEY_LNG, lng);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---retrieves all the locations---
public Cursor getAllLocations()
{
return db.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_LAT,
KEY_LNG}, null, null, null, null, null);
}
}