android.database.sqlite.SQLiteException:表联系人没有名为surname的列(代码1):

时间:2017-05-03 09:34:10

标签: java android sqlite

因为我添加了contactNo和dob

,我才能让这个注册工作起作用
public class SignupActivity extends AppCompatActivity {

DatabaseHelper helper = new DatabaseHelper(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);
}

public void onSignupClick(View v) {
    if(v.getId() == R.id.bSignup);
    {
        EditText name = (EditText)findViewById(R.id.TFname);
        EditText surname = (EditText)findViewById(R.id.TFsurname);
        EditText uname = (EditText)findViewById(R.id.TFusername);
        EditText pass = (EditText)findViewById(R.id.TFPassword);
        EditText pass2 = (EditText)findViewById(R.id.TFConfirmPass);
        EditText dob = (EditText)findViewById(R.id.TFdob);
        EditText contactNo = (EditText)findViewById(R.id.TFcontactNo);

        String namestr = name.getText().toString();
        String surnamestr = surname.getText().toString();
        String unamestr = uname.getText().toString();
        String passstr = pass.getText().toString();
        String pass2str = pass2.getText().toString();
        String dobstr = dob.getText().toString();
        String contactnostr = contactNo.getText().toString();

        if(!passstr.equals(pass2str)) {
            //popup message
            Toast pass1 = Toast.makeText(SignupActivity.this, "Passwords don't match!", Toast.LENGTH_SHORT);
            pass1.show();
        } else {
            //insert the details in database
            Contact c = new Contact();
            c.setForename(namestr);
            c.setPass(passstr);
            c.setSurname(surnamestr);
            c.setUname(unamestr);
            c.setDOB(dobstr);
            c.setContactNo(contactnostr);

        helper.insertContact(c);
            Intent i = new Intent(SignupActivity.this, LoginActivity.class);
            startActivity(i);

        }


    }
}
}

public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_FORENAME = "forename";
private static final String COLUMN_SURNAME = "surname";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASS = "pass";
private static final String COLUMN_DOB = "dob";
private static final String COLUMN_CONTACTNO = "contactNo";

SQLiteDatabase db;
private static final String TABLE_CREATE ="create table contacts (id integer primary key , forename text ," +
        " surname text , uname text , pass text , dob integer , contactNo integer )";

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


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db = db;

}

public void  insertContact(Contact c) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "Select * from contacts";
    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();

    values.put(COLUMN_ID, count);
    values.put(COLUMN_FORENAME, c.getForename());
    values.put(COLUMN_SURNAME, c.getSurname());
    values.put(COLUMN_UNAME, c.getUname());
    values.put(COLUMN_PASS, c.getPass());
    values.put(COLUMN_DOB, c.getDOB());
    values.put(COLUMN_CONTACTNO, c.getContactNo());

    db.insert(TABLE_NAME, null, values);
    db.close();
}

public String searchPass(String uname) {
    db = this.getReadableDatabase();
    String query = "Select uname,pass from "+TABLE_NAME;
    Cursor cursor = db.rawQuery(query, null);
    String a, b;
    b = "not found";
    if(cursor.moveToFirst()){
        do{
            a = cursor.getString(0);

            if(a.equals(uname)){
                b = cursor.getString(1);
                break;
            }
        }
                while(cursor.moveToNext());
    }

    return b;
}

public boolean updateData(String uname, String pass, String contactNo ){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_UNAME, uname);
    contentValues.put(COLUMN_PASS, pass);
    contentValues.put(COLUMN_CONTACTNO, contactNo);
    db.update(TABLE_NAME, contentValues, "uname = ?", new String[] { uname });
    return true;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "Drop table IF EXISTS "+TABLE_NAME;
    db.execSQL(query);
    this.onCreate(db);
}
}

我链接这两个,因为当我点击注册然后这是在com.example.mattyrobbo31.goagain.DatabaseHelper.insertContact(DatabaseHelper.java:58)的Android监视器中时发生错误                                                                                     并在com.example.mattyrobbo31.goagain.SignupActivity.onSignupClick(SignupActivity.java:53)

我在配置中将其设置为在启动之前卸载,并尝试更改数据库版本的数量,但没有更改。

1 个答案:

答案 0 :(得分:0)

线索是“因为我添加了contactNo和dob,所以无法使用此注册表工作”。请注意,DATABASE_VERSION仍设置为1。

可能您的代码是针对早期数据库版本运行的。每次进行更改时都会增加DATABASE_VERSION,以便调用onUpgrade方法。