백그라운드에서 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;
}
}
그리고 로그 캣 :
> 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
나는 피드백을 얻을 때 , 내 데이터가 삽입되었습니다. 왜 테이블 안에 아무것도 없습니까? 내가 도대체 뭘 잘못하고있는 겁니까? 여기
는 ReadRssBackground 내가 전화를 내 BackgroundService입니다 : 일상 checkXML에서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;
}
}
title.db 또는 table.db? // db.insert()에 – Stefan
이 표시되면 -1을 얻으면 API에 명시된 바와 같이 오류가 발생합니다. https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert%28java .lang.String, % 20java.lang.String, % 20andandroid.content.ContentValues % 29. 실제로 문장을 바꾼다. 오류가 발생한다. – Opiatefuchs
'db.insert' 연산의 결과가'-1' 일 때'Vergleich insert :: DATA INSERTED'라는 메시지가 표시된다. 데이터가 삽입되지 않았습니다. 로그 고양이를 검사 할 때 오류가 있습니까? – ishmaelMakitla