PageRenderTime 26ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/there/src/com/google/marvin/there/DbManager.java

http://eyes-free.googlecode.com/
Java | 78 lines | 53 code | 15 blank | 10 comment | 2 complexity | 02f18836ab69ec2ec8b4dcaee61df0d9 MD5 | raw file
 1package com.google.marvin.there;
 2
 3import java.util.ArrayList;
 4
 5import android.content.ContentValues;
 6import android.content.Context;
 7import android.database.Cursor;
 8import android.database.sqlite.SQLiteDatabase;
 9
10public class DbManager {
11  private final String DATABASE_NAME = "MARVIN_THERE";
12  private final String DATABASE_TABLE = "LOCATIONS";
13
14  SQLiteDatabase db;
15  Context parent;
16
17  public DbManager(Context ctx) {
18    parent = ctx;
19    db = parent.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
20    /* Create a Table in the Database. */
21    
22    // Uncomment this line to drop the table and reset it - dev purposes only
23    //db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
24    
25    db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE
26        + "(_id integer primary key autoincrement, Name VARCHAR, Desc VARCHAR,"
27        + " Lat VARCHAR, Lon VARCHAR);");
28  }
29
30
31  public void put(Place place) {
32    /*
33     * db.execSQL("INSERT INTO " + DATABASE_TABLE + " (Name, Desc, Lat, Lon)" +
34     * " VALUES ('" + place.name + "', '" + place.desc + "', '" + place.lat +
35     * "', '" + place.lon + "');");
36     */
37
38    ContentValues placeValues = new ContentValues();
39    placeValues.put("Name", place.name);
40    placeValues.put("Desc", place.desc);
41    placeValues.put("Lat", place.lat);
42    placeValues.put("Lon", place.lon);
43    db.insert(DATABASE_TABLE, null, placeValues);
44  }
45
46  public Place get(String name) {
47    Place answer = null;
48    Cursor c =
49        db.query(DATABASE_TABLE, null, "Name = '" + name + "';", null, null, null, null, null);
50    if (c.moveToFirst()) {
51      // 0th position is the ID
52      answer = new Place(c.getString(1), c.getString(2), c.getString(3), c.getString(4));
53    }
54    return answer;
55  }
56
57  public void delete(String name) {
58    db.delete(DATABASE_TABLE, "Name = '" + name + "';", null);
59  }
60
61  public ArrayList<Place> toArrayList() {
62    ArrayList<Place> list = new ArrayList<Place>();
63    Cursor c = db.query(DATABASE_TABLE, null, null, null, null, null, null, null);
64    while (c.moveToFirst()) {
65      // 0th position is the ID
66      list.add(new Place(c.getString(1), c.getString(2), c.getString(3), c.getString(4)));
67    }
68    return list;
69  }
70
71  public Cursor getCursor() {
72    Cursor c =
73        db.query(true, DATABASE_TABLE, null, null, null,
74            null, null, null, null);
75    return c;
76  }
77
78}