Daksh
Daksh

Reputation: 1177

Android SQLite Default Value

I want to make the user enter a String, say "TEST" while defining the columns of a table to be created.

Let the column name be "standard"

The user should never have to enter "TEST" again while inserting rows into the created Table.

so will this parameter do the job :

create table tablename( ..... , standard text default \'TEST\');

?

Please let me know, as I searched a while and could not get my doubt cleared

Upvotes: 0

Views: 8349

Answers (1)

QArea
QArea

Reputation: 4981

Why do you use \' \' in creation string?

Following works:

private static final String CREATE_TABLE_CONTACT = CREATE_TABLE + CONTACT.TABLE_NAME + " (" + CONTACT._ID+ " INTEGER PRIMARY KEY," + CONTACT.FIRST_NAME + " TEXT, " + CONTACT.LAST_NAME + " TEXT DEFAULT 'TEST');";

Here is full example of DBHelper:

public class DBHelper extends SQLiteOpenHelper {


public static interface CONTACT extends BaseColumns {
                String TABLE_NAME = "human";
                String FIRST_NAME = "first_name";
                String LAST_NAME = "last_name";
            }

  private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
            private static final String DATABASE_NAME = "someDataBase";
            private static final int DATABASE_VERSION = 1;


  private static final String CREATE_TABLE_CONTACT = CREATE_TABLE + CONTACT.TABLE_NAME + " (" + CONTACT._ID
                    + " INTEGER PRIMARY KEY," + CONTACT.FIRST_NAME + " TEXT, " + CONTACT.LAST_NAME
                    + " TEXT DEFAULT 'TTEESSTT');"; 

 public DBHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

   @Override
            public void onCreate(SQLiteDatabase db) {
                db.execSQL(CREATE_TABLE_CONTACT);
            }

@Override
            public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
                db.execSQL("DROP TABLE IF EXISTS " + CONTACT.TABLE_NAME);
                onCreate(db);
}

public Cursor getAllContacts() {
                return getReadableDatabase().query(CONTACT.TABLE_NAME, null, null, null, null, null, null);
            }


//method to insert contact
            public void insertContact(String firstName, String lastName) {
                final ContentValues values = new ContentValues();
                values.put(CONTACT.FIRST_NAME, firstName);
                //optional parameter 
                if (lastName != null) {
                    values.put(CONTACT.LAST_NAME, lastName);
                }
                getWritableDatabase().insert(CONTACT.TABLE_NAME, null, values);
            }
        }

And its usage, for example:

DBHelper db = new DBHelper(this);
            db.insertContact("Dmitry", "Antonov");
            db.insertContact("Andy", "Sudorov");
            db.insertContact("Oleg", null);
            db.insertContact("Ivan", "Lozin");
            db.insertContact("Serg", null);
            Cursor allContacts = db.getAllContacts();
            ListView testList = (ListView) findViewById(R.id.testList);
            testList.setAdapter(new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, allContacts,
                    new String[] { DBHelper.CONTACT.FIRST_NAME, DBHelper.CONTACT.LAST_NAME }, new int[] {android.R.id.text1, android.R.id.te

Upvotes: 4

Related Questions