Browse Source

Switch to autoincrementing _ID field

master
Chris Smith 14 years ago
parent
commit
394e859132

BIN
code/ContextAnalyser/dist/ContextAnalyser.apk View File


+ 11
- 6
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/DataHelper.java View File

@@ -29,6 +29,7 @@ import android.database.sqlite.SQLiteOpenHelper;
29 29
 import android.database.sqlite.SQLiteStatement;
30 30
 import android.location.Location;
31 31
 import android.util.Log;
32
+
32 33
 import java.util.HashMap;
33 34
 import java.util.Map;
34 35
 
@@ -40,12 +41,12 @@ public class DataHelper {
40 41
 
41 42
     private static final String LOCATIONS_TABLE = "locations";
42 43
     private static final String DATABASE_NAME = "contextapi.db";
43
-    private static final int DATABASE_VERSION = 1;
44
+    private static final int DATABASE_VERSION = 2;
44 45
 
45 46
     private static final String INSERT_LOCATION = "insert into "
46 47
       + LOCATIONS_TABLE + "(name, lat, lon) values (?, ?, ?)";
47 48
     private static final String UPDATE_LOCATION = "update "
48
-      + LOCATIONS_TABLE + " set name = ? where id = ?";
49
+      + LOCATIONS_TABLE + " set name = ? where _id = ?";
49 50
     private static final String UNNAMED_QUERY = "name LIKE '%.%,%.%'";
50 51
     private static final String LOCATION_QUERY = "lat > %1$s - 0.005 and "
51 52
             + "lat < %1$s + 0.005 and lon > %2$s - 0.01 and lon < %2$s + 0.01";
@@ -83,7 +84,7 @@ public class DataHelper {
83 84
         final Map<String, Long> results = new HashMap<String, Long>();
84 85
         
85 86
         final Cursor cursor = db.query(LOCATIONS_TABLE,
86
-                new String[] { "id", "name" },
87
+                new String[] { "_id", "name" },
87 88
                 UNNAMED_QUERY, null, null, null, null);
88 89
         
89 90
         if (cursor.moveToFirst()) {
@@ -101,7 +102,7 @@ public class DataHelper {
101 102
 
102 103
     public LocationResult findLocation(final double lat, final double lon) {
103 104
         final Cursor cursor = db.query(LOCATIONS_TABLE,
104
-                new String[] { "id", "name", "lat", "lon" },
105
+                new String[] { "_id", "name", "lat", "lon" },
105 106
                 String.format(LOCATION_QUERY, lat, lon), null, null, null, null);
106 107
 
107 108
         if (cursor.moveToFirst()) {
@@ -170,14 +171,18 @@ public class DataHelper {
170 171
         @Override
171 172
         public void onCreate(final SQLiteDatabase db) {
172 173
             db.execSQL("CREATE TABLE " + LOCATIONS_TABLE
173
-                    + " (id INTEGER PRIMARY KEY, name TEXT, lon REAL, lat REAL)");
174
+                    + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, lon REAL, lat REAL)");
174 175
         }
175 176
 
176 177
         /** {@inheritDoc} */
177 178
         @Override
178 179
         public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
179 180
                 final int newVersion) {
180
-            // Do nothing. Yet.
181
+            Log.i(getClass().getSimpleName(), "Upgrading DB " + oldVersion + "->" + newVersion);
182
+            if (oldVersion <= 1) {
183
+                db.execSQL("DROP TABLE " + LOCATIONS_TABLE);
184
+                onCreate(db);
185
+            }
181 186
         }
182 187
 
183 188
     }

Loading…
Cancel
Save