title: Not able to insert values for SQLIte in android
body: <p><em>I am using a simple demo program, I am creating Database, then a table and trying to add values to the table</em></p>
  
  <p><strong>What i am able to do</strong>::
  I am able to create table in sqlite</p>
  
  <p><strong>What i am not able to do</strong>::
  I am not able to insert values values into it</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity{
  
      Button addUser;
      SqliteAdapter helper;
      EditText usrName,pwd;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          helper=new SqliteAdapter(this);
          addUser=(Button) findViewById(R.id.button1);
          usrName=(EditText) findViewById(R.id.editText1);
          pwd=(EditText)findViewById(R.id.editText2);
  
      }
  
  
      public void addUser(View view){
          Log.d("LOG-MSG", "Onclick detected");
          helper.insertData(usrName.getText().toString(), pwd.getText().toString());
      }
  }
  </code></pre>
  
  <p><strong>SqliteAdapter.java</strong></p>
  
  <pre><code>public class SqliteAdapter{
  
      SqliteHelper helper;
      ContentValues cv;
      SQLiteDatabase db;
      public SqliteAdapter(Context context){
          helper=new SqliteHelper(context);
      }
  
      public void insertData(String name,String password){
  
          db=helper.getWritableDatabase();
          Log.d("LOG-MSG", "insertDataMethod Entry");
          try {
              cv.put(SqliteHelper.NAME, name);
              Log.d("LOG-MSG", "Name inserted");
              cv.put(SqliteHelper.PASSWORD, password);
              Log.d("LOG-MSG", "password inserted");
          } catch (Exception e) {
              // TODO Auto-generated catch block
              Log.d("LOG-MSG-EXCEPTION", e.toString());
          }
          db.insert(SqliteHelper.TABLE_NAME, null, cv);
      }
  
      static class SqliteHelper extends SQLiteOpenHelper{
  
          private Context context;
          private static final String DATABASE_NAME="MyDatabase";
          private final static String TABLE_NAME="MyTable";
          private static final int DATABASE_VERSION=1;
          private final static String ID="_id";
          private final static String NAME="name";
          private final static String PASSWORD="password";
          private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
          private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME+"";
  
          public SqliteHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
              this.context=context;
              Log.d("LOG-MSG", "Constructor Called");
              Message.message(context, "Constructor Called");
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              Log.d("LOG-MSG", "OnCreate Called");
              Message.message(context, "OnCreate Called");
              try {
                  db.execSQL(CREATE_TABLE);
              } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  Log.d("LOG-MSG-onCreate", e.toString());
              }
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              try {
                  db.execSQL(DROP_TABLE);
              } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  Log.d("LOG-MSG-onUpgrade", e.toString());
                  Message.message(context, "onUpgrade Called");
              }
              onCreate(db);
          }
      }   
  }
  </code></pre>
  
  <p><strong>Log</strong>::</p>
  
  <pre><code>04-18 13:45:19.567: D/LOG-MSG(3371): Constructor Called
  04-18 13:45:20.007: D/gralloc_goldfish(3371): Emulator without GPU emulation detected.
  04-18 13:45:20.107: W/TextLayoutCache(3371): computeValuesWithHarfbuzz -- need to force to single run
  04-18 13:46:22.747: D/LOG-MSG(3371): Onclick detected
  04-18 13:46:22.809: D/LOG-MSG(3371): insertDataMethod Entry
  04-18 13:46:22.809: D/LOG-MSG-EXCEPTION(3371): java.lang.NullPointerException
  04-18 13:46:22.817: I/SqliteDatabaseCpp(3371): sqlite returned: error code = 1, msg = near "null": syntax error, db=/data/data/com.example.sqlitedatabasedemo/databases/MyDatabase
  </code></pre>
  
  <hr>
  
  <h2>{After EDIT}</h2>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity{
  
      Button addUser;
      SqliteAdapter helper;
      EditText usrName,pwd;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          helper=new SqliteAdapter(this);
          addUser=(Button) findViewById(R.id.button1);
          usrName=(EditText) findViewById(R.id.editText1);
          pwd=(EditText)findViewById(R.id.editText2); 
      }
  
  
      public void addUser(View view){
          Log.d("LOG-MSG", "Onclick detected");
          long recievedValue=helper.insertData(usrName.getText().toString(), pwd.getText().toString());
          if(recievedValue&lt;0){
              Log.d("LOG-MSG", "NotInserted");
          }else
          {
              Log.d("LOG-MSG", "Inserted");           
          }
      }
  }
  </code></pre>
  
  <p><strong>SqliteAdapter.java</strong></p>
  
  <pre><code>public class SqliteAdapter{
  
      SqliteHelper helper;
      ContentValues cv;
      SQLiteDatabase db;
      public SqliteAdapter(Context context){
          helper=new SqliteHelper(context);
      }
  
      public long insertData(String name,String password){
  
          db=helper.getWritableDatabase();
          Log.d("LOG-MSG", "insertDataMethod Entry");
          cv=new ContentValues();
          cv.put(SqliteHelper.NAME, name);
          Log.d("LOG-MSG", "Name="+name+"inserted");
          cv.put(SqliteHelper.PASSWORD, password);
          Log.d("LOG-MSG", "password="+password+"inserted");
          long result=db.insert(SqliteHelper.TABLE_NAME, null, cv);
          return result;
      }
  
      static class SqliteHelper extends SQLiteOpenHelper{
  
          private Context context;
          private static final String DATABASE_NAME="MyDatabase";
          private final static String TABLE_NAME="MyTable";
          private static final int DATABASE_VERSION=1;
          private final static String ID="_id";
          private final static String NAME="name";
          private final static String PASSWORD="password";
          private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
          private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME+"";
  
          public SqliteHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
              this.context=context;
              Log.d("LOG-MSG", "Constructor Called");
              Message.message(context, "Constructor Called");
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              Log.d("LOG-MSG", "OnCreate Called");
              Message.message(context, "OnCreate Called");
              db.execSQL(CREATE_TABLE);
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              db.execSQL(DROP_TABLE);
              onCreate(db);
          }
      }   
  }
  </code></pre>
  
  <p><strong>Log</strong>::</p>
  
  <pre><code>04-18 14:21:59.937: D/LOG-MSG(13661): Onclick detected
  04-18 14:22:00.337: D/LOG-MSG(13661): insertDataMethod Entry
  04-18 14:22:00.337: D/LOG-MSG(13661): Name=usainserted
  04-18 14:22:00.337: D/LOG-MSG(13661): password=obamainserted
  04-18 14:22:00.377: D/LOG-MSG(13661): Inserted
  </code></pre>
  
  <p><strong>problem:</strong>: When i check the DDMS ....i can see the database. when i see it in query browser there are no values inserted .... why is this happening ... ive also tried to debug possible log statements</p>