[英]How To Store Html Files Directory in SQL Database?
I have few html files placed in the assets folder and i have their directory in the java file but i want to store the directorys into the SQLite database, with "pos" as the primary key. 我在Assets文件夹中放置了一些html文件,并且在java文件中有其目录,但是我想将目录存储到SQLite数据库中,并以“ pos”作为主键。 Then i'll need only a single line of code, ie something like
然后,我只需要一行代码,即像
web.loadUrl(sqlDb.load("select url FROM mytable where pos = " + pos));
to open the html files 打开html文件
but i dont know 但我不知道
below is my code 下面是我的代码
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
web = (WebView) findViewById(R.id.webView1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key",0);
if(pos==0){ web.loadUrl("file:///android_asset/1.html");
else if(pos==1){ web.loadUrl("file:///android_asset/2.html");}
else if(pos==2){ web.loadUrl("file:///android_asset/3.html");}
else if(pos==3){ web.loadUrl("file:///android_asset/4.html");}
else if(pos==4){ web.loadUrl("file:///android_asset/5.html");}
// similarly for 4 and 5 and so on.
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
}
About questions 1 - 2 there is 2 options read the following tutorial1 and tutorial2 关于问题1-2,有2个选项,请阅读以下tutorial1和tutorial2
About question 3 as I understand you mean to save the path to these files. 据我了解,关于问题3,您的意思是保存这些文件的路径。 I think the best way is to store these pathes in one of your tables as SQLite string.
我认为最好的方法是将这些路径作为SQLite字符串存储在您的表中。
Hope that this is what you need. 希望这就是您所需要的。
DatabaseHelper.java DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper{
// Database Version
private static final int VERSION = 1;
// Database Name
private static final String NAME = "MyDatabase.db";
// SqliteDatabase
SQLiteDatabase db = null;
public DatabaseHelper(Context context) {
super(context, NAME, null, VERSION);
}
public void open() throws SQLException {
db = this.getWritableDatabase();
}
public void close() {
db.close();
}
//table names
public static final String TABLE_URLS = "TABLE_URLS";
// table schema
private static final String SCHEMA_TABLE_URLS = "CREATE TABLE "+ TABLE_URLS +" (POS INTEGER PRIMARY KEY, URL TEXT);";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SCHEMA_TABLE_URLS);
System.out.println("database created.");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SCHEMA_TABLE_URLS);
System.out.println("database is updated");
}
public long insertURL(ContentValues values){
return db.insert(TABLE_URLS, null, values);
}
public Cursor getURL(String pos){
return db.query(TABLE_URLS, null, "POS =?", new String[]{pos}, null, null, null);
}
}
MainActivity.java MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create database helper object
DatabaseHelper helper = new DatabaseHelper(this);
helper.open();
// insert url to database
ContentValues values;
values = new ContentValues();
values.put("POS", "1");
values.put("URL", "file:///android_asset/1.html");
helper.insertURL(values);
values = new ContentValues();
values.put("POS", "2");
values.put("URL", "file:///android_asset/2.html");
helper.insertURL(values);
values = new ContentValues();
values.put("POS", "3");
values.put("URL", "file:///android_asset/3.html");
helper.insertURL(values);
// so on
// get url using position
String pos = "1";
Cursor c = helper.getURL(pos);
c.moveToFirst();
String url = c.getString(c.getColumnIndex("URL"));
System.out.println("URL "+url);
c.close();
helper.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.