我遇到SQLiteDatabase
时遇到问题。我已设法将数据存储到表中。不幸的是,我在表格中存储了名称,而不是id
。我需要将id
存储在表格中。
请查看我的代码,看看代码中是否有任何错误或遗漏。
这是我的数据库代码
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
哦这是另一个database
类,其中一个DBAdapter
类中创建了5个表
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);";
这是我需要将信息插入数据库表
的另一个代码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();
}
});
我已宣布likeDB
和dlikeDB
方法<{1}}
onCreate()
更新
我编辑了代码。我在帖子中删除了AnniversaryDBAdapter类的一些代码。
答案 0 :(得分:0)
你为什么感到惊讶?您正在insertLikes函数中的数据库中插入名称
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()