2016-08-25 178 views
2

我想在後臺插入一些使用AsyncTask的數據。我的logcat告訴我,一切進展順利,數據被插入。但是當我查看table.db時,裏面沒有數據。我正在使用模擬器來查看table.db。SQLite數據庫不會更新數據

這裏是我的AsyncTask類:

public class ReadRssBackground extends AsyncTask<Context, Void, Void> { 
    DatabaseHelper myDB; 
    Context ctx; 
    String id, title, link, category; 

    public ReadRssBackground(Context context) { 
     ctx = context.getApplicationContext(); 
    } 

    @Override 
    protected Void doInBackground(Context... contexts) { 
     myDB = new DatabaseHelper(ctx); 
     checkXML(GetData()); 
     return null; 
    } 
public void checkXML(Document data) { 
    if (data != null) { 
     Element root = data.getDocumentElement(); 
     Node channel = root.getChildNodes().item(1); 
     NodeList items = channel.getChildNodes(); 

     for (int i = 0; i < items.getLength(); i++) { 
      Node currentChild = items.item(i); 

      if (currentChild.getNodeName().equalsIgnoreCase("item") && count < 5) { 
       count++; 
       NodeList itemChilds = currentChild.getChildNodes(); 

       for (int j = 0; j < itemChilds.getLength(); j++) { 
        Node current = itemChilds.item(j); 

        if (current.getNodeName().equalsIgnoreCase("title")) { 
         title = current.getTextContent(); 
         Log.d("Vergleich Title", current.getTextContent()); 
        } else if (current.getNodeName().equalsIgnoreCase("link")) { 
         link = current.getTextContent(); 
         Log.d("Vergleich Link", current.getTextContent()); 
        } else if (current.getNodeName().equalsIgnoreCase("category")) { 
         category = current.getTextContent(); 
         Log.d("Vergleich Category", current.getTextContent()); 
        } 
       } 
    myDB.insertData(String.valueOf(count),title,link,category); 
      } 
     } 
    } 
} 

public Document GetData() { 
    try { 
     url = new URL(adress); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET"); 
     InputStream inputStream = connection.getInputStream(); 
     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = builderFactory.newDocumentBuilder(); 
     Document xmlDoc = builder.parse(inputStream); 
     return xmlDoc; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

}

在這裏,我DatabaseHelper:

public class DatabaseHelper extends SQLiteOpenHelper { 
    private final Context myContext; 
public static final String DATABASE_NAME = "title.db"; 
public static final String TABLE_NAME = "feed_table"; 
public static final String COL_1 = "ID"; 
public static final String COL_2 = "TITLE"; 
public static final String COL_3 = "LINK"; 
public static final String COL_4 = "CATEGORY"; 


public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, 1); 
    this.myContext = context; 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,TITLE TEXT,LINK TEXT)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    onCreate(db); 
} 


public boolean insertData(String id, String title, String link, String category) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_1, id); 
    contentValues.put(COL_2, title); 
    contentValues.put(COL_3, link); 
    contentValues.put(COL_4, category); 
    Log.d("Vergleich insert check:", contentValues.toString()); 
    long result = db.insert(TABLE_NAME, null, contentValues); 

    if (result == (-1)) { 
     Log.d("Vergleich insert:", "DATA INSERTED"); 
     return false; 
    } else { 
     Log.d("Vergleich insert:", "NOT"); 
     return true; 
    } 
} 

而且logcat的:

