我尝试四处寻找,但找不到明确的解决方案。
我要做的是从URL下载一个始终更新的XML格式的新输入SQL文件,并将其保存在/ data / data / appprogram / files /文件夹中。完成文件下载,并通过DDMS文件浏览器将测试确认文件下载到模拟器中。
接下来,我想打开此文件以获取所有语句并将其解析为定义为camera_database的数据库,该数据库位于/ data / data / appprogram / databases中。这是问题的开始。我无法超越这一点。
我尝试使用inputstream和openfileinput输入数据但不能正常工作。
以下是我使用的代码。请从任何灵魂中劝告..
public class Help_DatabaseHelperAdv extends SQLiteOpenHelper {
public static final String CAMERA_DATABASE_NAME = "camera_database";
protected Context camera_context;
public Help_DatabaseHelperAdv(Context context) {
super(context, CAMERA_DATABASE_NAME, null, 1);
this.camera_context = context;
}
@Override
public void onCreate(SQLiteDatabase cameradb) {
String s;
try {
InputStream in = camera_context.getApplicationContext().openFileInput("/data/data/appprogram/files/alladvertlist.xml");
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList statements = doc.getElementsByTagName("statement");
for (int i=0; i<statements.getLength(); i++) {
s = statements.item(i).getChildNodes().item(0).getNodeValue();
cameradb.execSQL(s);
}
} catch (Throwable t) {
Toast.makeText(camera_context, t.toString(), 50000).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase cameradb, int oldVersion, int newVersion) {
cameradb.execSQL("DROP TABLE IF EXISTS alladvert");
onCreate(cameradb);
}
}
感谢您的回复。希望添加,我做一些测试,以确保我的文件可以输入/ databases /文件夹。我在启动时将alladvertlist.xml放在res / raw文件夹中,使用
执行输入流InputStream in = camera_context.getResources().openRawResource(R.raw.alladvertlist);
工作正常,我可以使用上面的命令从/ databases / camera_database检索SQL数据。
但是当我将文件从url下载到特定文件夹/data/data/appprogram/alladvertlist.xml并使用inputstream将文件提取到documentbuilder时,它无法将文件解析为/ databases / camera_database。
下面是从URL下载文件的代码,并保存在一个工作正常的特定文件夹中。如果您觉得有用,请供所有人参考和使用。
/** Download a specified file from the web */
public void updateadvData(){
final String file_url = this.getString(R.string.alladvertlisturl);
/* Define and configure the progress dialog */
final ProgressDialog myProgress = new ProgressDialog(this);
myProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myProgress.setMessage(this.getString(R.string.dialog_text));
myProgress.setTitle(R.string.dialog_title);
/* Show the progress dialog. */
myProgress.show();
/* Download our file */
new Thread(new Runnable(){
public void run(){
try {
//create the new connection
URL url = new URL(file_url);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
File parentDirectory = new File("/data/data/appprogram/files");
if(!parentDirectory.exists())
{
System.err.println("It seems like parent directory does not exist...");
if(!parentDirectory.mkdirs())
{
System.err.println("And we cannot create it...");
// we have to return, throw or something else
}
}
// File file = new File(SDCardRoot,"filename.sqlite");
File file = new File(parentDirectory, "alladvertlist.xml");
if(file.exists())
{
System.err.println("Alladvertlist deleting...");
file.delete();
}
file.createNewFile();
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
myProgress.setMax(totalSize);
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
int progress = 0;
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//Here we update the progress
progress = downloadedSize;
myProgress.setProgress(progress);
}
//close the output stream when done
fileOutput.close();
myProgress.dismiss();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
我尝试搜索它与字节或字符串,当我对文档构建器输入流,但似乎它不是真正的原因。下面是从url下载的SQL文件的结构,消除了内容
<sql>
<statement> .... </statement>
</sql>
05-15 01:51:13.347: E/SQLiteLog(1391): (1) no such table: alladvert
05-15 01:51:13.347: D/AndroidRuntime(1391): Shutting down VM
05-15 01:51:13.347: W/dalvikvm(1391): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-15 01:51:13.377: E/AndroidRuntime(1391): FATAL EXCEPTION: main
05-15 01:51:13.377: E/AndroidRuntime(1391): java.lang.RuntimeException: Unable to start activity ComponentInfo{advertisebiz.gadgetscan/advertisebiz.gadgetscan.AdvPage02}: android.database.sqlite.SQLiteException: no such table: alladvert (code 1): , while compiling: SELECT _id, advcategory FROM alladvert WHERE advcategory = ?