2013-11-15 27 views
0

我遇到了一個問題,讓我的SQLite在我正在開發的這個Android應用程序中正常工作。看起來好像表格沒有根據LogCat消息正確創建。我覺得我一直在盯着屏幕太久。任何人都可以在我的代碼中發現問題嗎?遇到問題獲取SQLite在我的Android應用程序中工作

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DBHelper extends SQLiteOpenHelper { 

    // table name 
    public static final String TABLE_JOKES = "jokes"; 

    // database field names 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_CATEGORY = "category"; 
    public static final String COLUMN_SUBCATEGORY = "subcategory"; 
    public static final String COLUMN_JOKE_TYPE = "jokeType"; 
    public static final String COLUMN_DESCRIPTION = "description"; 
    public static final String COLUMN_QUESTION_TEXT = "questionText"; 
    public static final String COLUMN_ANSWER_TEXT = "answerText"; 
    public static final String COLUMN_MONOLOGUE_TEXT = "monologueText"; 
    public static final String COLUMN_RATING_SCALE = "ratingScale"; 
    public static final String COLUMN_COMMENTS = "comments"; 
    public static final String COLUMN_JOKE_SOURCE = "jokeSource"; 
    public static final String COLUMN_RELEASE_STATUS = "releaseStatus"; 
    public static final String COLUMN_CREATED = "created"; 
    public static final String COLUMN_MODIFIED = "modified"; 

    private static final String DATABASE_NAME = "jokes.db"; // file name 
    private static final int DATABASE_VERSION = 1; 

    // Database creation raw SQL statement 
    private static final String DATABASE_CREATE = "create table " + TABLE_JOKES 
      + "(" + COLUMN_ID + " integer primary key autoincrement, " 
      + COLUMN_CATEGORY + " text not null, " + COLUMN_SUBCATEGORY 
      + " text not null, " + COLUMN_JOKE_TYPE + " integer not null, " 
      + COLUMN_DESCRIPTION + " text," + COLUMN_QUESTION_TEXT + "text," 
      + COLUMN_ANSWER_TEXT + "text," + COLUMN_MONOLOGUE_TEXT + "text," 
      + COLUMN_RATING_SCALE + "integer," + COLUMN_COMMENTS + "text," 
      + COLUMN_JOKE_SOURCE + "text," + COLUMN_RELEASE_STATUS + "integer," 
      + COLUMN_CREATED + "text," + COLUMN_MODIFIED + "text" + ");"; 

    // static instance to share DBHelper 
    private static DBHelper dbHelper = null; 
    private SQLiteDatabase db ; 

    /** 
    * Constructor 
    * 
    * @param context 
    * @param name 
    * @param factory 
    * @param version 
    */ 
    public DBHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     db = getWritableDatabase(); 
    } 

    /** 
    * This is a static method that makes sure that only one database helper 
    * exists across the app's lifecycle 
    * 
    * @param context 
    * @return 
    */ 
    public static DBHelper getDBHelper(Context context) { 
     if (dbHelper == null) { 
      dbHelper = new DBHelper(context.getApplicationContext()); 
     } 
     return dbHelper; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see 
    * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite 
    * .SQLiteDatabase) 
    */ 
    @Override 
    public void onCreate(SQLiteDatabase database) { 
     database.execSQL(DATABASE_CREATE); 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see 
    * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite 
    * .SQLiteDatabase, int, int) 
    */ 
    @Override 
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 
     Log.w(DBHelper.class.getName(), "Upgrading database from version " 
       + oldVersion + " to " + newVersion); 
    } 

    /** 
    * CRUD - Update 
    * 
    * @param category 
    * @param subcategory 
    * @param jokeType 
    * @param description 
    * @param questionText 
    * @param answerText 
    * @param monologueText 
    * @param comments 
    * @param jokeSource 
    * @param ratingScale 
    * @param releaseStatus 
    * @return 
    */ 
    public long insertNewJoke(String category, String subcategory, 
      int jokeType, String description, String questionText, 
      String answerText, String monologueText, String comments, 
      String jokeSource, int ratingScale, int releaseStatus) { 
     ContentValues cv = new ContentValues(); 
     cv.put(COLUMN_CATEGORY, category); 
     cv.put(COLUMN_SUBCATEGORY, subcategory); 
     cv.put(COLUMN_JOKE_TYPE, jokeType); 
     cv.put(COLUMN_DESCRIPTION, description); 
     cv.put(COLUMN_QUESTION_TEXT, questionText); 
     cv.put(COLUMN_ANSWER_TEXT, answerText); 
     cv.put(COLUMN_MONOLOGUE_TEXT, monologueText); 
     cv.put(COLUMN_COMMENTS, comments); 
     cv.put(COLUMN_JOKE_SOURCE, jokeSource); 
     cv.put(COLUMN_RATING_SCALE, ratingScale); 
     cv.put(COLUMN_RELEASE_STATUS, releaseStatus); 
     cv.put(COLUMN_CREATED, ""); // how to put date?? 
     cv.put(COLUMN_CREATED, ""); 

     long code = getWritableDatabase().insert(TABLE_JOKES, null, cv); 
     return code; 
    } 

    /** 
    * CRUD - Retrieve 
    * 
    * @return 
    */ 
    public Cursor getJokes() { 
     String[] columns = { COLUMN_CATEGORY, COLUMN_SUBCATEGORY, 
       COLUMN_JOKE_TYPE, COLUMN_DESCRIPTION, COLUMN_ANSWER_TEXT, 
       COLUMN_MONOLOGUE_TEXT, COLUMN_RATING_SCALE, COLUMN_COMMENTS, 
       COLUMN_JOKE_SOURCE, COLUMN_RELEASE_STATUS, COLUMN_CREATED, 
       COLUMN_MODIFIED }; // might need the _id column 
     return getWritableDatabase().query(TABLE_JOKES, columns, null, null, 
       null, null, null); 
    } 

    /** 
    * CRUD - Delete 
    * 
    * @param id 
    */ 
    public void deleteJoke(int id) { 
     getWritableDatabase().delete(TABLE_JOKES, COLUMN_ID + "=?", 
       new String[] { String.valueOf(id) }); 
    } 

} 

所以的onCreate()(在我的MainActivity)我嘗試插入一個新的記錄到數據庫中,然後嘗試將其恢復到遊標。但是,它似乎在插入語句中被捕獲。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // the projection (fields from the database that we want to use) 
     String from[] = { DBHelper.COLUMN_DESCRIPTION }; 
     // matching fields on the layout to be used with the adapter 
     int to[] = { R.id.tv1 }; 

     setContentView(R.layout.activity_main); 

     lv = (ListView) findViewById(R.id.lvMainJokes); 
     dbHelper = DBHelper.getDBHelper(this); 

     long code = dbHelper.insertNewJoke("Holiday", "", 2, "Joke 1 Description", "", "", 
       "joke", "", "", 5, 1); 

     if (code != -1) 
     cursor = dbHelper.getJokes(); 
... 

這裏是logcat的輸出:

11-15 15:19:08.040: I/Choreographer(1016): Skipped 45 frames! The application may be doing too much work on its main thread. 
11-15 15:19:35.358: D/dalvikvm(1064): GC_FOR_ALLOC freed 58K, 4% free 2726K/2836K, paused 42ms, total 45ms 
11-15 15:19:35.368: I/dalvikvm-heap(1064): Grow heap (frag case) to 3.315MB for 635812-byte allocation 
11-15 15:19:35.438: D/dalvikvm(1064): GC_FOR_ALLOC freed 2K, 4% free 3344K/3460K, paused 63ms, total 63ms 
11-15 15:19:35.548: E/SQLiteLog(1064): (1) table jokes has no column named releaseStatus 
11-15 15:19:35.568: E/SQLiteDatabase(1064): Error inserting category=Holiday releaseStatus=1 jokeType=2 created= monologueText=joke description=Joke 1 Description subcategory= answerText= questionText= jokeSource= ratingScale=5 comments= 
11-15 15:19:35.568: E/SQLiteDatabase(1064): android.database.sqlite.SQLiteException: table jokes has no column named releaseStatus (code 1): , while compiling: INSERT INTO jokes(category,releaseStatus,jokeType,created,monologueText,description,subcategory,answerText,questionText,jokeSource,ratingScale,comments) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at com.teamdotgetname.android.phase1.jokeapplication.persistence.DBHelper.insertNewJoke(DBHelper.java:143) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at com.teamdotgetname.android.phase1.jokeapplication.presentation.MainActivity.onCreate(MainActivity.java:53) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.app.Activity.performCreate(Activity.java:5133) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.os.Looper.loop(Looper.java:137) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at java.lang.reflect.Method.invoke(Method.java:525) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
11-15 15:19:35.568: E/SQLiteDatabase(1064):  at dalvik.system.NativeStart.main(Native Method) 
11-15 15:19:35.798: I/Choreographer(1064): Skipped 42 frames! The application may be doing too much work on its main thread. 
11-15 15:19:35.868: D/gralloc_goldfish(1064): Emulator without GPU emulation detected. 

感謝您抽空看看。

+0

我懷疑數據庫可能包含從代碼的早期版本創建的表。也許我需要運行確保onUpgrade將清除舊的數據庫和表版本? –

+1

我認爲你正處於正確的軌道上,你的評論如上。如果這不起作用,我會建議使用adb shell進入模擬器,並使用sqlite3來驗證你的表的確包含列'releaseStatus'。 – MDrabic

+0

我解決了我的問題。數據庫版本是問題。謝謝。 –

回答

2

快速瀏覽,你的代碼看起來不錯。例外:因爲明確要創建在創建表DATABASE_CREATE字符串的releaseStatus

android.database.sqlite.SQLiteException: table jokes has no column named releaseStatus (code 1) 

似乎是錯誤的。我遇到了一個問題,我正在修改數據庫的模式,並且更改沒有在我的應用程序中生效,因爲我沒有增加DATABASE_VERSION

您選擇嘗試這都會導致數據庫得到重建是:

  1. 清除通過設置應用程序數據的apk
  2. 手動卸載,然後重新安裝應用程序。 (只是運行gradle installDebug或有Eclipse的安裝你的應用是不夠好)
  3. 增量您DATABASE_VERSION
+0

讓我檢查你的建議。 –

+0

是的。謝謝您的意見。更新數據庫版本,並卸載/重新安裝應用程序。我花了這麼多時間來完成這樣一個簡單的修復是多麼令人沮喪。非常感謝,但是。 –

+0

@LethalLeftEye高興地幫忙!祝好運與您的其餘項目。 – MDrabic

2

我會說你在Create語句中需要一些空間。 開始:COLUMN_QUESTION_TEXT +「文本」,轉換爲COLUMN_QUESTION_TEXT +「文本」。

+0

感謝您的意見。但是,這並不能解決我的問題。 –

1

在DATABASE_CREATE錯誤「integer」,+ COLUMN_COMMENTS +「text,」。沒有一片空白。

+0

這似乎不是問題。 :/ –

2

它看起來就像列名和列類型之間沒有空格。嘗試這個。

private static final String DATABASE_CREATE = "create table " + TABLE_JOKES 
     + "(" + COLUMN_ID + " integer primary key autoincrement, " 
     + COLUMN_CATEGORY + " text not null, " + COLUMN_SUBCATEGORY 
     + " text not null, " + COLUMN_JOKE_TYPE + " integer not null, " 
     + COLUMN_DESCRIPTION + " text," + COLUMN_QUESTION_TEXT + " text," 
     + COLUMN_ANSWER_TEXT + " text," + COLUMN_MONOLOGUE_TEXT + " text," 
     + COLUMN_RATING_SCALE + " integer," + COLUMN_COMMENTS + " text," 
     + COLUMN_JOKE_SOURCE + " text," + COLUMN_RELEASE_STATUS + " integer," 
     + COLUMN_CREATED + " text," + COLUMN_MODIFIED + " text" + ");"; 
+0

因此,我已經添加了空格作爲每個人的建議,但仍不能解決問題。 SQL通常對空白非常寬容。 –

+1

您是否使用我爲您提供的create語句?它給了你同樣的錯誤還是與衆不同?你之前有幾個列名看起來像這個'releaseStatustext'。 – DroidT

+0

我添加了空格。問題依然存在。 –

相關問題