【问题标题】:Access SQLite database from WebView从 WebView 访问 SQLite 数据库
【发布时间】:2016-12-10 19:56:53
【问题描述】:

我正在开发一个 Android 应用程序,我的计划是向用户展示一个 WebView,因为用 HTML 编写代码对我来说更容易。这个应用程序应该维护一些信息。

理论上,我应该使用本机方法将我的应用程序连接到 SQLite 数据库。由于我想使用 WebView,我正在寻找一些方法来直接从 WebView 访问数据库。

是否可以从 WebView 访问 SQLite 数据库?是否有任何替代方法可以用来创建使用 HTML 的应用程序?

【问题讨论】:

  • 你想用SQLite来存储一些本地文件html吗?
  • 不,我必须存储一些数据。然后我应该能够从 HTML 文件中添加、编辑和删除数据。

标签: javascript java android sqlite webview


【解决方案1】:

1.首先制作扩展SQLiteOpenHelper的类

2.为该类创建一个承包商并实现所需的所有功能。

关于方法 onCreate((SQLiteDatabasedb)

只需创建名为查询的字符串并创建您想要的表,然后 execSql吧,例如:

String query="如果不存在表创建表(文本文本,名称 text2,_id text)"; db.execSQL(query); 然后在 sql lite 使用:

public void add_new_row(String text,String text2,String _id){ try{
ContentValues cn=new ContentValues();  cn.put("text",per_name); 
cn.put("text2",per_age);       cn.put("_id",per_age);      

SQLiteDatabase db;      db=getWritableDatabase();   
db.insert("table",null,cn);} catch(Exception e){
Log.i("AddSql_lite","Print :"+e.to );

 } And for read Row from sqllite you just use :

public String getRow_by_id(String _id){ string text = ""; try{
   db=getWritableDatabase(); query="select * from table where _id =
   '"+_id+"'";

     Cursor cr=db.rawQuery(query, null);
     if(cr.moveToFirst()){
         do{
             text = cr.getString(0);
             String text2 = cr.getString(1);
             String _id = cr.getString(2);
         }while(cr.moveToNext());
     }

 }catch(Exception e){
 }

 return text; } And for reading all the rows in the table one after another :

public int get_all_Rows(){ string int counter = 0; try{
db=getWritableDatabase(); query="select * from table";

     Cursor cr=db.rawQuery(query, null);
     if(cr.moveToFirst()){
         do{
             text = cr.getString(0);
             String text2 = cr.getString(1);
             String _id = cr.getString(2);
             counter++;
         }while(cr.moveToNext());
     }

 }catch(Exception e){
 }

 return counter; } Hope its will help you to understand how Sqllite work and how build it correctly :)

为了让它运行得这么简单:

SQLiteDataBase Sqllite_ref = new SQLiteDataBase(context, "VerName",
null, 1);

Sqllite_ref.add_new_row("hey","hey4","1");
Sqllite_ref.add_new_row("hey2","hey5","2");
Sqllite_ref.add_new_row("hey3","hey6","3");

Sqllite_ref.getRow_by_id("2");>>> will return "hey2"
Sqllite_ref.get_all_Rows()>>> will return 3 [number of items you have
in the sqllite]

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 2014-06-02
    • 2011-07-21
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2011-10-04
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多