title: How to write a SQLite execSQL function in Android?
body: <p>I'm developing an application where I need to retrieve some data and to store in SQLite database. I'm using <code>SQLiteOpenHelper</code>. Here <code>databaseHandler.addDetails(obj);</code> renders error message. Any help would be appreciated. here my codes.</p>
  
  <p><strong>Register.java</strong></p>
  
  <pre><code>public class Register extends Activity{
  
  //  variables for all text data.
      String FIRSTNAME = null;
      String LASTNAME = null;
      String USERNAME = null;
      String PASSWORD = null;
      String CITY= null;
  
  //  variables for individual ID s .
      EditText fnameText = null;
      EditText lnameText = null;
      EditText unameText = null;
      EditText passText = null;
      EditText cityText = null;
      Button redg = null;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.register);
  
  
          fnameText = (EditText)findViewById(R.id.editText1);
          lnameText = (EditText)findViewById(R.id.editText2);
          unameText = (EditText)findViewById(R.id.editText3);
          passText = (EditText)findViewById(R.id.editText4);
          cityText = (EditText)findViewById(R.id.editText5);
          redg = (Button)findViewById(R.id.button1);
  
  //      button click event
          redg.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  System.out.println("On click event of Register Activity..........");
                  UserDetailsClass obj = new UserDetailsClass();
                  FIRSTNAME = fnameText.getText().toString();
                  LASTNAME = lnameText.getText().toString();
                  USERNAME = unameText.getText().toString();
                  PASSWORD = passText.getText().toString();
                  CITY = cityText.getText().toString();
  
                   obj.setFname(FIRSTNAME);
                   obj.setLname(LASTNAME);
                   obj.setUname(USERNAME);
                   obj.setPass(PASSWORD);
                   obj.setCity(CITY);
                   System.out.println("object set in Register Activity........");
                  DatabaseHandler databaseHandler = new DatabaseHandler(getApplicationContext());
                  System.out.println("Database handler created ........");
                  databaseHandler.addDetails(obj);
              }
          });
  
      }
  }
  </code></pre>
  
  <p><strong>DatabaseHandler.java</strong></p>
  
  <pre><code>public class DatabaseHandler  extends SQLiteOpenHelper{
  
  
      // All Static variables
      // Database Version
      private static final int DATABASE_VERSION = 1;
  
      // Database Name
      private static final String DATABASE_NAME = "UserManager";
  
      // Contacts table name
      private static final String USER_DETAILS = "userdetails";
  
      // Contacts Table Columns names
      private static final String KEY_FNAME = "fname";
      private static final String KEY_LNAME = "lname";
      private static final String KEY_USERNAME = "username";
      private static final String KEY_PASSWORD = "password";
      private static final String KEY_CITY = "city";
  
  
      public DatabaseHandler(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      // Creating Tables
      @Override
      public void onCreate(SQLiteDatabase db) {
          System.out.println(" begin of table creation...");
          String CREATE_USER_TABLE = "CREATE TABLE " + USER_DETAILS + "(" + KEY_FNAME + "TEXT," 
          + KEY_LNAME + "TEXT," + KEY_PASSWORD + "TEXT," + KEY_USERNAME + " TEXT PRIMARY KEY," 
          + KEY_CITY + " TEXT" + ")";
          db.execSQL(CREATE_USER_TABLE);
      }
  
      // Upgrading database
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // Drop older table if existed
          db.execSQL("DROP TABLE IF EXISTS " + USER_DETAILS);
  
          // Create tables again
          onCreate(db);
      }
  
  
  //   REGISTERING USER METHOD 
      void addDetails(UserDetailsClass obj) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(KEY_FNAME, obj.getFname());
          values.put(KEY_LNAME, obj.getLname());
          values.put(KEY_USERNAME, obj.getUname());
          values.put(KEY_PASSWORD, obj.getPass());
          values.put(KEY_CITY, obj.getCity());
  
          // Inserting Row
          db.insert(USER_DETAILS, null, values);
          db.close(); // Closing database connection
      }
  
      // Getting single contact
       UserDetailsClass getDetails(String uname, String pass) {
          SQLiteDatabase db = this.getReadableDatabase();
          System.out.println("I'm in getDetails() of DatabaseHandler .......");
          Cursor cursor = db.query(USER_DETAILS, new String[] { KEY_FNAME,KEY_LNAME,KEY_USERNAME,
                  KEY_PASSWORD, KEY_CITY }, KEY_USERNAME + "=? &amp; " + KEY_PASSWORD + "=?",
                  new String[] { String.valueOf(uname), String.valueOf(pass) }, null, null, null, null);
          if (cursor != null)
              cursor.moveToFirst();
  
          UserDetailsClass obj1 = new UserDetailsClass();
          obj1.setFname(cursor.getString(0));
          obj1.setLname(cursor.getString(1));
          obj1.setUname(cursor.getString(2));
          obj1.setPass(cursor.getString(3));
          obj1.setCity(cursor.getString(4));
          // return contact
          return obj1;
      }
  }
  </code></pre>
  
  <p><strong>UserDetailsClass.java</strong></p>
  
  <pre><code>public class UserDetailsClass {
      public String fname = null;
      public String lname = null;
      public String Uname = null;
      public String Pass = null;
      public String city = null;
      public String getFname() {
          return fname;
      }
      public void setFname(String fname) {
          this.fname = fname;
      }
      public String getLname() {
          return lname;
      }
      public void setLname(String lname) {
          this.lname = lname;
      }
      public String getUname() {
          return Uname;
      }
      public void setUname(String uname) {
          Uname = uname;
      }
      public String getPass() {
          return Pass;
      }
      public void setPass(String pass) {
          Pass = pass;
      }
      public String getCity() {
          return city;
      }
      public void setCity(String city) {
          this.city = city;
      }
  
  }
  </code></pre>
  
  <p><strong>Here my logcat.</strong></p>
  
  <pre><code>01-10 18:08:46.782: E/Database(334): Error inserting username=jmwtp  lname=gjtp  password=ajtp fname=gjm  city=address 
  01-10 18:08:46.782: E/Database(334): android.database.sqlite.SQLiteException: table userdetails has no column named lname: , while compiling: INSERT INTO userdetails(username, lname, password, fname, city) VALUES(?, ?, ?, ?, ?);
  01-10 18:08:46.782: E/Database(334):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
  01-10 18:08:46.782: E/Database(334):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
  01-10 18:08:46.782: E/Database(334):    at android.database.sqlite.SQLiteCompiledSql.&lt;init&gt;(SQLiteCompiledSql.java:65)
  01-10 18:08:46.782: E/Database(334):    at android.database.sqlite.SQLiteProgram.&lt;init&gt;(SQLiteProgram.java:83)
  01-10 18:08:46.782: E/Database(334):    at android.database.sqlite.SQLiteStatement.&lt;init&gt;(SQLiteStatement.java:41)
  01-10 18:08:46.782: E/Database(334):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
  01-10 18:08:46.782: E/Database(334):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
  01-10 18:08:46.782: E/Database(334):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
  01-10 18:08:46.782: E/Database(334):    at com.bam.testdatabse.DatabaseHandler.addDetails(DatabaseHandler.java:67)
  01-10 18:08:46.782: E/Database(334):    at com.bam.testdatabse.Register$1.onClick(Register.java:62)
  01-10 18:08:46.782: E/Database(334):    at android.view.View.performClick(View.java:2485)
  01-10 18:08:46.782: E/Database(334):    at android.view.View$PerformClick.run(View.java:9080)
  01-10 18:08:46.782: E/Database(334):    at android.os.Handler.handleCallback(Handler.java:587)
  01-10 18:08:46.782: E/Database(334):    at android.os.Handler.dispatchMessage(Handler.java:92)
  01-10 18:08:46.782: E/Database(334):    at android.os.Looper.loop(Looper.java:123)
  01-10 18:08:46.782: E/Database(334):    at android.app.ActivityThread.main(ActivityThread.java:3683)
  01-10 18:08:46.782: E/Database(334):    at java.lang.reflect.Method.invokeNative(Native Method)
  01-10 18:08:46.782: E/Database(334):    at java.lang.reflect.Method.invoke(Method.java:507)
  01-10 18:08:46.782: E/Database(334):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  01-10 18:08:46.782: E/Database(334):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  01-10 18:08:46.782: E/Database(334):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>