2011-03-06 97 views
4

錯誤:無法創建Android的SQLite數據庫:PRAGMA錯誤

E/Database(8614): Failure 21 (out of memory) on 0x0 when preparing 'PRAGMA user_version = 1'. 
E/Database(8614): Failure 21 (out of memory) on 0x0 when preparing 'ROLLBACK;'. 
D/Database(8614): exception during rollback, maybe the DB previously performed an auto-rollback 
D/AndroidRuntime(8614): Shutting down VM 
W/dalvikvm(8614): threadid=3: thread exiting with uncaught exception (group=0x4001dc20) 
E/AndroidRuntime(8614): Uncaught handler: thread main exiting due to uncaught exception 

我當前的代碼:

import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class Database extends SQLiteOpenHelper { 

    private static String DatabaseName = "Entries.db"; 

    public Database(Context context) { 
     super(context, DatabaseName, null, 1); 
    } 

    public void onCreate(SQLiteDatabase D) { 
     D.execSQL(
      "CREATE TABLE Containers (" 
      + "ID INTEGER PRIMARY KEY AUTOINCREMENT," 
      + "Parent INTEGER," 
      + "Sequence INTEGER," 
      + "Name TEXT" 
      + ")" 
    ); 
     D.execSQL(
      "CREATE TABLE Files (" 
      + "ID INTEGER PRIMARY KEY AUTOINCREMENT," 
      + "Parent INTEGER," 
      + "Sequence INTEGER," 
      + "Name TEXT," 
      + "Text TEXT" 
      + ")" 
    ); 
     D.execSQL("INSERT INTO Containers (Parent, Sequence, Name) VALUES (0, 2, \"TestLine2\")"); 
     D.execSQL("INSERT INTO Containers (Parent, Sequence, Name) VALUES (0, 1, \"TestLine1\")"); 
     D.execSQL("INSERT INTO Containers (Parent, Sequence, Name) VALUES (0, 3, \"TestLine3\")"); 
     D.execSQL("INSERT INTO Containers (Parent, Sequence, Name) VALUES (2, 1, \"TestLine2-1\")"); 
     D.execSQL("INSERT INTO Containers (Parent, Sequence, Name) VALUES (2, 2, \"TestLine2-2\")"); 
     D.close(); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
    } 

    public static Cursor Query(Context context, String SQL) { 
     StartQuerySeries(context); 
     Cursor Result = Query(SQL); 
     StopQuerySeries(); 
     return Result; 
    } 

    private static Database D = null; 
    public static void StartQuerySeries(Context context) { 
     D = new Database(context); 
    } 
    public static Cursor Query(String SQL) { 
     SQLiteDatabase X = D.getWritableDatabase(); 
     return X.rawQuery(SQL, null); 
    } 
    public static void StopQuerySeries() { 
     D.close(); 
     D = null; 
    } 

} 

時,在主要活動,這就是所謂的像這樣的錯誤發生:

Database.Query(this, "INSERT INTO Files (Parent, Sequence, Name, Text) VALUES (1, 1, \"Item1\", \"Item1 Text\")"); 
發生在「D.getWritableDatabase()」行

的錯誤...我能找到最接近的是,在http://www.sqlite.org/c3ref/c_abort.html失敗21說「庫使用不正確」 - 任何幫助?

哦,我查 - 數據庫文件不生成,但目前還沒有表中它,這樣的onCreate()以上是沒有得到調用。

回答

6

2分鐘前我有同樣的問題。我通過刪除D.close()行來解決它。

好像機器人當你關閉通過SQLiteDatabase對象不喜歡它。我認爲調用onCreate()方法的代碼已經可以正確管理數據庫的打開和關閉。所以你只需要盡一切努力獲得表格。

也許有這個對象上進行創建表後,一些工作。我想知道這是否也解決了您的問題。

我也很喜歡聽到這種行爲的確切解釋。

+0

我要發佈,「不,沒有運氣的我」,然後注意到我有2'D.close()'線 - 在'的onCreate去掉一個後()',它似乎是現在的工作。謝謝! – Izkata

+0

有關此行爲的解釋,請參閱http://stackoverflow.com/questions/5324241/what-happens-to-sqlitedatabase-object-which-is-passed-to-oncreate-method。 – Chris

1

非常感謝!

症狀: 是完全一樣的,我......我 成功獲得創建的數據庫文件,但不表 和我正在下面的錯誤在logcat的

03-16 09:55:12.093: ERROR/Database(224): Failure 21 (out of memory) on 0x0 when preparing 'ROLLBACK;'.<BR> 
03-16 09:55:12.093: DEBUG/Database(224): exception during rollback, maybe the DB previously performed an auto-rollback 

我如何固定它: 我跟着你的意見,只是刪除「db.close」指令我在的onCreate手術後 db.execSQL("CREATE TABLE ....添加和我的工作完美。

+0

現在我對爲什麼會發生這種情況更感興趣。所以我在這裏打開了一個與原因有關的新問題:http://stackoverflow.com/questions/5324241/what-happens-to-sqlitedatabase-object-which-is-passed-to-oncreate-method – Chris

0

取出db.close()的方法的onCreate

@Override 
public void onCreate(SQLiteDatabase db) 

不要叫SQLiteDatabas#close()方法;