askIT

Insert (Android SQLite)

The class DBadapter should include code for inserting data into a database table. Imagine that we want to insert data in the table notes. This is done by first programming method insertRecordToNotes in DBadapter. The class MainActivity can then call this method.

Tabell notes

Field Type Key
note_id int Primary key
note_title varchar
note_text text

DBadapter

// Insert record
public long insertRecordToNotes(String inpTitle, String inpText){
        ContentValues initialValues = new ContentValues();
        initialValues.put("note_title", inpTitle);
        initialValues.put("note_text", inpText);

        return db.insert(databaseTableNotes, null, initialValues);
}

MainActivity

long id = db.insertRecordToNotes("Shopping list", "Cola Zero, Crispbread");

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.