无法创建Android SQLite数据库:PRAGMA错误

时间:2011-03-06 04:00:01

标签: android database 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;
   }

}

当在主Activity中调用它时,会发生错误:

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()没有被调用。

3 个答案:

答案 0 :(得分:6)

我在2分钟前遇到了同样的问题。我通过删除D.close()行来解决它。

当你关闭传递的SQLiteDatabase对象时,似乎android不喜欢它。我认为调用onCreate()方法的周围代码已经正确地管理打开和关闭数据库。所以你只需要做所有事情来获得表格。

在创建表格后,可能会对此对象执行一些操作。我想知道这是否也解决了你的问题。

我也很想听到这种行为的确切解释。

答案 1 :(得分: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 ....,我工作得很完美。

答案 2 :(得分:0)

删除方法onCreate

中的db.close()
@Override
public void onCreate(SQLiteDatabase db)

不要调用SQLiteDatabas#close();