title: Android saving data from edit texts fields to database
body: <p>I am new to SQLite. I have an activity in which I have different fields like Name,Email,Date of Birth etc. When user fill the information and clicks the save button I want it to get saved to the database. But I am stuck in mid way and don't know what to do.How to save the data from Edit Text Views to the database on Save Button click.Please help me.
  I am sharing my code.</p>
  
  <p>DataBaseHelper.java</p>
  
  <pre><code>public class DatabaseHelper extends SQLiteOpenHelper {
  
      private static final int DATABASE_VERSION = 1;
      private static final String DATABASE_NAME = "NewUserDb.db";
      private static final String TABLE_INFO = "info";
  
      public static final String COLUMN_ID = "_id";
      public static final String COLUMN_GOAL = "userGOAL";
      public static final String COLUMN_NAME = "userName";
      public static final String COLUMN_EMAIL = "userEmail";
      public static final String COLUMN_DOB = "userDOB";
      public static final String COLUMN_HEIGHT = "userHeight";
      public static final String COLUMN_WEIGHT = "userWeight";
      public static final String COLUMN_GENGER = "userGender";;
      public static final String COLUMN_ZIP = "userZIP";
  
      private static final String[] COLUMNS = { COLUMN_ID, COLUMN_GOAL,
              COLUMN_NAME, COLUMN_EMAIL, COLUMN_DOB, COLUMN_HEIGHT,
              COLUMN_WEIGHT, COLUMN_GENGER, COLUMN_ZIP };
  
      // http://www.techotopia.com/index.php/An_Android_SQLite_Database_Tutorial
  
      /*
       * public DatabaseHelper(Context context, String name, CursorFactory
       * factory, int version) { super(context, name, factory, version); // TODO
       * Auto-generated constructor stub
       * 
       * }
       */
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
  
          String CREATE_USER_TABLE = "CREATE TABLE User ( "
                  + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "gaol TEXT, "
                  + "name TEXT )";
  
          db.execSQL(CREATE_USER_TABLE);
  
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub
          db.execSQL("DROP TABLE IF EXISTS users");
  
          // create fresh books table
          this.onCreate(db);
      }
  
      public DatabaseHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      public void addUser(NewUserDb newuserdb) {
          // for logging
          // Log.d("addBook", book.toString());
  
          // 1. get reference to writable DB
          SQLiteDatabase db = this.getWritableDatabase();
  
          // 2. create ContentValues to add key "column"/value
          ContentValues values = new ContentValues();
          values.put(COLUMN_GOAL, newuserdb.getGoal()); // get title
          values.put(COLUMN_NAME, newuserdb.getName()); // get author
          values.put(COLUMN_EMAIL, newuserdb.getEmail());
          values.put(COLUMN_DOB, newuserdb.getDob());
          values.put(COLUMN_HEIGHT, newuserdb.getHeight());
          values.put(COLUMN_WEIGHT, newuserdb.getWeight());
          values.put(COLUMN_GENGER, newuserdb.getGender());
          values.put(COLUMN_ZIP, newuserdb.getZip());
  
          // 3. insert
          db.insert(TABLE_INFO, // table
                  null, // nullColumnHack
                  values); // key/value -&gt; keys = column names/ values = column
                              // values
  
          // 4. close
          db.close();
      }
  
      public NewUserDb getNewUserDb(int id) {
  
          // 1. get reference to readable DB
          SQLiteDatabase db = this.getReadableDatabase();
  
          // 2. build query
          Cursor cursor = db.query(TABLE_INFO, // a. table
                  COLUMNS, // b. column names
                  " id = ?", // c. selections
                  new String[] { String.valueOf(id) }, // d. selections args
                  null, // e. group by
                  null, // f. having
                  null, // g. order by
                  null); // h. limit
  
          // 3. if we got results get the first one
          if (cursor != null)
              cursor.moveToFirst();
  
          // 4. build book object
          NewUserDb newUserdb = new NewUserDb();
          newUserdb.setId(Integer.parseInt(cursor.getString(0)));
          newUserdb.setGoal(cursor.getString(1));
          newUserdb.setName(cursor.getString(2));
          newUserdb.setEmail(cursor.getString(3));
          newUserdb.setDob(cursor.getString(4));
          newUserdb.setHeight(cursor.getString(5));
          newUserdb.setWeight(cursor.getString(6));
          newUserdb.setGender(cursor.getString(7));
          newUserdb.setZip(cursor.getString(8));
  
          // Log.d("getBook("+id+")", book.toString());
  
          // 5. return book
          return newUserdb;
      }
  
      // Deleting single book
      public void deleteUser(NewUserDb newuserDB) {
  
          // 1. get reference to writable DB
          SQLiteDatabase db = this.getWritableDatabase();
  
          // 2. delete
          db.delete(TABLE_INFO, COLUMN_ID + " = ?",
                  new String[] { String.valueOf(newuserDB.getId()) });
  
          // 3. close
          db.close();
  
          Log.d("deleteBook", newuserDB.toString());
  
      }
  
  }
  </code></pre>
  
  <p>NewUserDb.java</p>
  
  <pre><code>public class NewUserDb {
  
      private int id;
      private String goal;
      private String name;
      private String email;
      private String dob;
      private String height;
      private String weight;
      private String gender;
      private String zip;
  
      public NewUserDb() {
      }
  
      public NewUserDb(int id, String goal, String name, String email,
              String dob, String height, String weight, String gender, String zip) {
          super();
          this.id = id;
          this.goal = goal;
          this.name = name;
          this.email = email;
          this.dob = dob;
          this.height = height;
          this.weight = weight;
          this.gender = gender;
          this.zip = zip;
      };
  
  
  
  
  
  
      public int getId() {
  
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getGoal() {
          return goal;
      }
  
      public void setGoal(String goal) {
          this.goal = goal;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getDob() {
          return dob;
      }
  
      public void setDob(String dob) {
          this.dob = dob;
      }
  
      public String getHeight() {
          return height;
      }
  
      public void setHeight(String height) {
          this.height = height;
      }
  
      public String getWeight() {
          return weight;
      }
  
      public void setWeight(String weight) {
          this.weight = weight;
      }
  
      public String getGender() {
          return gender;
      }
  
      public void setGender(String gender) {
          this.gender = gender;
      }
  
      public String getZip() {
          return zip;
      }
  
      public void setZip(String zip) {
          this.zip = zip;
      }
  
      public String toString() {
          return "User [id=" + id + ", goal=" + goal + ", name=" + name
                  + ", email=" + email + ", dob=" + dob + ", height=" + height
                  + ", weight=" + weight + ", gender=" + gender + ", zip =" + zip
  
                  + "]";
  
      }
  
  }
  </code></pre>
  
  <p>AccountActivity.java</p>
  
  <pre><code>public class AccountActivity extends Activity {
      private EditText et_weight, et_height, et_email, et_dob, et_name;
      private Button btn_uploadPhoto, btn_shootPhoto, btn_del, btn_save;
      private Switch genderSwitch;
      private DatePickerDialog datePickerDialog;
      private String year;
      private String month;
      private String day;
  
      DatabaseHelper dbh = new DatabaseHelper(this);
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.accounts);
  
          btn_shootPhoto = (Button) findViewById(R.id.button1);
          btn_uploadPhoto = (Button) findViewById(R.id.button2);
          btn_del = (Button) findViewById(R.id.btn_del);
          btn_save = (Button) findViewById(R.id.btn_save);
          et_dob = (EditText) findViewById(R.id.et_dob);
          genderSwitch = (Switch) findViewById(R.id.mySwitch);
          et_name = (EditText) findViewById(R.id.et_name);
          et_email = (EditText) findViewById(R.id.et_email);
          et_height = (EditText) findViewById(R.id.et_height);
          et_height.setInputType(InputType.TYPE_CLASS_NUMBER);
          et_weight = (EditText) findViewById(R.id.et_weight);
          et_weight.setInputType(InputType.TYPE_CLASS_NUMBER);
  
          btn_save.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
  
                        //what to do here ??
              }
          });
  }
  </code></pre>