Unable to create Android SQLite database: PRAGMA error

Errors:

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 

My current code is:

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

An error occurs when in the main operation it is called like this:

 Database.Query(this, "INSERT INTO Files (Parent, Sequence, Name, Text) VALUES (1, 1, \"Item1\", \"Item1 Text\")"); 

The error occurs in the string "D.getWritableDatabase ()". The closest thing I can find is http://www.sqlite.org/c3ref/c_abort.html that Error 21 says: "The library was not used correctly" - any help?

Oh, and I checked - the database file is created, but there are no tables in it, so onCreate () is not called above.

+4
source share
3 answers

I have the same problem 2 minutes ago. I solved this by removing the D.close() .

Android doesn't seem to like it when you close the passed SQLiteDatabase object. I think that the surrounding code that calls the onCreate() method already controls the opening and closing of the database correctly. Therefore, you just need to do everything to make tables.

Perhaps after creating tables with this object some work is done. I would like to know if this solves your problem.

I would also love to hear an accurate explanation of this behavior.

+6
source

Thanks a lot!

Symptom: it was exactly the same for me ... I managed to create a database file, but not a table, and I got the following error in 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 

How I fixed it: I followed your advice, just delete the "db.close" statement that I added after db.execSQL("CREATE TABLE .... in my onCreate procedure, and I worked fine.

+1
source

Remove db.close () in the onCreate method

 @Override public void onCreate(SQLiteDatabase db) 

Do not call SQLiteDatabas # close ();

0
source

Source: https://habr.com/ru/post/1342480/


All Articles