1

我已经阅读了大量有关该主题的文章和书籍。在大多数情况下,对android开发有一个不错的掌握。但是我很难合并数据库。我有一个名为“图书馆”的数据库。在数据库中,我有一个名为“Books”的表。在“书籍”表中,我有 3 列“id、Book、Chapter”。我想引用基于 Book 字段的 Chapter 字段,例如..."Select Chapter FROM Books WHERE Book ="Genesis1";。Chapter 字段的内容是纯 html,所以我想保存我的输出查询一个字符串,然后将该字符串放入 webview,目的是让我可以创建一个搜索框或小部件,以使用户能够搜索所有书籍内容。这是我目前所拥有的:

常量.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";
}

图书馆数据.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());   
}
}

这就是我卡住的地方!哈哈!我可能已经偏离轨道了。我很乐意为您提供任何建议。谢谢!

4

1 回答 1

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

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

于 2012-04-15T04:47:31.193 回答