如何查询android sqlite数据库并将结果存储在可以在webview中显示的字符串中?

时间:2012-04-15 03:31:23

标签: android html sqlite webview cursor

我读过很多关于这个主题的文章和书籍。在大多数情况下,对android开发有一个很好的把握。但是我很难整合数据库。我有一个名为“Library”的数据库。在数据库中,我有一个名为“Books”的表。在“书籍”表中,我有3列“id,Book,Chapter”。我想引用基于Book字段的Chapter字段,例如......“选择章节FROM Books WHERE Book =”Genesis1“;。Chapter字段的内容是纯HTML,所以我想保存我的输出查询字符串,然后将该字符串放入webview,目的是使我可以创建搜索框或小部件,以便用户搜索所有图书内容。这是我到目前为止所拥有的:

Constants.java

package watchtower.library.org;

import android.provider.BaseColumns;

public interface Constants extends BaseColumns {
public static final String TABLE_NAME = "Books";

public static final String Book = "book";
public static final String Chapter = "chapter";
}

LibraryData.java

package watchtower.library.org;

import static android.provider.BaseColumns._ID;
import static watchtower.library.org.Constants.TABLE_NAME;
import static watchtower.library.org.Constants.Book;
import static watchtower.library.org.Constants.Chapter;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class LibraryData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "WatchtowerLibrary.db";
private static final int DATABASE_VERSION = 1;

public LibraryData(Context ctx){
    super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME +"( +_ID" 
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Book
            + " INTEGER," + Chapter + " TEXT NOT NULL);");

}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}
}

Genesis1.java

package watchtower.library.org;

import static watchtower.library.org.Constants.TABLE_NAME;
import static watchtower.library.org.Constants.Chapter;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Genesis1 extends Activity {
private LibraryData WatchtowerLibrary;
private Cursor getEvents() {
    SQLiteDatabase db = WatchtowerLibrary.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT Chapter" + "FROM" + TABLE_NAME + "WHERE                                                                                                              
Book is Genesis1", null); 
    startManagingCursor(cursor);
    return cursor;
}
private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scriptures);

    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.setWebChromeClient(new WebChromeClient());   
}
}

那就是我被困的地方!大声笑!我可能偏离轨道。无论你有什么建议,我都很满意。谢谢!

1 个答案:

答案 0 :(得分:0)

这部分代码:

Cursor cursor = db.rawQuery("SELECT Chapter" + "FROM" + TABLE_NAME + 
"WHERE  Book is Genesis1", null);

需要像这样

Cursor cursor = db.rawQuery("SELECT Chapter FROM " + TABLE_NAME + " WHERE                 
Book is Genesis1", null);

猜猜有什么区别?适当的空间...