我是 android 新手,正在尝试制作一个应用程序,您将在 edittext 中输入一条消息,并将其发送到数据库中行 ID =“1”的另一个活动,一旦发送
另一个活动将收到此消息,如果单击名为“删除此消息”的按钮,则该消息将在数据库中同时删除它的行 ID
删除后,我可以再次在数据库中输入行 ID =“1”的消息。
这是我的代码
package com.example.pmfinal;
import java.sql.SQLException;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabasePM {
private static final String DATABASE_NAME = "InfoDb";
private static final int DATABASE_VERSION = 1;
public static final String KEY_ROWIDPM = "_id";
public static final String NOTIFPM = "notifpm";
private static final String TABLE_NOTIFPM = "notifprojman";
private static final String CREATE_NOTIFPM = "CREATE TABLE " + TABLE_NOTIFPM + " (" +
KEY_ROWIDPM + " INTEGER PRIMARY KEY, " +
NOTIFPM + " TEXT NOT NULL); ";
private DbHelper ourHelper;
private Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context Context) {
super(Context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_NOTIFPM);
;
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST " + TABLE_NOTIFPM);
onCreate(db);
}
}
public DatabasePM(Context c){
ourContext = c;
}
public DatabasePM open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createProject(String notifpm) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(NOTIFPM, notifpm);
return ourDatabase.insert(TABLE_NOTIFPM, null, cv);
}
public String getNotif(long l) {
String[] columns = new String[]{ KEY_ROWIDPM, NOTIFPM};
Cursor c = ourDatabase.query(TABLE_NOTIFPM, columns, KEY_ROWIDPM + "=" + l , null, null, null, null);
if (c != null) {
c.moveToFirst();
String notif = c.getString(1);
return notif;
}
return null;
}
public void deleteNotifPM(long l) {
// TODO Auto-generated method stub
ourDatabase.delete(TABLE_NOTIFPM, KEY_ROWIDPM + "=" + l , null);
}
}