Tuesday, February 28, 2012
Database Helper Class for Android
[sourcecode language="java"]
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;
}
}
[/sourcecode]
Subscribe to:
Post Comments (Atom)
How to enable CORS in Laravel 5
https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...
-
< Requirements Java Development Kit (JDK) NetBeans IDE Apache Axis 2 Apache Tomcat Server Main Topics Setup Development Environ...
-
Download Sourcecode [sourcecode language="csharp"] using System; using System.Collections.Generic; using System.Linq; using System...
No comments:
Post a Comment