2016-08-04 1 views
0

私はAndroidアプリケーションを作成しています。 ローカルファイル(HTML)をいくつか作成してWebViewに表示する必要がありますが、ローカルデータベースが必要です。 SqlLiteをWebViewに統合することはできますか?それとも、何か別のものを使うだけですか?おかげさまで 1.FirstすべてのWebViewのSqlLite

+0

いくつかのローカルファイルhtmlを格納するためにSQLiteを使用しますか? – sonnv1368

+0

いいえ、データを保存する必要があります。次に、HTMLファイルからデータを追加、編集、削除することができます。 –

答えて

0

は、そのクラスのSQLiteOpenHelper

2.Createに請負業者を拡張し、必要なすべての機能を実装したクラスを作ります。メソッドのonCreate((SQLiteDatabasedb)

ただ、例えば、それを文字列には、クエリと呼ば作成し、必要なテーブルを作成し、 ExecSQLメソッド:テーブルを存在しない場合は

文字列のクエリは= "テーブルを作成します(テキスト、テキスト、名前 テキスト2、_idテキスト)」; db.execSQL(クエリ);その後 の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] 
関連する問題