RSS

Database Helper Class for Android

28 Feb

package com.my.first;
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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Shader.TileMode;
import android.util.Log;

public class DBAdapter {
public static final String KEY_ROWID = "songId";
public static final String KEY_TITLE = "title";
public static final String KEY_TYPE = "type";
public static final String KEY_SIZE = "size";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "music";
private static final String DATABASE_TABLE = "songs";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table songs (songId integer primary key autoincrement, "
+ "title varchar not null, type varchar not null, "
+ "size float not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

privateĀ  static class DatabaseHelper extends SQLiteOpenHelper
{

public DatabaseHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS songs");
onCreate(db);
}



}

//---opens the database---
public DBAdapter open()
{
try
{
db = DBHelper.getWritableDatabase();

}
catch(SQLException ex)
{

}
return this;
}

//---closes the database---
public void close()
{
DBHelper.close();
}

//---insert a song into the database---
public long insertTitle(String title, String type, String size)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_TYPE, type);
initialValues.put(KEY_SIZE, size);
return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}

//---retrieves all the titles---
public Cursor getAllSongTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_TITLE,
KEY_TYPE,
KEY_SIZE},
null,
null,
null,
null,
null);
}

//---retrieves a particular title---
public Cursor getSongTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_TITLE,
KEY_TYPE,
KEY_SIZE
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}


//---updates a title---
public boolean updateSongTitle(long rowId, String title,
String type, String size)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_TYPE, type);
args.put(KEY_SIZE, size);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}

}


Advertisements
 
Leave a comment

Posted by on February 28, 2012 in Android

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: