当我访问删除方法数据库时应用程序强制关闭

时间:2013-05-17 10:03:57

标签: android database sqlite android-sqlite sqliteopenhelper

当我访问delete方法时,我的应用程序强制关闭,它与insert和get items方法一起工作正常。 在databasehandler中,

public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "mydb";
private static final String TABLE_NAME = "mytable";
private static final String _id = "_id";
private static final String name = "name";

public DatabaseHandler(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTableQuery = "create table " + TABLE_NAME + "(" + _id
            + " INTEGER PRIMARY KEY," + name + " TEXT)";
    db.execSQL(createTableQuery);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("drop table if exists " + TABLE_NAME);

    onCreate(db);
}

public void insertData(String label) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(name, label);
    db.insert(TABLE_NAME, null, values);
    db.close();
}

public void deleteData(String item){
    SQLiteDatabase db =this.getWritableDatabase();
    //db.delete(TABLE_NAME, name+ "='" +item +"'", null);
    db.execSQL("DELETE FROM TABLE_NAME WHERE name='"+item+"'");
    db.close();
}

     public Set<String> getAllData() {
    Set<String> set = new HashSet<String>();

    String selectQuery = "select * from " + TABLE_NAME;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            set.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    return set;
}
   }

在我的活动中,我有这个和

  public void onItemSelected(AdapterView<?> parent, View v, int position,
     long id) {
    String deleteitem = parent.getItemAtPosition(position).toString();
    db.deleteData(deleteitem);
    loadSpinner();
    }

如果我从我的活动中删除“db.deleteData(deleteitem);”,它没有任何问题。我不知道我错在哪里。一切似乎都很好。

 05-17 16:44:01.855: E/AndroidRuntime(1936): FATAL EXCEPTION: main
 05-17 16:44:01.855: E/AndroidRuntime(1936): android.database.sqlite.SQLiteException: no such table: TABLE_NAME: DELETE FROM TABLE_NAME WHERE name='Hi i am not available now. i will contact you soon.'
 05-17 16:44:01.855: E/AndroidRuntime(1936):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at com.praveenkutti1990.ezmesseger.DatabaseHandler.deleteData(DatabaseHandler.java:50)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at com.praveenkutti1990.ezmesseger.ListActivity.onItemSelected(ListActivity.java:90)
 05-17 16:44:01.855: E/AndroidRuntime(1936):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at android.widget.AdapterView.access$200(AdapterView.java:42)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at android.os.Handler.handleCallback(Handler.java:587)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at android.os.Handler.dispatchMessage(Handler.java:92)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at android.os.Looper.loop(Looper.java:123)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at android.app.ActivityThread.main(ActivityThread.java:3683)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at java.lang.reflect.Method.invokeNative(Native Method)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at java.lang.reflect.Method.invoke(Method.java:507)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  05-17 16:44:01.855: E/AndroidRuntime(1936):   at dalvik.system.NativeStart.main(Native Method)
  05-17 16:44:03.945: I/Process(1936): Sending signal. PID: 1936 SIG: 9

我的活动

public class ListActivity extends Activity implements OnClickListener,
         OnItemSelectedListener {

private EditText edittext;
private Button btnAdd;
DatabaseHandler db;
private Spinner spinner;
ArrayAdapter<String> adapter;
List<String> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    db = new DatabaseHandler(ListActivity.this);
    edittext = (EditText) findViewById(R.id.editText1);
    btnAdd = (Button) findViewById(R.id.btnadd);
    spinner = (Spinner) findViewById(R.id.spinner);
    btnAdd.setOnClickListener(this);
    spinner.setOnItemSelectedListener(this);
    loadSpinner();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    String name = edittext.getText().toString().trim();

    if (TextUtils.isEmpty(name)) {
        edittext.setError("Enter a valid Message");
        edittext.requestFocus();
    } else {
        db.insertData(name);
        edittext.setText("");

        // Hiding the keyboard
        InputMethodManager inputmangager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputmangager.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

    }
    Toast.makeText(getApplicationContext(),"Template added Successfully", 0).show();
    Intent in = new Intent(ListActivity.this,MainActivity.class);
    startActivity(in);
    finish();
}

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
     long id) {
    String deleteitem = parent.getItemAtPosition(position).toString();
    //db.deleteData(deleteitem);
    loadSpinner();
    }

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

private void loadSpinner() {
    // TODO Auto-generated method stub
    Set<String> set = db.getAllData();

    List<String> list = new ArrayList<String>(set);

    adapter = new ArrayAdapter<String>(ListActivity.this,
            android.R.layout.simple_spinner_item, list);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(adapter);
    spinner.setWillNotDraw(false);

}

  }

3 个答案:

答案 0 :(得分:3)

尝试改变

db.delete(TABLE_NAME, name+ "=" +item, null);

这一行

db.delete(TABLE_NAME, name+ "=?", new String[] { (item) });

像这样或者

db.delete(TABLE_NAME, name+ "='" +item+"'", null);
像这样。

我认为您在申请中没有使用btnAdd。如果是,请删除

btnAdd = (Button) findViewById(R.id.btnadd);

这一行。并从

中删除评论符号(//
//db.deleteData(deleteitem);

这一行。

并且还要改变

String createTableQuery = "create table " + TABLE_NAME + "(" + _id
            + " INTEGER PRIMARY KEY," + name + " TEXT)";

这一行

String createTableQuery = "create table " + TABLE_NAME + " (" + _id
            + " INTEGER PRIMARY KEY," + name + " TEXT)";
像这样。请注意TABLE_NAME(之间的空格。

让我知道会发生什么。

答案 1 :(得分:0)

只是'错过了这样的字符串:

db.delete(TABLE_NAME, name+ "='" +item + "'", null);

您正在删除SQL中需要放置的字符串。

更新:

只需改变一下:

db.execSQL("DELETE FROM TABLE_NAME WHERE name='"+item+"'");

by:

db.execSQL("DELETE FROM " +  TABLE_NAME + " WHERE name='"+item+"'");

答案 2 :(得分:0)

我更喜欢execSQL:

db.execSQL("DELETE FROM x WHERE y='"+z+"'");