Skip to main content

Posts

Showing posts with the label sqlite

SQLiteManager plugin for Eclipse

See more basics on Android along with interview questions How can we access the database?i will give solution to all these problems. I created the database from my previous post about Using SQLite in ANDROID You can see the sqlite database in eclipse by opening File Explorer .Then /data/data/package_name/databases But here we cannot see the tables and table data . For viewing the table details Eclipse has a plugin. You can download the jar from below.           download jar Now put the jar in the folder eclipse/dropins/ and restart the eclipse and now you can see the sqlitemanager plugin on the top right of the File Explorer window reference link : http://www.coderzheaven.com/2011/04/18/sqlitemanager-plugin-for-eclipse/

A complete reference to Database Helper Class in SQLite in ANDROID

For COMPLETE TUTORIALS see here : DABASEHELPER CLASS : 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.SQLiteOpenHelper; public class DBAdapter { public static final String KEY_ROWID = "id"; public static final String KEY_NAME = "title"; public static final String KEY_NICKNAME = "duedate"; private static final String DATABASE_NAME = "assinDB.db"; private static final String DATABASE_TABLE = "assignments"; private static final int DATABASE_VERSION = 2; private final Context context; private DatabaseHelper ObjectDBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { context = ctx; } private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context con...

Get a single record from SQLite in ANDROID

For COMPLETE TUTORIALS see here : in DATABASE HELPER class: String[] coloumns = new String[] { KEY_ROWID, KEY_NAME, KEY_NICKNAME }; // calling elements in an array Cursor c = db.query(DATABASE_TABLE, coloumns, KEY_ROWID + "=" + l,null, null, null, null); if (c != null) { c.moveToFirst(); String name = c.getString(1); // since name is in position 1 ie second coloumn return name; } return null; in sqlite.java : String s =editinfo.getText().toString(); long l=Long.parseLong(s); DBAdapter adapter=new DBAdapter(SQliteExample.this); adapter.open(); String returnedname=adapter.returnName(l); String returnednickname=adapter.returnickname(l); adapter.close(); editName.setText(returnedname); editNickName.setText(returnednickname);

Get all records from SQLite in ANDROID

For COMPLETE TUTORIALS see here : in DABASEHELPER CLASS : String[] coloumns = new String[] { KEY_ROWID, KEY_NAME,KEY_NICKNAME }; // calling all elements in an array Cursor c = db.query(DATABASE_TABLE, coloumns, null, null, null, null,null);// basically reading a database need cursor String result = " ";// since string is to be returned System.out.println("count of cursor=====>>" + c.getCount()); int iRowid = c.getColumnIndex(KEY_ROWID); // calling each row values int iRowName = c.getColumnIndex(KEY_NAME); int iRowNickName = c.getColumnIndex(KEY_NICKNAME); if (c.moveToFirst()) { c.moveToFirst(); for (int i = 0; i " + iRowid + " "+ iRowName); c.moveToNext(); } } c.close(); db.close(); return result; in sqlite.java : TextView infotext=(TextView) findViewById(R.id.tvsqlinfo); DBAdapter info = new DBAdapter(this); ...

Modify records in SQLite in ANDROID

For COMPLETE TUTORIALS see here : in DABASEHELPER CLASS : ContentValues cvupdate = new ContentValues(); // Bundle for writting the database fields cvupdate.put(KEY_NAME, namestr);// put the values passed to ContentValues cvupdate.put(KEY_NICKNAME, nickstr);// put the values passed to ContentValues db.update(DATABASE_TABLE, cvupdate, KEY_ROWID + "=" + smodify, null);// specify where to be changed in sqlite.java : String Namestr = editName.getText().toString(); String Nickstr = editNickName.getText().toString(); String smodify =editinfo.getText().toString(); long lmodify =Long.parseLong(smodify); DBAdapter adaptmodify =new DBAdapter(SQliteExample.this); adaptmodify.open(); adaptmodify.updateentry(lmodify,Namestr,Nickstr); // pass index and new values to be inserted adaptmodify.close();

Delete records from SQLite table in ANDROID

For COMPLETE TUTORIALS see here : in DABASEHELPER CLASS : public void deleteentry(long ldelete) { // TODO Auto-generated method stub db.delete(DATABASE_TABLE, KEY_ROWID + "=" +ldelete, null); } in Sqlite.java : String sdelete =editinfo.getText().toString(); long ldelete =Long.parseLong(sdelete); DBAdapter adaptdelete =new DBAdapter(SQliteExample.this); adaptdelete.open(); adaptdelete.deleteentry(ldelete); //pass the index to be deleted adaptdelete.close();

Insert records in SQLite tables in ANDROID

For COMPLETE TUTORIAL see here : in DABASEHELPER CLASS : public void entryfield(String namestr, String nickstr) { ContentValues cv = new ContentValues(); // Bundle for writting the database fields cv.put(KEY_NAME, namestr);// put the values passed to ContentValues cv.put(KEY_NICKNAME, nickstr);// put the values passed to ContentValues db.insert(DATABASE_TABLE, null, cv);// insert into databse with vlues as "content vales" } in sqlite.java //==Create an object of adapter class to call methods== DBAdapter Entryadapter = new DBAdapter(SQliteExample.this); Entryadapter.open(); Entryadapter.entryfield(Namestr,Nickstr); // Calling the function in DBAdapter Class Entryadapter.close();

Simple SQLite Example in Android

See more basics on Android along with interview questions DBAdapter.java (DABASEHELPER CLASS) 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.SQLiteOpenHelper; public class DBAdapter { public static final String KEY_ROWID = "id"; public static final String KEY_NAME = "title"; public static final String KEY_NICKNAME = "duedate"; private static final String DATABASE_NAME = "assinDB.db"; private static final String DATABASE_TABLE = "assignments"; private static final int DATABASE_VERSION = 2; private final Context context; private DatabaseHelper ObjectDBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { context = ctx; } private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context ...