如何使用新数据正确覆盖SQLite表行?

时间:2017-11-28 11:51:05

标签: android json sqlite android-sqlite

我制作了一个新闻应用程序,显示了10个最新的新闻标题。当API返回10个不同的新闻文章时,它们会被插入到表中而不会删除当前的新闻文章,导致每次数据量增加10个。

到目前为止,我已经尝试更新行,这些表在表已有数据时有效。我试图删除表并重新创建它,它什么也没显示......

我发现的唯一解决方案是将伪数据插入表格并用api中的数据覆盖它......最好的方法是什么?请参阅下面的代码。

dbHelper

public class dbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="mytube";
    private static final int DATABASE_VERSION = 1;
    private static final String NEWS_TABLE = "news";
    private static final String CREATE_TABLE = "create table "+NEWS_TABLE +"(title TEXT, desc TEXT, url TEXT)";

    Context context;

    public dbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + NEWS_TABLE);
        // Create tables (again)
        onCreate(db);
    }
    // Insert into DB

    public void insertIntoDB(String title,String desc,String url){
        Log.d("insert data", "");

        // get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();

        // Create ContentValues to add key
        ContentValues values = new ContentValues();
        values.put("title", title);
        values.put("desc", desc);
        values.put("url", url);


        // Insert Data
        db.insert(NEWS_TABLE, null, values);

        // Close DB
        db.close();
        Toast.makeText(context, "insert value", Toast.LENGTH_LONG);
        Log.i("", "Insert complete");
    }
    /* Retrive  data from database */
    public List<DatabaseModel> getDataFromDB(){
        List<DatabaseModel> modelList = new ArrayList<DatabaseModel>();
        String query = "select * from "+NEWS_TABLE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);

        if (cursor.moveToFirst()){
            do {
                DatabaseModel model = new DatabaseModel();
                model.setTitle(cursor.getString(0));
                model.setDesc(cursor.getString(1));
                model.setUrl(cursor.getString(2));

                modelList.add(model);
            }while (cursor.moveToNext());
        }


       // Log.d("news data", modelList.toString());


        return modelList;
    }




}

活动

public class journeyEntertainment extends AppCompatActivity {

    dbHelper helper;
    List<DatabaseModel> dbList;
    RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_journey_entertainment);
       /* Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/

        getNews();
        helper = new dbHelper(this);
        dbList= new ArrayList<DatabaseModel>();

        dbList = helper.getDataFromDB();

        mRecyclerView = (RecyclerView)findViewById(R.id.recycleview);

        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter (see also next example)
        mAdapter = new RecyclerAdapter(this,dbList);
        mRecyclerView.setAdapter(mAdapter);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_second, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void getNews(){
        RequestQueue queue = Volley.newRequestQueue(this);
        String url = "https://newsapi.org/v2/top-headlines?sources=bbc-news&apiKey=927e06d9dddb47ee929c9bb7597777055434";

        final StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("articles");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jo = jsonArray.getJSONObject(i);

                        String title = jo.getString("title");
                        String  desc = jo.getString("description");
                        String  url = jo.getString("url");

                        title = title.replaceAll("'", "");
                        desc = desc.replaceAll("'", "");
                        url = url.replaceAll("'", "");





                        helper = new dbHelper(journeyEntertainment.this);
                        helper.insertIntoDB(title, desc, url);


                    }

                } catch (JSONException e) {

                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }
        });

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    }
}

1 个答案:

答案 0 :(得分:1)

1)将以下方法添加到dbHelper(这将删除表中的所有行);

public void removeAllRowsFromNewsTable(){
    Log.d("insert data", "");

    // get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(NEWS_TABLE,null,null);
}

2)在你的getNews方法中修改: -

                    helper = new dbHelper(journeyEntertainment.this);
                    helper.insertIntoDB(title, desc, url);

来: -

                    helper = new dbHelper(journeyEntertainment.this);
                    helper.removeAllRowsFromNewsTable();
                    helper.insertIntoDB(title, desc, url);