SQLite数据库不会更新数据

时间:2016-08-25 08:50:53

标签: android android-asynctask android-sqlite

我试图在后台使用AsyncTask插入一些数据。我的logcat告诉我一切进展顺利,数据已插入。但当我查看table.db时,里面没有数据。我正在使用模拟器来查看table.db。

这是我的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;
    }
}

1 个答案:

答案 0 :(得分: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;
}