如何将查询结果保存到单独的表中?

时间:2012-12-01 17:50:55

标签: android database sqlite save

我有一个数据库,允许用户查询数据库中的书籍。

我正在尝试将查询结果另存为“书签”。用户输入查询,然后可以单击结果以查看该特定书籍的详细信息。

然后有一个菜单选项,允许用户将查询“保存”到他们的书签。保存的查询将插入到数据库表中。书签是一种活动,它将保存的数据作为列表视图加载,允许用户管理其书签。

除了储蓄之外,一切都已经完成并且工作得非常好。在选择保存的菜单选项时,我不知道如何以及将查询结果导入数据库的最佳方式。

标准sqlite插件有效,但没有特定的查询结果数据。

部分代码:

package com.example.newdatabase;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class DetailView extends Activity
{
    private DBAdapter dbHelper;
    private SimpleCursorAdapter dataAdapter;
    private long rowid;//row for book from position in database
    public static final String BOOKM_ID = "rowid";

@Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.detailviewlist);

  rowid=getIntent().getExtras().getLong(TitleSearch.ROW_ID);//passed from TitleSearch pass the row id

        dbHelper = new DBAdapter(this);
        dbHelper.open();

        //loads data into list 
        displayListView();
   }

private void displayListView()
{
    Cursor cursor=dbHelper.getABOOK(rowid);//cursor and query method only shows one book

    //defines columns to bind
    String [] columns =new String[]
            {
                DBAdapter.KEY_TITLE,
                DBAdapter.KEY_AUTHOR,
                DBAdapter.KEY_EDITION,
                DBAdapter.KEY_PUBLISHER,
                DBAdapter.KEY_DESCRIPTION,
                DBAdapter.KEY_OPAC,
                DBAdapter.KEY_LOCATION,
                DBAdapter.KEY_CALL,
                DBAdapter.KEY_COPIES,
                DBAdapter.KEY_STATUS,

            };

    // the XML defined views which the data will be bound to
      int[] to = new int[] 
      {
        R.id.title,
        R.id.author,
        R.id.edition,
        R.id.publisher,
        R.id.description,
        R.id.subject,
        R.id.location,
        R.id.callnum,
        R.id.copies,
        R.id.status,
      };

    // create the adapter using the cursor pointing to the desired data
      //as well as the layout information
      dataAdapter = new SimpleCursorAdapter(
        this, R.layout.detailbook,
       cursor,
       columns,
        to,
        0);

     ListView listView = (ListView) findViewById(R.id.listView2);
      // Assign adapter to ListView
      listView.setAdapter(dataAdapter);

}
   @Override
    public boolean onCreateOptionsMenu(Menu menu) //save menu
    {
       super.onCreateOptionsMenu(menu);
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.savemenu, menu);
       return true;

    }



  /* @Override
    public boolean onOptionsItemSelected(MenuItem item) //lauches the bookmark when saved
    {
       Intent addContact = new Intent(DetailView.this, Bookmark.class);
       startActivity(addContact);
       return super.onOptionsItemSelected(item);
    }*/


   public boolean onOptionsItemSelected(MenuItem item)//menu action
    {

        switch (item.getItemId())
        {
        case R.id.addsave:
            // Single menu item is selected to save book info

            //adds info into bookmark on save click

            //this is where i am having an issue





            Toast.makeText(DetailView.this, "The book has been saved!", Toast.LENGTH_SHORT).show();//feedback for user
            return true;

        default:
            return super.onOptionsItemSelected(item);

        }

    } //end of options
    }//end of detail view

我正在尝试将getABOOK(rowid)查询的结果保存到Bookmarks使用的数据库表中。

除非有更好的方法来保存结果。

1 个答案:

答案 0 :(得分:0)

我建议在同一个表中添加一个名为Bookmark的新列,并在用户选择书签时将书签的时间戳添加到特定行。

现在,在书签视图中,您只需查询书签不为空的所有行。