无法创建Android的SQLite数据库:PRAGMA错误 [英] Cannot create Android SQLite database: PRAGMA error

查看:200
本文介绍了无法创建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

我目前的code:

My current code:

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

}

时,在主要活动,它被称为像这样发生错误:

The error happens when, in the primary Activity, it's called like this:

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写着图书馆使用不当? - 任何帮助

The error happens on the "D.getWritableDatabase()" line... The closest thing I can find is, on http://www.sqlite.org/c3ref/c_abort.html that Failure 21 says "Library used incorrectly" - any help?

哦,我查 - 数据库文件并生成,但在它没有桌子,这样的onCreate()以上是没有得到所谓的

Oh, and I checked - the database file does get created, but there are no tables in it, so that onCreate() above isn't getting called.

推荐答案

我已经得到了同样的问题,2分钟前。我通过删除 D.close()行解决它。

I've got the same problem 2 minutes ago. I solved it by removing the D.close() line.

好像机器人当你关闭通过 SQLiteDatabase 对象不喜欢它。我觉得周围的code它调用的onCreate()方法已经管理打开和关闭正确的数据库。所以,你只需要尽一切努力让表始终。

It seems like android doesn't like it when you close the passed SQLiteDatabase Object. I think the surrounding code which calls the onCreate() method already manages opening and closing the database correctly. So you just have to do everything to get the tables up.

也许有这个对象上进行创建表后的一些工作。我想知道这样能否解决您的问题,以及

Maybe There is some work done on this object after you created the tables. I would like to know if this solves your problem as well.

我也真的很喜欢听到这种行为的确切的解释。

I would also really love to hear the exact explaination for this behaviour.

这篇关于无法创建Android的SQLite数据库:PRAGMA错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