1
我已经阅读了大量关于这个主题的文章和书籍。大多数情况下,对Android开发有一个体面的把握。但是我很难整合一个数据库。我有一个名为“图书馆”的数据库。在数据库中我有一个名为“书籍”的表格。在“书籍”表中,我有3列“id,Book,Chapter”。我想引用基于书籍字段的章节字段,例如...“选择章节从书籍WHERE书=”Genesis1“;。章节字段的内容是纯html,所以我想保存我的输出查询一个字符串,然后把这个字符串放到一个webview中,目的是让我可以创建一个搜索框或者小部件来让用户搜索所有的图书内容。下面是我到目前为止的:如何查询android sqlite数据库并将结果存储在可以在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";
}
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());
}
}
而这就是我卡住的地方!大声笑!我可能偏离轨道。无论你有什么建议,我都很好。谢谢!
我做了你所说的,我明白你的观点。然而,现在我的查询语法是正确的,我该如何去引用查询的结果(我在考虑返回光标)到我的WebView中?因为这是日食是说我没有在任何地方使用“私人光标getEvents()”。 – Craig 2012-04-15 06:17:32