Preeyah Preeyah - 27 days ago 6
Android Question

Name is stored inside database table of SQLiteDatabase instead of its ID

I'm having trouble with

SQLiteDatabase
. I've managed to store data into table. Unfortunately, I've stored name inside table instead of it's
id
. I need to store the
id
inside the table.

Please take a look at my codes and see if there's anything wrong or anything missing from the codes.

This is my database codes
LikesDBAdapter.java


public class LikesDBAdapter extends AnniversaryDBAdapter
{
public static final String KEY_ROWID = "likes_id";
public static final String KEY_LIKES = "like";
public static final String KEY_NAME = "name_id";
private static final String TAG = "DBAdapter";
private static final String CREATE_TABLE_LIKES = "likes";

//private SQLiteDatabase db;

public LikesDBAdapter(Context ctx)
{
super(ctx);

}

public long insertLikes(String likes, String name)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_LIKES, likes);
initialValues.put(KEY_NAME, name);
return db.insert(CREATE_TABLE_LIKES, null, initialValues);
}// end insertContact()

public boolean deleteLikes(long rowId)
{
return db.delete(CREATE_TABLE_LIKES, KEY_ROWID + "=" + rowId, null) > 0;
}// end deleteContact()

public Cursor getAllLikes()
{
return db.query(CREATE_TABLE_LIKES, new String[]
{ KEY_ROWID, KEY_LIKES, KEY_NAME }, null, null, null, null, null);
}// end getAllContacts()

public Cursor getLikes(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, CREATE_TABLE_LIKES, new String[] {
KEY_ROWID, KEY_LIKES, KEY_NAME }, KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}// end getContact()

}//end DBAdapter


Oh this is another
database
class where 5 tables are created within one
DBAdapter
class

public class AnniversaryDBAdapter
{

private static final String DATABASE_NAME = "Tables";
private static final int DATABASE_VERSION = 2;

private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, title text not null, image text not null);";
private static final String CREATE_TABLE_BUDDIESLIST = "create table buddiesList(name_id integer primary key autoincrement, name text not null);";
private static final String CREATE_TABLE_LIKES = "create table likes(likes_id integer primary key autoincrement,like text not null, name text not null);";
private static final String CREATE_TABLE_DISLIKES = "create table dislike(dlike_id integer primary key autoincrement, dislike text not null, name text not null);";
private static final String CREATE_TABLE_EVENTS = "create table events" +
"(event_id integer primary key autoincrement, " +
"title text not null, location text not null, " +
"starttime text not null, " +
"endtime text not null, " +
"desc text not null, " +
"alarm text not null, " +
"date text not null, " +
"name_id integer not null);";


This is another code where I need to insert information into database table

btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
likeDB.open();
long like_id;

Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
String NameValue = nameSpinner.getSelectedItem().toString();

EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
String LikeValue = txtLikes.getText().toString();

like_id = likeDB.insertLikes(LikeValue, NameValue);
likeDB.close();

dlikeDB.open();
long dlike_id;

Spinner names = (Spinner) findViewById(R.id.nameSpinner);
String NamesValue = names.getSelectedItem().toString();

EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
String DislikeValue = txtDislikes.getText().toString();

dlike_id = dlikeDB.insertDislikes(DislikeValue, NamesValue);
dlikeDB.close();

Toast.makeText(getBaseContext(), "Your information is saved successfully! :D", Toast.LENGTH_LONG).show();


}
});


I've declared the
likeDB
and
dlikeDB
above
onCreate()
method

LikesDBAdapter likeDB = new LikesDBAdapter(this);
DislikesDBAdapter dlikeDB = new DislikesDBAdapter(this);


UPDATE


i've edited the codes. I've removed some codes of the AnniversaryDBAdapter class in the post.

Answer

Why are you surprised? You are inserting name in the database in your insertLikes function

    public long insertLikes(String likes, String name)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_LIKES, likes);
        ***initialValues.put(KEY_NAME, name);***
        return db.insert(CREATE_TABLE_LIKES, null, initialValues);
    }// end insertContact()