sqlite example program in android [closed]
I am Newbee to Database concepts specially i need relative database concepts..I want an example working that uses sqlite database for android..i walked through android developer site but found very little stuff regarding database examples.. As i am new to database concepts Can anyone suggest me some good resources for understanding create(),insert(),Query()...etc..and it would be better if i get any sample program working with android..
Thanks in advance
it's very simple follow the code below... Create 3 class named for example - 1. i used Constants interface,refer full code and find it's use
package com.mypackage.quaddeals;
import android.net.Uri;
import android.provider.BaseColumns;
public interface Constants extends BaseColumns {
public static final String TABLE_NAME = "user_deal";
public static final String AUTHORITY = "com.mypackage.quaddeals";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/" + TABLE_NAME);
public static final String TITLE = "title";
public static final String CITYID = "cityid";
}
2.BruPress data class...
package com.mypackage.quaddeals;
import static android.provider.BaseColumns._ID;
import static com.mypackage.quaddeals.Constants.CITYID;
import static com.mypackage.quaddeals.Constants.TITLE;
import static com.mypackage.quaddeals.Constants.TABLE_NAME;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Bru_Press_Data extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "QuadDeals.db";
public static final int DATABASE_VERSION = 1;
/** Create a helper object for the Events database */
public Bru_Press_Data(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," + TITLE
+ " TEXT UNIQUE," + CITYID + " TEXT);");
} catch (Exception e) {
Log.v("Bru_Press_Data", "exception in table created");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
3.Provider class
package com.mypackage.quaddeals;
import static android.provider.BaseColumns._ID;
import static com.mypackage.quaddeals.Constants.CONTENT_URI;
import static com.mypackage.quaddeals.Constants.AUTHORITY;
import static com.mypackage.quaddeals.Constants.TABLE_NAME;
import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
public class Bru_Press_Provider extends ContentProvider {
private static final int QUADUSER = 1;
private static final int QUADUSER_ID = 2;
/** The MIME type of a directory of events */
private static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.example.brown";
/** The MIME type of a single event */
private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.example.brown";
private static final String CONTENT_TYPE1 = "vnd.android.cursor.dir/vnd.example.brown";
/** The MIME type of a single event */
private static final String CONTENT_ITEM_TYPE1 = "vnd.android.cursor.item/vnd.example.brown";
// private static final String CONTENT_TYPE2 = "vnd.android.cursor.dir/vnd.example.brown";
/** The MIME type of a single event */
// private static final String CONTENT_ITEM_TYPE2 = "vnd.android.cursor.item/vnd.example.brown";
private Bru_Press_Data news;
private UriMatcher uriMatcher;
private Cursor cursor;
private SQLiteDatabase db;
String[] str;
@Override
public boolean onCreate() {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "user_deal", QUADUSER);
uriMatcher.addURI(AUTHORITY, "user_deal/#", QUADUSER_ID);
news = new Bru_Press_Data(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String orderBy) {
SQLiteDatabase db;
Cursor cursor = null;
if (uriMatcher.match(uri) == QUADUSER_ID) {
long id = Long.parseLong(uri.getPathSegments().get(1));
selection = appendRowId(selection, id);
}
if (uriMatcher.match(uri) == QUADUSER) {
// Get the database and run the query
db = news.getReadableDatabase();
cursor = db.query(TABLE_NAME, projection, selection, selectionArgs,
null, null, orderBy);
}
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case QUADUSER:
return CONTENT_TYPE;
case QUADUSER_ID:
return CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = news.getWritableDatabase();
Uri newUri = null;
long id = -1;
// Validate the requested uri
if (uriMatcher.match(uri) == QUADUSER) {
try {
id = db.insertOrThrow(TABLE_NAME, null, values);
// Notify any watchers of the change
newUri = ContentUris.withAppendedId(CONTENT_URI, id);
getContext().getContentResolver().notifyChange(newUri, null);
} catch (NullPointerException e) {
Log.v("Error","OnInsert"+e);
}
}
else if (uriMatcher.match(uri) != QUADUSER) {
throw new IllegalArgumentException("Unknown URI bru press " + uri);
}
return newUri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = news.getWritableDatabase();
int count;
long id;
Log.v("Delete",selection);
switch (uriMatcher.match(uri)) {
case QUADUSER:
count = db.delete(TABLE_NAME, selection, selectionArgs);
break;
case QUADUSER_ID:
id = Long.parseLong(uri.getPathSegments().get(1));
count = db.delete(TABLE_NAME, appendRowId(selection, id),
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = news.getWritableDatabase();
int count;
long id;
switch (uriMatcher.match(uri)) {
case QUADUSER:
count = db.update(TABLE_NAME, values, selection, selectionArgs);
break;
case QUADUSER_ID:
id = Long.parseLong(uri.getPathSegments().get(1));
count = db.update(TABLE_NAME, values, appendRowId(selection, id),
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
// Notify any watchers of the change
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
/** Append an id test to a SQL selection expression */
private String appendRowId(String selection, long id) {
return _ID
+ "="
+ id
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
: "");
}
}
Note: you suppose to define this Provider class in Manifest file like .
In another class,where i can insert and retrieve the data to DB...has follows
class A extends Activity{
{
addevent(cityname,cityid); // to insert
}
private void addevent(String a1, String a2) {
try {
ContentValues values = new ContentValues();
values.put(Constants.TITLE, a1);
values.put(Constants.CITYID, a2);
getContentResolver().insert(Constants.CONTENT_URI, values);
} catch (Exception e) {
Log.v("Error",""+e);
}
}
to retrieve from Db
Cursor c = getContentResolver().query(Constants.CONTENT_URI,
null, null, null,null);
int a = c.getCount();
Log.v("Count",""+a);
int columnIndex = c.getColumnIndex(Constants.TITLE);
int columnIndex2 = c.getColumnIndex(Constants.CITYID);
if (c.moveToNext()) {
cityName = c.getString(columnIndex);
city_id= c.getString(columnIndex2);
}
}
try some thing like this...it will help you