Android Sqlite数据库未在新表中存储数据

时间:2019-02-19 09:35:21

标签: android sqlite android-intent android-sqlite

我创建了一个应用程序,它使用sqlite数据库显示数据。 我可以读取数据并将其显示在两个活动中,但是我无法存储或删除数据。我想保存并删除另一个表中的书签。

我在互联网上搜索了很多东西,看了很多视频,但是没有帮助。

这是我的数据库助手。

  public class DatabaseHelper extends SQLiteOpenHelper {

private  static final String DB_NAME = "mydic.db";
private static final int DB_VERSION = 1;
private String DB_PATH = null;
private  static final String TABLE_DICTIONARY = "dictionary1";
public   static final String TABLE_BOOKMARK= "bookmark";
private static final String COLUMN_ID = "id";
public static final String COL_WORD = "word";
public static final String COL_DEFINITION = "definition";
private Context mcontext;
private SQLiteDatabase mDatabase;

public DatabaseHelper( Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.mcontext = context;
    this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();
    if (!dbExist) {
        this.getReadableDatabase();
        try {
            mcontext.deleteDatabase(DB_NAME);
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}
public boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        e.printStackTrace();
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = mcontext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();
}
public void openDatabase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    mDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}

@Override
public synchronized void close() {
    if (mDatabase != null)
        mDatabase.close();
    super.close();
}
public Cursor getListWord(String wordSearch) {

    String query = "SELECT * FROM " + TABLE_DICTIONARY + " Where word Like ? " ;
    Cursor cursor = null;
    try {
        openDatabase();
        String[] args = {"%"+wordSearch+"%"};
         cursor = mDatabase.rawQuery(query , args);
    }catch (Exception e){
        e.printStackTrace();
    }
    return cursor;
}
public boolean addBookmark(String title, String subtitle) {

    // Gets the data repository in write mode
    SQLiteDatabase db = getWritableDatabase();

    // Create a new map of values, where column names are the keys
    ContentValues values = new ContentValues();
    values.put(DatabaseHelper.COL_WORD, title);
    values.put(DatabaseHelper.COL_DEFINITION, subtitle);

    // Insert the new row, returning the primary key value of the new row
    db.insert(DatabaseHelper.TABLE_BOOKMARK, null, values);

    return true;
}
public void removeBookmark(String title, String subtitle) {
    mDatabase = this.getReadableDatabase();
    mDatabase.execSQL("delete from favourites where  word Like ? ");
    mDatabase.close();
}
public Cursor getBookmarkWord(String wordSearch) {
    String query = "SELECT * FROM favourites Where word Like ? " ;
    Cursor cursor = null;
    try {
        openDatabase();
        String[] args = {"%"+wordSearch+"%"};
        cursor = mDatabase.rawQuery(query , args);
    }catch (Exception e){
        e.printStackTrace();
    }
    return cursor;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE favourites ( id INTEGER PRIMARY KEY AUTOINCREMENT,word,definition)" );
}

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

    if (newVersion > oldVersion) {
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

这是我的适配器

  word = itemView.findViewById(R.id.wordText);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int position = getAdapterPosition();
                cursor.moveToPosition(position);

                Intent intent = new Intent(context, DetailActivity.class);
                intent.putExtra("WORD",cursor.getString(1));
                intent.putExtra("DEFINITION",cursor.getString(2));
                context.startActivity(intent);

这是我的详细活动。

public class DetailActivity extends AppCompatActivity {

private TextToSpeech tts;
DatabaseHelper mDBHelper;
Context context;
DictionaryAdapter dictionaryAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

      final String word = getIntent().getStringExtra("WORD");
      final String definition = getIntent().getStringExtra("DEFINITION");


     final TextView tvWordDetail = findViewById(R.id.tvWordDetail);
    WebView wordDefinition = findViewById(R.id.tvWordDefinition);
     final ImageButton btnBookmark = findViewById(R.id.btnBookmark);
    ImageButton textToSpeech = findViewById(R.id.textToSpeech);

    Cursor bookmarkWord = mDBHelper.getBookmarkWord("");
    int isMark = bookmarkWord == null? 0 : 1;
    btnBookmark.setTag(isMark);

    int icon = bookmarkWord == null? R.drawable.ic_bookmark_border:R.drawable.ic_bookmark_fill;
    btnBookmark.setImageResource(icon);
    tvWordDetail.setText(Html.fromHtml(word));
    wordDefinition.loadDataWithBaseURL(null,definition,"text/html", "utf-8",null);


    btnBookmark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int i = (int) btnBookmark.getTag();
            if (i == 0) {
                btnBookmark.setBackgroundResource(R.drawable.ic_bookmark_fill);
                btnBookmark.setTag(1);
                mDBHelper.addBookmark(word,definition);
            } else if (i == 1){
                btnBookmark.setBackgroundResource(R.drawable.ic_bookmark_border);
                btnBookmark.setTag(0);
                mDBHelper.removeBookmark(word,definition);
            }
        }
    });


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id== android.R.id.home){
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

这是我的日志

 2019-02-19 15:08:08.727 11561-11561/com.elytelabs.testnav E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elytelabs.testnav, PID: 11561
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elytelabs.testnav/com.elytelabs.testnav.DetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.elytelabs.testnav.database.DatabaseHelper.getBookmarkWord(java.lang.String)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.elytelabs.testnav.database.DatabaseHelper.getBookmarkWord(java.lang.String)' on a null object reference
    at com.elytelabs.testnav.DetailActivity.onCreate(DetailActivity.java:54)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

1 个答案:

答案 0 :(得分:1)

可能的原因是您尚未实例化 mDBHelper (仅声明它的评估者)。

通常您会使用DatabaseHelper mDBhelper;这将声明变量

通常在您要进行的活动的 onCreate 方法中

mDBHelper = new DatabaseHelper(this); 

实例化(构造) mDBHelper

因此,DetailActivity中的您的代码应为:-

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
mDBHelper = new DatabaseHelper(this); //<<<<<<<<<< ADDED THIS LINE

getSupportActionBar().setDisplayHomeAsUpEnabled(true);