2016-08-25 180 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