Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.android.googleio2010bootcampcodelab;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.*;
- import android.util.Log;
- import java.util.HashMap;
- import java.util.LinkedList;
- public class DataHelper {
- private static final String DATABASE_NAME = "searchterms.sqlite";
- private static final int DATABASE_VERSION = 1;
- private static final String TABLE_NAME = "terms";
- static final String QUERY_TEXT = "QT";
- static final String QUERY_DATE = "QD";
- private Context context;
- private SQLiteDatabase db;
- private SQLiteStatement insertStmt;
- private static final String INSERT = "insert into "
- + TABLE_NAME + "(term, date) values (?, ?)";
- public DataHelper(Context context) {
- this.context = context;
- OpenHelper openHelper = new OpenHelper(this.context);
- try {
- this.db = openHelper.getWritableDatabase();
- this.insertStmt = this.db.compileStatement(INSERT);
- }catch(Exception e) {
- Log.e("CollectData", "misfunctioning open" + e.toString());
- }
- }
- public long insert(String term, String date) {
- this.insertStmt.bindString(1, term);
- this.insertStmt.bindString(2, date);
- return this.insertStmt.executeInsert();
- }
- public void deleteAll() {
- this.db.delete(TABLE_NAME, null, null);
- }
- public LinkedList<HashMap<String, String>> selectAll() {
- LinkedList<HashMap<String, String>> list = new LinkedList<HashMap<String, String>>();
- HashMap<String, String> map;
- Cursor cursor = this.db.query(TABLE_NAME, new String[] { "term", "date" },
- null, null, null, null, "id desc");
- if (cursor.moveToFirst()) {
- do {
- map = new HashMap<String, String>();
- map.put(QUERY_TEXT, cursor.getString(0));
- map.put(QUERY_DATE, cursor.getString(1));
- list.add(map);
- } while (cursor.moveToNext());
- }
- if (cursor != null && !cursor.isClosed()) {
- cursor.close();
- }
- return list;
- }
- private static class OpenHelper extends SQLiteOpenHelper {
- OpenHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db){
- Log.w("CollectData", "Create database.");
- db.execSQL("CREATE TABLE " + TABLE_NAME + " " +
- "(id INTEGER PRIMARY KEY, term text, date text);");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w("CollectData", "Upgrading database, this will drop tables and recreate.");
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
- onCreate(db);
- }
- }
- }
Add Comment
Please, Sign In to add comment