如何在SQL数据库中存储Html文件目录?

时间:2013-12-10 08:28:34

标签: java android sql sqlite

我在assets文件夹中放置了几个html文件,我的目录在java文件中 但我想将directorys存储到SQLite数据库中,以“pos”作为主键。然后我只需要一行代码,例如

web.loadUrl(sqlDb.load("select url FROM mytable where pos = " + pos));打开html文件

但我不知道

  1. 如何在eclipse中创建SQL数据库
  2. 如何将我的活动引荐到数据库
  3. 如何在sql数据库中存储目录。
  4. 下面是我的代码

    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);
    
        }
     }
     }
    

2 个答案:

答案 0 :(得分:0)

关于问题1 - 2有2个选项,请阅读以下tutorial1tutorial2

关于问题3,据我所知,您的意思是保存这些文件的路径。我认为最好的方法是将这些pathes存储在一个表中SQLite字符串。

希望这就是你所需要的。

答案 1 :(得分:0)

<强> 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

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();
    }
}