简体   繁体   中英

Android: Throwing error while inserting data in table

I have created 2 tables in a db. 1 is working fine. TABLE_NAME2 is giving error while inserting data in it saying Column Email is not found.

DatabaseHelper.java:

     @Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME2 + "("
            + USER_NAME + "TEXT,"
            + USER_EMAIL + "TEXT,"
            + USER_MOBILE + "TEXT, "
            + USER_PASS + "TEXT)");
    db.execSQL("CREATE Table " + TABLE_NAME1 + "("// + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COL2 + " TEXT,"
            + COL3 + " TEXT,"
            + COL4 + " TEXT,"
            + COL5 + " TEXT,"
            + COL6 + " TEXT,"
            + COL7 + " TEXT)");
     }  

    public boolean insertSignupDataDB(String name, String email, String mobile, String pass){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(USER_NAME, name);
    contentValues.put(USER_EMAIL, email);
    contentValues.put(USER_MOBILE, mobile);
    contentValues.put(USER_PASS, pass);

    long result = db.insert(TABLE_NAME2, null, contentValues);
    if (result==-1){
        return false;
    }
    else
        return true;
}

Log:

(1) table signup has no column named EMAIL 06-08 21:00:51.306 15019-15019/com.mgm.manish.trekcompanion E/SQLiteDatabase: Error inserting EMAIL=abc NAME=abc PASSWORD=abc MOBILE=abc android.database.sqlite.SQLiteException: table signup has no column named EMAIL (code 1): , while compiling: INSERT INTO signup(EMAIL,NAME,PASSWORD,MOBILE) VALUES (?,?,?,?)

Please solve this

From this page I am sending data to DatabaseHelper Class to insert data. It shows the toast "successful signup", but is showing error in Logs, and also it is not navigating to LoginActivity class. it is navigating to MainActivity. Even else part is not working. when i put different passwords it gives error app stopped.

 if(etSignPass.getText().toString().equals(etSignConPass.getText().toString())){

                boolean queryResult = dbHelper.insertSignupDataDB(etSignName.getText().toString(),
                        etSignEmail.getText().toString(), etSignMobile.getText().toString(), etSignPass.getText().toString());
                if(queryResult=true){
                    Toast.makeText(SignUp.this, "Successfully Signed up", Toast.LENGTH_SHORT).show();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        navigateUpTo(new Intent(SignUp.this,LoginActivity.class));
                    }

                }
                else{
                    Toast.makeText(SignUp.this, "Error", Toast.LENGTH_SHORT).show();
                }

            }

            else{
                etSignConPass.setBackgroundColor(Color.RED);
                onClicSignUp();
            }
        }

This is complete code of DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "abc.db";
public static final String TABLE_NAME1 = "table1";
public static final String COL2 = "col1";
public static final String COL3 = "Date";
public static final String COL4 = "Cost";
public static final String COL5 = "loc1";
public static final String COL6 = "loc2";
public static final String COL7 = "Description";

public static final String TABLE_NAME2 = "signup";
public static final String USER_NAME = "NAME";
public static final String USER_EMAIL = "EMAIL";
public static final String USER_MOBILE = "MOBILE";
public static final String USER_PASS = "PASSWORD";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);


}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME2 + "("
            + USER_NAME + "TEXT,"
            + USER_EMAIL + "TEXT,"
            + USER_MOBILE + "TEXT, "
            + USER_PASS + "TEXT)");
    db.execSQL("CREATE Table " + TABLE_NAME1 + "("// + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COL2 + " TEXT,"
            + COL3 + " TEXT,"
            + COL4 + " TEXT,"
            + COL5 + " TEXT,"
            + COL6 + " TEXT,"
            + COL7 + " TEXT)");
     }



    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME1);
    db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME2);
    onCreate(db);


}

public boolean insertHomeDataDB(String locName, String date, String cost, String startLoc, String endLoc, String description){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, locName);
    contentValues.put(COL3, date);
    contentValues.put(COL4, cost);
    contentValues.put(COL5, startLoc);
    contentValues.put(COL6, endLoc);
    contentValues.put(COL7, description);
    //contentValues.put(COL8, userId);

    long result = db.insert(TABLE_NAME1, null, contentValues);
    if (result==-1){
        return false;
    }
    else
        return true;
}

public boolean insertSignupDataDB(String name, String email, String mobile, String pass){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(USER_NAME, name);
    contentValues.put(USER_EMAIL, email);
    contentValues.put(USER_MOBILE, mobile);
    contentValues.put(USER_PASS, pass);

    long result = db.insert(TABLE_NAME2, null, contentValues);
    if (result==-1){
        return false;
    }
    else
        return true;
}

public Cursor getAllHomeData(SQLiteDatabase db){
    Cursor res;
    String[] projection = {COL2,COL3,COL4};
    res = db.query(TABLE_NAME1,projection,null,null,null,null,null);
    return res;

}

public Cursor getAllProfileData(SQLiteDatabase db){
    Cursor res;
    String[] projection={USER_NAME,USER_EMAIL,USER_MOBILE,USER_PASS};
    res = db.query(TABLE_NAME2, projection,null,null,null,null,null);
    return res;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM