2012-04-15 83 views
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()); 
} 
} 

而這就是我卡住的地方!大聲笑!我可能偏離軌道。無論你有什麼建議,我都很好。謝謝!

回答

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

猜猜有什麼區別?適當的空間...

+0

我做了你所說的,我明白你的觀點。然而,現在我的查詢語法是正確的,我該如何去引用查詢的結果(我在考慮返回光標)到我的WebView中?因爲這是日食是說我沒有在任何地方使用「私人光標getEvents()」。 – Craig 2012-04-15 06:17:32

相關問題