我尝试四处搜索,但找不到明确的解决方案。
我想要做的是从 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();
}
当我对documentbuilder进行输入流时,我尝试搜索与字节或字符串有关,但似乎这并不是真正的原因。下面是可以从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 = ?