Koss
Koss

Reputation: 124

SQLite Database does not update data

I'm trying to insert some Data using AsyncTask in background. My logcat is telling me that everything is going well and data is inserted. But when i look inside the table.db there is no data inside. I'm using a emulator to see the table.db.

Here is my AsyncTask class:

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;
    }
}

}

And here my 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;
    }
}

And the 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

When i get the feedback, that my data is inserted, why is nothing inside the table? What am i doing wrong?

Here is my BackgroundService where i call 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;
    }
}

Upvotes: 2

Views: 750

Answers (1)

Salif
Salif

Reputation: 1192

In your routine checkXML(GetData()); you are not using the parameter data

public void checkXML(Document data) {

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

            }

In your constructor you don't have attribute parameters :

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

Replace it with :

 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;
        }

have you already try this :

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

Upvotes: 1

Related Questions