0

i'm new in Android. So i have created the following database...

public class database {

    private Db DbHelper;
    private Context ct;
    private SQLiteDatabase database;

    private static class Db extends SQLiteOpenHelper{

        public Db(Context context) {
            super(context, "db", null, 1);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String query; 
            query = "CREATE TABLE currency (USD, GBP, EUR, CAD)"; 
            db.execSQL(query);
        }

        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub

        }}
    public  database(Context c){
        ct = c;
    }
}

Now i want to add a row of values to the table "currency" which i'll use later in my app. I want to add them in my code like:

INSERT INTO currency
VALUES (2.1, 2.2, 2.3, 2.4);

How can i do that? Please help!

1
  • 1
    Oh come on. At least try the documentation, before you ask a question! Have a look at SQLiteDatabase.insert Commented Jul 20, 2014 at 23:40

1 Answer 1

1

In your Activity, add this to onCreate:

Db db = new Db(this);
db.execSQL("INSERT INTO currency VALUES (2.1, 2.2, 2.3, 2.4)");

Note that this works, but the Android documentation suggests using the insert Command instead, since it is easier to debug if you run into problems.

Sign up to request clarification or add additional context in comments.

1 Comment

This is true, but in general execSQL isn't really the best way to go, thus you should at least mention the right way to do it. i.e. SQLiteDatabase.insert(...)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.