Android:在表中插入数据时出错

时间:2016-06-08 15:39:22

标签: java android

我在db中创建了2个表。 1工作正常。 TABLE_NAME2在插入数据时发出错误,指出未找到“列电子邮件”。

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;
}

日志:

(1)表注册没有名为EMAIL的列     06-08 21:00:51.306 15019-15019 / com.mgm.manish.trekcompanion E / SQLiteDatabase:插入EMAIL时出错= abc NAME = abc PASSWORD = abc MOBILE = abc                                                                               android.database.sqlite.SQLiteException:表注册没有名为EMAIL的列(代码1):,编译时:INSERT INTO注册(EMAIL,NAME,PASSWORD,MOBILE)VALUES(?,?,?,?)

请解决此问题

2 个答案:

答案 0 :(得分:0)

在此页面中,我将数据发送到DatabaseHelper Class以插入数据。 它显示了toast"成功注册",但在Logs中显示错误,并且它也没有导航到LoginActivity类。它正在导航到MainActivity。 即使是其他部分也不起作用。当我输入不同的密码时,它会让错误的应用停止。

 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();
            }
        }

答案 1 :(得分:0)

这是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;
}

}