android.database.sqlite.SQLiteException:表Empinfo没有名为emp_dob(code 1)的列:,编译时:INSERT INTO Empinfo

时间:2015-06-18 15:12:16

标签: android sqlite

我是新来的android请帮忙

当我运行我的应用程序后显示错误“插入emp_phone = 65 emp_dob = 5 emp_address = da emp_name = sa时出错 android.database.sqlite.SQLiteException:表Empinfo没有名为emp_dob的列(代码1):,同时编译:INSERT INTO Empinfo(emp_phone,emp_dob,emp_address,emp_name)VALUES(?,?,?,?)“

public class Employee extends ActionBarActivity implements OnClickListener {

EditText nameedittext,dateedittext,addressedittext,phoneedittext;
Button add;
DBhandle dbhandle;

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

    nameedittext=(EditText)findViewById(R.id.editText1);
    dateedittext=(EditText)findViewById(R.id.editText4);
    addressedittext=(EditText)findViewById(R.id.editText3);
    phoneedittext=(EditText)findViewById(R.id.editText2);

    add=(Button)findViewById(R.id.button1);

    add.setOnClickListener(this);

}

public void onClick(View view){

    String empname=nameedittext.getText().toString();
    String empdateofbirth=dateedittext.getText().toString();
    String empaddress=addressedittext.getText().toString();
    String empphone=phoneedittext.getText().toString();

    if(view==add)
    {
        if(empname.equals("")||empdateofbirth.equals("")||empaddress.equals("")||empphone.equals(""))
        {
            Toast.makeText(Employee.this, "file vacant", Toast.LENGTH_LONG).show();
        }
        else
        {
            dbhandle=new DBhandle(this);
            dbhandle=dbhandle.open();
            dbhandle.addEmpInfo(empname, empdateofbirth, empaddress, empphone);
            Toast.makeText(Employee.this, " info added successfully", Toast.LENGTH_LONG).show();

        }

        clearText();
}

数据库类

public class DBhandle {

private static final String DATABASE_NAME = "restaurantdatabase";
private static final int DATABASE_VERSION = 1;
final Context context;
private SQLiteDatabase ourDatabase;
DatabaseHelper dbHelper;

    //table name
    private static final String EMPINFO_TABLE_NAME = "Empinfo";

    public static final String EMP_ID = "emp_id";
    public static final String EMP_NAME = "emp_name";
    public static final String EMP_DOB = "emp_dob";
    public static final String EMP_PHONE = "emp_phone";
    public static final String EMP_ADDR = "emp_address";


public class DatabaseHelper extends SQLiteOpenHelper{

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

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + EMPINFO_TABLE_NAME + " (" + EMP_ID 
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + EMP_NAME 
                + " TEXT NOT NULL, " + EMP_PHONE + " TEXT NOT NULL, " + EMP_ADDR + " TEXT NOT NULL);"
                );

    }

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

    }

}

public DBhandle(Context ctx){

        context = ctx;
}

public DBhandle open() throws SQLException
{
    dbHelper = new DatabaseHelper(context);
    ourDatabase = dbHelper.getWritableDatabase();

    return this;
}

public void close(){

    dbHelper.close();
}
public long addEmpInfo(String empname,String empdateofbirth,String empaddress,String empphone) {
        ContentValues newValues = new ContentValues();
        // Assign values for each row.
        newValues.put("emp_name", empname);
        newValues.put("emp_dob", empdateofbirth);
        newValues.put("emp_address", empaddress);
    return ourDatabase.insert(EMPINFO_TABLE_NAME, null, newValues);
    }

1 个答案:

答案 0 :(得分:2)

在oncreate方法中未创建EMP_DOB,未指定EMP_DOB

 public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + EMPINFO_TABLE_NAME + " (" + EMP_ID 
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + EMP_NAME 
            + " TEXT NOT NULL, " + EMP_PHONE + " TEXT NOT NULL, " + EMP_ADDR + " TEXT NOT NULL);"
            );

以下是正确的

 public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + EMPINFO_TABLE_NAME + " (" + EMP_ID 
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + EMP_NAME 
            + " TEXT NOT NULL, " + EMP_DOB + " TEXT NOT NULL, " + EMP_PHONE + " TEXT NOT NULL, " + EMP_ADDR + " TEXT NOT NULL);"
            );