prakash .k
prakash .k

Reputation: 635

Database update in android

In my application i have an database in that i have 2 tables.I have 2 following queries:

1.Is it possible to create database with some default value?? 2.I have 1 screen in that i have 5 edit boxes,1 save button.When user press the save button the 5 edit boxes value should be updated (replace the old) in the database?

Please help me.

This is for creating database:

private static final String DATABASE_SPIN=
"create table spinner(_id integer primary key autoincrement,"+" spin text not null);";

This is exesql:

public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_SPIN);
}

My populate code is:

Cursor c=db.getAllTitlesSpin();
           startManagingCursor(c);
           int spinColumnIndex = c.getColumnIndexOrThrow("spin"); 

//          String group[]=getResources().getStringArray(R.array.group_array);

            ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item);
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s2.setAdapter(adapter1);    

            if (c.moveToFirst()) { 
                do { 
                    adapter1.add(c.getString(spinColumnIndex)); 
                } 
                while (c.moveToNext()); 
                if (db != null) { 
                db.close(); 
            } 
            }

Upvotes: 0

Views: 201

Answers (2)

Vinay W
Vinay W

Reputation: 10190

  • you can create tables with default values in columns

Each time a row is inserted into the table by an INSERT statement that does not provide explicit values for all table columns the values stored in the new row are determined by their default values, as follows:

If the default value of the column is a constant NULL, text, blob or signed-number value, then that value is used directly in the new row.

If the default value of a column is an expression in parentheses, then the expression is evaluated once for each row inserted and the results used in the new row.

If the default value of a column is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the value used in the new row is a text representation of the current UTC date and/or time. For CURRENT_TIME, the format of the value is "HH:MM:SS". For CURRENT_DATE, "YYYY-MM-DD". The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".

  • you can create and populate a table with output of a Select query (CREATE TABLE ... AS SELECT Statements )
  • You can copy the contents of one table and use it to populate another table as long the structure of the tables are same. INSERT INTO Destination SELECT * FROM Source;

Upvotes: 1

Aswin Kumar
Aswin Kumar

Reputation: 5278

  1. If you are using SQLiteOpenHelper to create the database, use the onCreate method to create the databases, and afterwards, enter the default values you want to store there using another db.execSQL() call
  2. You can update the values in sqlitedb just like in any other db - use the _id field to identify the row to update

If you are storing just the 5 values ever, SharedPreferences are a better way to do it.

Upvotes: 0

Related Questions