- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里看到了很多类似的问题,但没有一个能解决我的问题。我正在制作一个将 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/
我正在尝试在 Android SQLite 数据库中创建一个表。但是 execSQLite 方法在 on create 方法中不起作用。它给了我信息- Non-static method 'execS
我编写了一个调度程序应用程序,当用户使用以下命令从互联网更新日程安排时,它会将日程安排数据添加到数据库中: sqLiteDatabase.execSQL("INSERT OR IGNORE INTO
我的 execSQL() 命令出现错误。 Eclipse 说我需要在我的 DBHelper 类中定义它。我该怎么做呢?我不熟悉使用 SQL 数据库及其命令。 这是我的类(class)代码; packa
我有以下 execSQL 抛出语法错误并导致我的应用程序崩溃。考虑到我已经转义了单引号和双引号,我不知道为什么。 代码如下: database.execSQL("insert into " + TAB
好吧,也许我只是不明白这一点,但是,当我尝试删除多个表时,比如 static final String DROP_TABLES = "DROP TABLE IF EXISTS ?,?,?,?"; 并尝
您好,我有这个函数可以使用 TUniQuery 更新我的 Access DB: var Res:Boolean; begin Res:=false; try with MyQuery do
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在尝试获取从 EditText 字段存储的一些值并将它们存储到我的 SQLLite 数据库中。 我有以下字符串变量: 网站名称_地址_用户_pass 这是我尝试使用的 Sql 命令: SQLite
我正在尝试使用 rawQuery 和 execSQL 方法来操作我的数据库,而不是 .update、.insert 等。我正在尝试使用以下代码执行更新: db.execSQL("UPDATE conf
我正在为Android 3.2版本编写应用程序,然后要求升级的ASUS Eee Pad TF01,这将Android版本更改为4.0.3。从那时起我就遇到了应用程序的问题,我将其追踪到 SQLite
为什么这不起作用?? db.execSQL("INSERT INTO PARTIES (PARTY_NAME, PARTY_COUNT) SELECT DISTINCT(PARTY), COUNT(P
我正在尝试制作使用SQLite数据库的简单注册/登录应用程序,到目前为止我得到了这段代码。这是我的“注册” Activity ,我猜它应该在按下注册按钮后将用户名和 pin(密码)实现到数据库,遗憾的
我在这里看到了很多类似的问题,但没有一个能解决我的问题。我正在制作一个将 XML 内容放入数据库的应用程序。一切工作正常,但是当我第二次打开应用程序时,会添加越来越多的值。我试图删除表,删除数据库,甚
我有这行代码: String insertSQL = "INSERT INTO " + BOOK_NAME + " (id, title, date, type, excerpt, c
这个问题在这里已经有了答案: Unfortunately MyApp has stopped. How can I solve this? (23 个回答) 关闭 7 年前。 这是我的 onCrea
我想有一个选项来调用“rawQuery”并传入一个整数列表,但看起来我不能:所有“rawQuery”方法都需要字符串数组... 为什么?我可以为每个传递的 int 对象调用“toString”,但我在
我按照标准教程使用 Android 构建数据库。我创建了一个名为 DbHelper 的类,它扩展了 SQLiteOpenHelper。我已经重写了创建处理程序来执行一个字符串。 @Override p
系统/版本信息: Windows Server 2019 数据中心 Ubuntu 16.04 Python 2.7 pip 20.1 我正在尝试使用以下命令安装最新版本的 execsql (1.72)
在 Delphi 中,每当我使用 TQuery 对数据库执行 SELECT 时,我都会在 Query.Open 后面加上 try..finally,并在finally 部分中使用 Query.Clos
我们如何获得受 FDQuery.ExecSQL 影响的计数对 SQLITE 数据库的 table1 执行 INSERT、UPDATE 或 DELETE 命令后 FireDAC 命令? FDQuery.
我是一名优秀的程序员,十分优秀!