-4

Hi,

I want to create a sqlite database for use in an Android application to store data for offline use. When there is no internet connectivity my application will show some data from this sqlite DB. Any pointers, where I should begin with?

Thank you

2
  • is there you want to show some sqlite db related webpages from your application when Internet is not available?
    – user370305
    Sep 1, 2011 at 12:17
  • No i want to dsiplay data that is inside the sqlite DB.
    – Kushal
    Sep 1, 2011 at 12:29

3 Answers 3

3
public class DatabaseHelper extends SQLiteOpenHelper  {
    public DatabaseHelper(Context context) {
        super(context, "DATABASE_NAME", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE yourTable ("
                + "id INTEGER PRIMARY KEY, "
                + "name TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int o, int arg2) {
        db.execSQL("DROP TABLE IF EXISTS yourTable");
        onCreate(db);
    }

    public void insertMethod(String name) {
        SQLiteDatabase db = getWritableDatabase();
        String insertQuery = "INSERT INTO yourTable (name) VALUES(?)";
        SQLiteStatement stmt = db.compileStatement(insertQuery);
        stmt.bindString(1, name);
        stmt.execute();
        stmt.close();
        db.close();
    }

    public String getMethod() {
        String name;
        SQLiteDatabase db = getReadableDatabase();
        String selectQuery = "SELECT name FROM yourTable LIMIT 1";
        Cursor cur = db.rawQuery(selectQuery, null);
        cur.moveToFirst();

        if(cur.isAfterLast() == false)
            name= cur.getString(0);

        cur.close();
        db.close();
        return name;
    }

}
2
  • May i know how to call this class from the base class??
    – Kushal
    Sep 1, 2011 at 14:24
  • To create an instance of the database just write DatabaseHelper database = new DatabaseHelper(this); and then to use the insert and get methods database.insertMethod("String to insert"); and String text = database.getMethod();
    – drewi
    Sep 1, 2011 at 14:51
1

here is the article link for the database

http://www.vogella.de/articles/AndroidSQLite/article.html

0

you can create sqlite database dynamically or copy existing database to our workspace.

Refer this link: http://www.screaming-penguin.com/node/7742

2
  • so do we have the exe of the sqlite DB?
    – Kushal
    Sep 1, 2011 at 12:29
  • what if i don't want to create dynamically?? Any other option is available?
    – Kushal
    Sep 1, 2011 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.