> 08-25 04:48:00.102 3244-3309/? D/Vergleich Title: Anhörung in 
> Neuseeland: Kim Dotcom wehrt sich gegen Auslieferung 08-25 
> 04:48:00.102 3244-3309/? D/Vergleich Link: 
> http://www.spiegel.de/netzwelt/web/anhoerung-in-neuseeland-dotcom-wehrt-sich-gegen-auslieferung-a-1109396.html#ref=rss 
> 08-25 04:48:00.102 3244-3309/? D/Vergleich Category: Netzwelt 08-25 
> 04:48:00.102 3244-3309/? D/Vergleich insert check:: ID=1 
> CATEGORY=Netzwelt 
> LINK=http://www.spiegel.de/netzwelt/web/anhoerung-in-neuseeland-dotcom-wehrt-sich-gegen-auslieferung-a-1109396.html#ref=rss 
> TITLE=Anhörung in Neuseeland: Kim Dotcom wehrt sich gegen Auslieferung 
> 08-25 04:48:00.102 3244-3309/? D/Vergleich insert:: DATA INSERTED 
> 08-25 04:48:00.102 3244-3309/? D/Vergleich Title: Paisley Park: 
> Studios von Prince bald offen für Besucher  08-25 04:48:00.102 
> 3244-3309/? D/Vergleich Link: 
> http://www.spiegel.de/kultur/musik/paisley-park-studios-von-prince-bald-offen-fuer-besucher-a-1109401.html#ref=rss 
> 08-25 04:48:00.102 3244-3309/? D/Vergleich Category: Kultur 08-25 
> 04:48:00.102 3244-3309/? D/Vergleich insert check:: ID=2 
> CATEGORY=Kultur 
> LINK=http://www.spiegel.de/kultur/musik/paisley-park-studios-von-prince-bald-offen-fuer-besucher-a-1109401.html#ref=rss 
> TITLE=Paisley Park: Studios von Prince bald offen für Besucher  08-25 
> 04:48:00.102 3244-3309/? D/Vergleich insert:: DATA INSERTED 

當我得到的反饋,我的數據被插入,爲什麼表裏面什麼都沒有?我究竟做錯了什麼?

這裏是我的BackgroundService在那裏我打電話ReadRssBackground:

public class BackgroundService extends Service { 
    private boolean isRunning; 
    private Context context; 
    private Thread backgroundThread; 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     this.context=this; 
     this.isRunning=false; 
     this.backgroundThread = new Thread(myTask); 
     super.onCreate(); 
    } 

    private Runnable myTask = new Runnable() { 
     @Override 
     public void run() { 
      //do something in Background 
      ReadRssBackground readRss = new ReadRssBackground(context); 
      readRss.execute(); 

      stopSelf(); 
     } 
    }; 

    @Override 
    public void onDestroy() { 
     this.isRunning=false; 
     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     if(!this.isRunning){ 
      this.isRunning=true; 
      this.backgroundThread.start(); 
     } 
     return START_STICKY; 
    } 
} 
+0

title.db or table.db? – Stefan

+0

on'db.insert()'如果得到-1,則發生錯誤,如API中所述:https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert%28java .lang.String,%20java.lang.String,%20android.content.ContentValues%29。你切換語句,實際上你得到一個錯誤 – Opiatefuchs

+0

當'db.insert'操作的結果是'-1'時,你顯示'Vergleich insert :: DATA INSERTED'消息 - 這意味着你看到這條消息,因爲數據未被插入。你在檢查你的日誌貓時是否有任何錯誤? – ishmaelMakitla

回答

1

在你的日常checkXML(的GetData());不使用參數數據

public void checkXML(Document data) { 

       myDB.insertData(id,title,link,category); 

      } 

在你的構造你沒有屬性參數:

public ReadRssBackground(Context context) { 
    ctx = context.getApplicationContext(); 
} 

將其替換爲:

public ReadRssBackground(Context context, String id, String title, String link, String category) { 
      this.ctx = context; 
      this.id = id; 
      this.title = title; 
      this.link = link; 
      this.category = category; 
     } 

有你已經嘗試這個辦法:

@Override 
protected Void doInBackground(Context... contexts) { 
    myDB = new DatabaseHelper(ctx); 
    myDB.insertData(id,title,link,category); 
    return null; 
} 
+0

謝謝你的幫助。我上面的代碼只是我整個代碼的一部分。不關心參數數據。我希望在checkXML()中插入任何數據,比如myDB.insertData(「id」,「title」,「link」,「category」);我嘗試了你所說的,但我仍然無法插入任何數據。 – Koss

+0

是的,但你需要像這樣調用你的構造函數:ReadRssBackground readRssBackground = new ReadRssBackground(context,「id here」,「title here」,「link here」,「category here」); – Seelass

+0

仍然一樣。我編輯了上面的代碼,並用Document GetData()粘貼了整個checkXML()。還有我稱之爲ReadRssBackground的後臺服務。無論如何, – Koss