2

Possible Duplicate:
how to create database in android

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.

Community
  • 1
  • 1
Alex
  • 1,241
  • 3
  • 12
  • 15

2 Answers2

1

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
  • 10,073
  • 4
  • 31
  • 45
0

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

Ryan Conrad
  • 6,870
  • 2
  • 36
  • 36