gpt4 book ai didi

java - execSQL 仅显示 XML 的最后一行

转载 作者:行者123 更新时间:2023-12-01 12:02:30 24 4
gpt4 key购买 nike

我在这里看到了很多类似的问题,但没有一个能解决我的问题。我正在制作一个将 XML 内容放入数据库的应用程序。一切工作正常,但是当我第二次打开应用程序时,会添加越来越多的值。我试图删除表,删除数据库,甚至删除文件,但这些都不起作用。现在我添加了这段代码,应用程序只将 xml 的最后一行解析到数据库,我真的不知道为什么会发生这种情况。

myDatabase.execSQL("DROP TABLE " + DATABASE_TABLE);
myDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_LINK + " TEXT NOT NULL);"
);

完整代码:

XML 帮助器:

package com.example.partedoxml;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class XMLHelper extends DefaultHandler {
private String URL_MAIN = "http://he4dless.webege.com/packages.xml";
String TAG = "XMLHelper";

Boolean currTag = false;
String currTagVal = "";
public PostValue menes = null;
public ArrayList<PostValue> he4dless = new ArrayList<PostValue>();

public void get() {
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser mSaxParser = factory.newSAXParser();
XMLReader mXmlReader = mSaxParser.getXMLReader();
mXmlReader.setContentHandler(this);
InputStream mInputStream = new URL(URL_MAIN).openStream();
mXmlReader.parse(new InputSource(mInputStream));



}catch(Exception e){
Log.e(TAG, "Exeption:"+e.getMessage());


}




}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(currTag){
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
}
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currTag = false;

//if(localName.equalsIgnoreCase("id"))
//packages.setId(currTagVal);

if(localName.equalsIgnoreCase("name"))
menes.setName(currTagVal);

else if(localName.equalsIgnoreCase("link"))
menes.setLink(currTagVal);

else if(localName.equalsIgnoreCase("menes"))
he4dless.add(menes);
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i(TAG, "TAG:"+localName);

currTag = true;
currTagVal = "";

if(localName.equals("menes"))
menes = new PostValue();

}



}

SQL 帮助器:

package com.example.partedoxml;

import java.io.File;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLHelper {

public static final String DATABASE_NAME = "he4dless";

public static final String DATABASE_TABLE = "menes";
public static final int DATABASE_VERSION = 1;

public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_LINK = "link";
public static final String TAG_1 = "tag1";
public static final String TAG_2 = "tag2";
public static final String TAG_3 = "tag3";

private DbHelper myHelper;
private Context myContext;
private SQLiteDatabase myDatabase;



private static class DbHelper extends SQLiteOpenHelper {


public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {


db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_LINK + " TEXT NOT NULL);"
//TAG_1 + " TEXT NOT NULL, " +
//TAG_2 + " TEXT NOT NULL, " +
//TAG_3 + " TEXT NOT NULL);"
);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);

}





}
public SQLHelper(Context c){
myContext = c;
}
public SQLHelper open(){
myHelper = new DbHelper(myContext);
myDatabase = myHelper.getWritableDatabase();
myDatabase.execSQL("DROP TABLE " + DATABASE_TABLE);
myDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_LINK + " TEXT NOT NULL);"
);







return this;
}
public void close(){
myHelper.close();
}
public long create(String name, String link) {
ContentValues cv = new ContentValues();


cv.put(KEY_NAME, name);
cv.put(KEY_LINK, link);

return myDatabase.insert(DATABASE_TABLE, null, cv);

}

}

主要 Activity :

package com.example.partedoxml;

import android.support.v7.app.ActionBarActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


tv = (TextView)findViewById(R.id.tv);
new PostAsync().execute();
}

class PostAsync extends AsyncTask<Void, Void, Void>{
ProgressDialog pd;
XMLHelper helper;

@Override
protected void onPreExecute() {
pd = ProgressDialog.show(MainActivity.this, "Esta porra esta carregando", "Baixando o XML", true, false);

}

@Override
protected Void doInBackground(Void... params) {
helper = new XMLHelper();
helper.get();

return null;
}

@Override
protected void onPostExecute(Void result) {
StringBuilder builder = new StringBuilder();
for(PostValue post : helper.he4dless){
//builder.append("\nId: "+ post.getId());

builder.append("\nName: "+ post.getName());

builder.append("\nSection: "+ post.getLink());
builder.append("\n");


String name = post.getName();
String link = post.getLink();

SQLHelper entry = new SQLHelper(MainActivity.this);

entry.open();

entry.create(name, link);
entry.close();


}
tv.setText(builder.toString());
pd.dismiss();
}



}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

如果有人解决这个问题我会很高兴

最佳答案

您的 open() 删除并重新创建表,然后在每次 for 循环迭代中重新打开数据库。

将架构设置留给辅助回调,例如 onCreate()。此外,您不需要在循环内重新打开数据库。

关于java - execSQL 仅显示 XML 的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27892808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com