0

我遇到了麻烦SQLiteDatabase。我已经设法将数据存储到表中。不幸的是,我将 name 存储在 table 而不是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();


                }
            });

我已经声明likeDBdlikeDB上述onCreate()方法

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

更新
我已经编辑了代码。我在帖子中删除了 AnniversaryDBAdapter 类的一些代码。

4

1 回答 1

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()
于 2012-08-29T07:00:19.223 回答