Android 如何避免 SQLite 数据库中的重复行?

How to avoid duplicate rows in SQLite database in Android?

我对如何避免 Sqlite 中的重复行感到困惑 Database.I 在 Whosebug 上尝试了所有可能的答案,但 none 对 me.I 有效 尝试使用唯一约束,db.insertWithOnConflict() 但它们都没有用。 谁能帮我弄清楚我的错误? 这是我的 Android 工作室的代码:

Params.java:

public class Params {
public static final int DB_VERSION=1;
public static final String DB_NAME="PDF_NAME";
public static final String TABLE_NAME="PDF_TABLE";

public static final String KEY_ID="ID";
public static final String KEY_NAME="NAME";
public static final String KEY_PAGE="PAGE";

}

MyDbHandler.java

public class MyDbHandler extends SQLiteOpenHelper {
  public static SQLiteDatabase database;
public MyDbHandler(Context context) {
    super(context, Params.DB_NAME, null, Params.DB_VERSION);
}

@Override
   public void onCreate(SQLiteDatabase db) {
    String create="CREATE TABLE "+Params.TABLE_NAME+"("+Params.KEY_ID+" INTEGER PRIMARY KEY,"+Params.KEY_NAME+" TEXT UNIQUE, "
            +Params.KEY_PAGE+" INTEGER"+")";
    db.execSQL(create);
}

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

}
public void addPDF(File file)
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(Params.KEY_NAME,file.getName());
    values.put(Params.KEY_PAGE, ViewPdf.pagenumber);
    db.insertWithOnConflict(Params.TABLE_NAME,null,values,SQLiteDatabase.CONFLICT_REPLACE);
    Log.d("pa","pagenumber"+file.getName() + ViewPdf.pagenumber);
    Log.d("data","Inserted");
    db.close();

}
public Cursor getALlfile()
{
    database=this.getReadableDatabase();
    String select="SELECT * FROM "+Params.TABLE_NAME;
    Cursor cursor=database.rawQuery(select,null);
   return cursor;
 }


}

DocumentsFragment.java:

 public void onItemClick(int position) {

    Intent intent = new Intent(getContext(), ViewPdf.class);
    intent.putExtra("Position", position);
    startActivity(intent);
    if (!HistoryFragment.pdfHistory.contains(pdf.get(position))) {
        HistoryFragment.pdfHistory.add(0, pdf.get(position));
    } else {
        File newpdf = pdf.get(position);
        int pos = HistoryFragment.pdfHistory.indexOf(newpdf);
        HistoryFragment.pdfHistory.remove(pos);
        HistoryFragment.pdfHistory.add(0, newpdf);
    }
     Cursor cursor=MyDbHandler.database.rawQuery("Select * from" + Params.TABLE_NAME + "where"+Params.KEY_NAME+ "=" +(pdf.get(position).getName()) ,null);
    if(cursor.moveToFirst())
        Toast.makeText(getContext(),"already exist", Toast.LENGTH_SHORT).show();
    else{
       db.addPDF(pdf.get(position));
    }

我也附上我的 logcat 如果它有任何用处:

更新

     2021-05-08 17:54:15.181 11873-11873/com.flashxpdfreader.flash2021 E/SQLiteLog: (1) no such column: ghty.pdf
2021-05-08 17:54:15.182 11873-11873/com.flashxpdfreader.flash2021 D/AndroidRuntime: Shutting down VM
2021-05-08 17:54:15.183 11873-11873/com.flashxpdfreader.flash2021 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.flashxpdfreader.flash2021, PID: 11873
    android.database.sqlite.SQLiteException: no such column: ghty.pdf (code 1 SQLITE_ERROR): , while compiling: Select * from PDF_TABLE where NAME = ghty.pdf
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1382)
        at com.flashxpdfreader.flash2021.Fragment.DocumentsFragment.onItemClick(DocumentsFragment.java:156)
        at com.flashxpdfreader.flash2021.RecylerAdapter$ViewHolder.lambda$new[=14=]$RecylerAdapter$ViewHolder(RecylerAdapter.java:137)
        at com.flashxpdfreader.flash2021.-$$Lambda$RecylerAdapter$ViewHoldera1BheGsPwqyn4c_IFEij433NiM.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7185)
        at android.view.View.performClickInternal(View.java:7162)
        at android.view.View.access00(View.java:819)
        at android.view.View$PerformClick.run(View.java:27684)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7562)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

这是我的 Sqlite 数据库浏览器的屏幕截图:

您可以注意到我的数据库中实际上有相同的文件名,但它仍然说没有这样的列。

而是检查你的光标是否 returns 有什么东西。如果 itemcount 是 != 0 pdf 存在。

Cursor cursor=MyDbHandler.database.rawQuery("Select * from " + Params.TABLE_NAME + " where "+Params.KEY_NAME+ "=" +(pdf.get(position).getName()) ,null);    
if(cursor.getCount() != 0)
    Toast.makeText(getContext(),"already exist",Toast.LENGTH_SHORT).show();
else{
   db.addPDF(pdf.get(position));
}
android.database.sqlite.SQLiteException: near "fromPDF_TABLEwhereNAME": syntax error (code 1 SQLITE_ERROR): , while compiling: Select * fromPDF_TABLEwhereNAME=fhg.pdf

目前,您的sql声明是这样的:

Select * fromPDF_TABLEwhereNAME=fhg.pdf

您需要添加 space。您的 sql 声明需要像这样:

Select * from PDF_TABLE where NAME = fhg.pdf

在 DocumentsFragment.java 中,尝试更改

中的代码
Cursor cursor=MyDbHandler.database.rawQuery("Select * from" + Params.TABLE_NAME + "where"+Params.KEY_NAME+ "=" +(pdf.get(position).getName()) ,null);

Cursor cursor=MyDbHandler.database.rawQuery("Select * from " + Params.TABLE_NAME + " where "+Params.KEY_NAME+ " = " +(pdf.get(position).getName()) ,null);