Friday, August 28, 2015

Android SQLite 예제

package com.example.xxx;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BookmarkDB extends SQLiteOpenHelper {
 private final static String TAG="BookmarkDB";

 public BookmarkDB(Context context, String name, CursorFactory factory,
   int version) {
  super(context, name, factory, version);
  // TODO Auto-generated constructor stub
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
  // TODO Auto-generated method stub
  db.execSQL("CREATE TABLE bookmark (id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT, page INTEGER);");
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // TODO Auto-generated method stub

 }

 public void querySQL(String sql) {
  Log.d(TAG,"querySQL sql="+sql);
  SQLiteDatabase db = getWritableDatabase();
  db.execSQL(sql);
  db.close();
 }

 ArrayList<Integer> getPages(String path) {
  SQLiteDatabase db = getReadableDatabase();
  Cursor cursor = db.rawQuery("SELECT page FROM bookmark WHERE path='"
    + path + "'", null);

  ArrayList<Integer> pageList = new ArrayList<Integer>();
  while (cursor.moveToNext()) {
   int page = cursor.getInt(0);
   Log.d(TAG, "getPages page="+page);
   pageList.add(page);
  }
  db.close();
  return pageList;
 }

}

No comments:

Post a Comment