简体   繁体   English

SQLite更新/替换表Android

[英]SQLite Update / Replace Table Android

I have an app that gets JSON data from a server. 我有一个从服务器获取JSON数据的应用程序。 I then put the parsed data into the android SQLite database and use the data as needed. 然后,我将解析的数据放入android SQLite数据库中,并根据需要使用数据。 This all works great, however, I am unable to find a method to update the whole table. 这一切都很好,但是,我找不到更新整个表的方法。 The scenario would be that this Json Data feed gets updated every week on the server. 场景是该Json数据提要每周在服务器上更新一次。 I have two Questions: 我有两个问题:

What am I missing or what is the method for updating the SQLite table? 我缺少什么或更新SQLite表的方法是什么? (currently this just duplicates the data) (当前这只是复制数据)

public void updateTable(Product product){
    SQLiteDatabase db = this.getWritableDatabase();
    try{
        ContentValues values = new ContentValues();
    values.put(KEY_TYPE_NAME, product.getmProductTypeName());
    // more columns here...
    db.update(TABLE_NAME, values, null,null);
    db.close();
    }catch(Exception e){
    Log.e("error:",e + "in updateData method")
    }

What is an ideal system for updating the data? 什么是理想的数据更新系统? Would it be silly and bad practice to just call the method when connected to internet? 仅在连接到Internet时调用该方法会是愚蠢且不好的做法吗?

Related Code in "Main Activity": “主要活动”中的相关代码:

    handler = new DBHandler(this);
    NetworkUtils utils = new NetworkUtils(MainActivity.this);
    if (handler.getProductCount() == 0 && utils.isConnectingToInternet()) {
        new JsonDataParse().execute();
    }`

Related Code "DBhandler" Activity: 相关代码“ DBhandler”活动:

    public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_TABLE);
    onCreate(db);
}

String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_TYPE_NAME + " TEXT" + ")"

That is basically my CREATE TABLE String format. 那基本上就是我的CREATE TABLE String格式。 I just condensed to because it has 16 columns. 我简而言之,因为它有16列。

This is the code I added to only delete the stored data only if there was data: 这是我添加的代码,仅当有数据时才删除存储的数据:

        if(handler.getProductCount() == 0) {

              }else{
                handler.deleteData();
            }

Then I just just added the delete the method as suggested: 然后,我只是按照建议添加了delete方法:

    public void deleteData() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME, "1", null);
}

I'm not sure what you mean by "update the whole table". 我不确定“更新整个表格”是什么意思。 It sounds to me like you just need to delete the data in the table and then use your current method to add the new data. 在我看来,您只需要删除表中的数据,然后使用当前方法添加新数据即可。 To delete the contents you can use: 要删除内容,可以使用:

db.delete(TABLE_NAME, "1", null);

Then call your existing method to re-populate the table from the server. 然后调用您现有的方法以从服务器重新填充表。

What is an ideal system for updating the data? 什么是理想的数据更新系统? Would it be silly and bad practice to just call the method when connected to internet? 仅在连接到Internet时调用该方法会是愚蠢且不好的做法吗?

No it wouldn't be bad practice. 不,这不是一个坏习惯。 That makes sense, as you'll only be able to reach the server if you're connected to the internet anyway. 这是有道理的,因为无论如何您都只能连接到Internet才能访问服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM