简体   繁体   中英

Android Studio SQLite save title if not in database?

Hello I am making a simple note application, using an SQLite database, using a custom arraylist adapter, where the user can save a note having a title, a descriptive text, and the date. Everything works, but I want users to be able to save a new note only if the title is not in the database. How can I do this ?

Here is the edit note

public class Edit_notes extends AppCompatActivity {

    private DBOpenHelper dbop;
    private SQLiteDatabase sdb;
    private EditText title_text;
    private EditText note_text;


    public boolean SaveNote(){

        String note_title_string = title_text.getText().toString();
        String note_text_string = note_text.getText().toString();

        if (!note_title_string.isEmpty()){
            if(!note_text_string.isEmpty()) {

// Need to check if title is not in the database then insert else don't

                String date = new Date().getDate() + "/" + (new Date().getMonth() + 1) + "/" + (new Date().getYear() + 1900);
                AddData(note_title_string, note_text_string, date); // Add title to the database
                Toast.makeText(this, "Note saved", Toast.LENGTH_SHORT).show();
                finish();
            }
            else {
                Toast.makeText(this, "Note text cannot be empty", Toast.LENGTH_SHORT).show();
            }

        }
        else{
            Toast.makeText(this, "Title cannot be empty", Toast.LENGTH_SHORT).show();
        }

        return true;
    }

    public void AddData(String title_entry, String text_entry, String date){

        dbop = new DBOpenHelper(this);
        sdb = dbop.getWritableDatabase();

        ContentValues cv = new ContentValues();
        cv.put("TITLE", title_entry);
        cv.put("TEXT", text_entry);
        cv.put("DATE", date);

        sdb.insert("note_table", null, cv);
    }

}

SQLite database.java:

public class DBOpenHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "notes.db";
public static final String TABLE_NAME = "note_table";
public static final String ID_COLUMN = "ID";
public static final String TITLE_COLUMN = "TITLE";
public static final String TEXT_COLUMN = "TEXT";
public static final String DATE_COLUMN = "DATE";

SQLiteDatabase db = this.getWritableDatabase();


public DBOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, 5);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME
            + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + " TITLE TEXT, " + " TEXT TEXT, " + " DATE STRING)";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table note_table");
    onCreate(db);
}

}

I guess there is no need to provide the mainactivity.java

Change the table Create to :-

String createTable = "CREATE TABLE " + TABLE_NAME
        + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + " TITLE TEXT UNIQUE, " + " TEXT TEXT, " + " DATE STRING)";

Uninstall the App, or delete the App's Data, or increase the database version number and rerun the App. Row will not be added UNIQUE constraint conflict (same title) (insert method effectively uses INSERT OR IGNORE ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM