Not able to create table and columns in SQLite Database

I created database for my application and produced code for table creation. It produce the error as follows.

Database Coding (DatabaseHandler3)

 import java.util.ArrayList;
 import java.util.List;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class DatabaseHandler3 extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "DOCROID.sqlite";
private static final String TABLE_CONTACTS = "UserRegistration";
private static final String username = "name";
private static final String userage = "age";
private static final String usergender = "gender";
private static final String useraddress = "address";
private static final String usercode = "code";
private static final String userid = "id";
private static final String userpassword = "password";
private static final String usercpass = "cpass";
private static final String LOGCAT = null;
private static Context context;
public DatabaseHandler3(Context context) {
    super(context, "/sdcard/DOCROID/" + DATABASE_NAME, null, DATABASE_VERSION);
    Log.d("DatabaseHandler3","Database Created");
    // TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

    db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + username + " TEXT" + userage + " INTEGER " + usergender + " TEXT" + useraddress + " TEXT" + usercode + " INTEGER" + userid + " INTEGER" + userpassword + " BLOB" + usercpass + " BLOB ) ;");
    Log.d("DatabaseHandler3", " User Table Created");


}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}
public void addContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(username, contact.getNAME());
    values.put(userage, contact.getAGE());
    values.put(usergender, contact.getGENDER());
    values.put(useraddress, contact.getADDRESS());
    values.put(usercode, contact.getCODE());
    values.put(userid, contact.getID());
    values.put(userpassword, contact.getPASSWORD());
    values.put(usercpass, contact.getCPASS());
   // db.insert(TABLE_CONTACTS, null, values);
    db.execSQL("insert into UserRegistration (username,userage,usergender,useraddress,usercode,userid,userpass,usercpass) values ('" + username +"','"+ userage + "','" + usergender +"','" + useraddress + "','" + usercode +"','" + userid + "','" + userpassword + "','" + usercpass + "')");
    db.close();
}
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setNAME(cursor.getString(0));
            contact.setAGE(cursor.getString(1));
            contact.setGENDER(cursor.getString(2));
            contact.setADDRESS(cursor.getString(3));
            contact.setCODE(cursor.getString(4));
            contact.setID(cursor.getString(5));
            contact.setPASSWORD(cursor.getString(6));
            contact.setCPASS(cursor.getString(7));
            contactList.add(contact);
        } while (cursor.moveToNext());
    }
    return contactList;
}
public void deleteall() {
    SQLiteDatabase db = this.getWritableDatabase();
    String selectQuery = "DELETE FROM " + TABLE_CONTACTS;
    db.execSQL(selectQuery);
}
}

Main Code (userregistration.java)

 public class userregistration extends ActionBarActivity {
 EditText UserName,UserAge,UserGender,Useraddress,Userpincode, Userid,UserPass,UserCPass;
 String username,userage, usergender, useraddress, usercode, userid, userpass,usercpass;
public SQLiteDatabase db = null;
Button Reg;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_userregistration);
    UserName = (EditText) findViewById(R.id.editText11);
    UserAge = (EditText) findViewById(R.id.editText69);
    UserGender = (EditText) findViewById(R.id.editText70);
    Useraddress = (EditText) findViewById(R.id.editText73);
    Userpincode = (EditText) findViewById(R.id.editText74);
    Userid = (EditText) findViewById(R.id.editText75);
    UserPass = (EditText) findViewById(R.id.editText76);
    UserCPass = (EditText) findViewById(R.id.editText72);
    Reg = (Button) findViewById(R.id.button24);
    Reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            username =UserName.getText().toString();
            userage = UserAge.getText().toString();
            usergender = UserGender.getText().toString();
            useraddress = Useraddress.getText().toString();
            usercode = Userpincode.getText().toString();
            userid = Userid.getText().toString();
            userpass = UserPass.getText().toString();
            usercpass = UserCPass.getText().toString();

            if (username.trim().equals("")&& userage.trim().equals("") && usergender.trim().equals("") && useraddress.trim().equals("") && usercode.trim().equals("") && userid.trim().equals("") && userpass.trim().equals("") && usercpass.trim().equals(""))
            {
                Toast.makeText(getBaseContext(),"Fields should not empty",Toast.LENGTH_LONG).show();
            }
            else
            {
              DatabaseHandler3 db=new DatabaseHandler3(userregistration.this);
                db.addContact(new Contact(username, userage, usergender, useraddress, usercode, userid, userpass, usercpass));
                Toast.makeText(getBaseContext(),"Registration Success",Toast.LENGTH_LONG).show();
             finish();
            }
        }
    }) ;
}

Error Report:

  05-04 21:49:20.516    6128-6128/com.example.bathrivasudevan.docroid     E/SQLiteLog﹕ (1) table UserRegistration has no column named username
  05-04 21:49:20.556    6128-6128/com.example.bathrivasudevan.docroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: table UserRegistration has no column named username (code 1): , while compiling: insert into UserRegistration (username,userage,usergender,useraddress,usercode,userid,userpass,usercpass) values ('name','age','gender','address','code','id','password','cpass')
        at     android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1108)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:681)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:589)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1769)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1700)
        at com.example.bathrivasudevan.docroid.DatabaseHandler3.addContact(DatabaseHandler3.java:60)
        at com.example.bathrivasudevan.docroid.userregistration$1.onClick(userregistration.java:85)
        at android.view.View.performClick(View.java:4275)
        at android.view.View$PerformClick.run(View.java:17434)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:177)
        at android.app.ActivityThread.main(ActivityThread.java:4947)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)

Answers


column "username" does not exists, you have created a column with name "name". See your username variable

private static final String username = "name";

You are referencing column with name "username"

 db.execSQL("insert into UserRegistration (username,userage,usergender,useraddress,usercode,userid,userpass,usercpass) values ('" + username +"','"+ userage + "','" + usergender +"','" + useraddress + "','" + usercode +"','" + userid + "','" + userpassword + "','" + usercpass + "')");

Change one of the Strings so they are equal.

EDIT: You seem to have several issues like this, fix them all.


Use Like this add , after type

db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + username + " TEXT," + userage + " INTEGER, " + usergender + " TEXT," + useraddress + " TEXT," + usercode + " INTEGER," + userid + " INTEGER," + userpassword + " BLOB," + usercpass + " BLOB)");

Need Your Help

How do I separate two SQL queries into separate HTML classes?

php mysql sql database

I have a MySQL query that selects two random rows in a table.

Rails app, and need to add tags to a model

ruby-on-rails ruby model tags

I have this quiz rails app linked to an IRC bot who asks questions (yes, on IRC), where I have this Question model which contains, well, questions, answers, hints, and a few more things.