views:

148

answers:

4

I am new to mobile application development.I wish to know how to create and use the database in android. Is there any requirements needed to create the database(like sql)? now i use the eclipse ide.

+1  A: 

There's a good start here: http://www.devx.com/wireless/Article/40842/1954

Andir
you had me by 23 seconds ;) but I didn't just give him a link... get to work already.
Ryan Conrad
Haha, I got you cause you were editing his post! (And I'm getting ready for work now...see you in a few!)
Andir
A: 

Android uses SQLite for its database engine. There are some "helper" classes in the SDK for working with the database.

Ryan Conrad
+1  A: 

One of the approaches is to extend SQLiteOpenHelper as below and override onCreate to create the database. Below is a simple example.

public class DBHelper extends SQLiteOpenHelper {

  private static String DB_NAME = "example.db";
  private static int DB_VERSION = 1;

  public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   db.execSQL("CREATE TABLE props (name TEXT PRIMARY KEY, value TEXT);");
INTEGER);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO: Write upgrade db scripts

  }
}

And then you may do a query like

SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables("PROPS");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = qb.query(db, new String[]{"value"}, "name = '" + name +"'", null, null, null, null);
if(c.getCount() > 0) {
  c.moveToFirst();
  val = c.getString(c.getColumnIndexOrThrow("value"));
}
closeDbAndCursor(db,c);
Gopi
A: 

Follow this Link

Karthick