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:
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\")");
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?
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.
I've got the same problem 2 minutes ago. I solved it by removing the D.close() line.
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.
Thanks a lot !!!
The symptom:
was exactly the same for me ...
I succeeded to get the database file created but not the tables
and I was getting 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 remove a "db.close" instruction I added after
db.execSQL("CREATE TABLE .... in my onCreate procedure and i worked perfect.
Remove the db.close() in the method onCreate
#Override
public void onCreate(SQLiteDatabase db)
Don't call SQLiteDatabas# close();
Related
I have the next code to create my db..
public class ModeloPaciente extends SQLiteOpenHelper {
//Tabla Proposicion Condicional
static final String proposicionCondicionalTabla="ProposicionCondicional";
static final String colproposicionCondicionalID="ProposicionCondicionalID";
static final String colproposicionCondicionalDescripcion="ProposicionCondicionalDescripcion";
static final String colproposicionCondicionalcuandoInferirForeign="CuandoInferir";
public ModeloPaciente(Context context) {
super(context, dbName, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+proposicionCondicionalTabla+" " +
"("+colproposicionCondicionalID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +
colproposicionCondicionalDescripcion+ " TEXT NOT NULL," +
colproposicionCondicionalcuandoInferirForeign+" INTEGER NOT NULL,"+
"FOREIGN KEY ("+colproposicionCondicionalcuandoInferirForeign+") REFERENCES "+cuandoInferirTabla+"
("+colcuandoInferirID+"));");
}
Later I put data inside the table like this
public boolean insertarProposicion(Proposicion proposicion) {
try {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv= new ContentValues();
cv.put(colproposicionCondicionalDescripcion, proposicion.getProposicionCondicionalDescripcion());
cv.put(colproposicionCondicionalcuandoInferirForeign, getCuandoInferirID(proposicion.getProposicionCondicionalCuandoInferirForeign()));
db.insert(proposicionCondicionalTabla, colproposicionCondicionalID, cv);
//db.close();
return true;
} catch (Exception e) {
System.out.println(e);
return false;
}
But I get an error that said:
06-07 15:36:15.507: E/Database(257): Error inserting CuandoInferir=1 ProposicionCondicionalDescripcion=Se debe inferir acerca de los dias de marcha realizados o no
06-07 15:36:15.507: E/Database(257): android.database.sqlite.SQLiteException: no such column: CuandoInferir: , while compiling: INSERT INTO ProposicionCondicional(CuandoInferir, ProposicionCondicionalDescripcion) VALUES(?, ?);
And I check the database and it has the corresponding column "CuandoInferir"..¿What happen, I do not know? Thanks for your help
You might be working with an older version of the database. Clear your app's data and try again:
Settings --> Applications --> Manage applications --> [your app] --> Clear data
Your code seems fine, however I would check the existing database. Check the column names in the 'ProposicionCondicional' table. You can do it by opening your database in sqlite3 following these steps (obviously connect your phone to pc before doing the steps)
adb shell
cd /data/data/<your.applications.package>/databases
sqlite3 <databases_name>
.schema
See if the table really has the 'CuandoInferir' column. If not, try to recreate the table.
I am facing problem in android application i want to create database with following script.
CREATE TABLE IF NOT EXISTS events (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
source_device_id INT,
timestamp INT NOT NULL,
processed BOOLEAN DEFAULT FALSE,
event_type TINYINT NOT NULL,
payload BLOB);
CREATE INDEX IF NOT EXISTS events_unprocessed ON events (processed, _id);
and my android code is
public void onCreate(SQLiteDatabase db) {
InputStream schemaStream = context.getResources().openRawResource(R.raw.logdb_schema);
Scanner schemaScanner = new Scanner(schemaStream, "UTF-8");
schemaScanner.useDelimiter(";");
Log.i(TAG, "Creating database");
try {
db.beginTransaction();
while (schemaScanner.hasNext()) {
String statement = schemaScanner.next();
Log.d(TAG, "Creating database: " + statement);
db.execSQL(statement);
}
db.setTransactionSuccessful();
}
finally {
db.endTransaction();
}
}
Its not working its generating run-time exception:
android.database.sqlite.SQLiteException: not an error:
I had this problem because of whitespace after the last semicolon in my schema. My java code was executing that whitespace as a separate query.
On Froyo it complains, on Ice Cream Sandwich it doesn't cause a problem.
My guess would that you are using a BOOLEAN & TINYINT type which are not supported. You need to use INTEGER instead
My app's got a database with three tables in it: one to store the names of the people it tracks, one to track an ongoing event, and one - for lack of a better term - for settings.
I load the first table when the app starts. I ask for a readable database to load in members to display, and later I write to the database when the list changes. I've had no problems here.
The other two tables, however, I can't get to work. The code in the helper classes is identical with the exception of class names and column names, and (at least until the point where I try to access the table) the code to use the table is nearly identical as well.
Here's the code for my helper class (I've got a separate helper for each table, and as I said, it's identical except for class names and columns):
public class db_MembersOpenHelper extends SQLiteOpenHelper
{
public static final String TABLE_NAME = "members_table";
public static final String[] COLUMN_NAMES = new String[] {
Constants.KEY_ID,
"name",
"score"
};
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_NAMES[0] + " INTEGER PRIMARY KEY autoincrement, "
+ COLUMN_NAMES[1] + " TEXT, "
+ COLUMN_NAMES[2] + " INTEGER);";
public db_MembersOpenHelper(Context context)
{
super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) { db.execSQL(TABLE_CREATE); }
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion + " to " + newVersion + ".");
// Do nothing. We do not have any updated DB version
}
}
Here's how I use it successfully:
db_MembersOpenHelper membersDbHelper = new db_MembersOpenHelper(this);
SQLiteDatabase membersDb = membersDbHelper.getReadableDatabase();
Cursor membersResult = membersDb.query(TABLE_NAME, null, null, null, null, null, null);
members = new HashMap<String, Integer>();
membersResult.moveToFirst();
for(int r = 0; r < membersResult.getCount(); r++)
{
members.put(membersResult.getString(1), membersResult.getInt(2));
membersResult.moveToNext();
}
membersDb.close();
And here's where it fails:
db_PlayersOpenHelper playersDbHelper = new db_PlayersOpenHelper(this);
final SQLiteDatabase playersDb = playersDbHelper.getWritableDatabase();
if(newGame)
{
for(String name : players)
{
ContentValues row = new ContentValues();
row.put(COLUMN_NAMES[1], name);
row.put(COLUMN_NAMES[2], (Integer)null);
playersDb.insert(TABLE_NAME, null, row);
}
}
The first one works like a charm. The second results in ERROR/Database(6739): Error inserting achievement_id=null name=c
android.database.sqlite.SQLiteException: no such table: players_table: , while compiling: INSERT INTO players_table(achievement_id, name) VALUES(?, ?);
...
I did do some testing, and the onCreate method is not being called at all for the tables that aren't working. Which would explain why my phone thinks the table doesn't exist, but I don't know why the method isn't getting called.
I can't figure this out; what am I doing so wrong with the one table that I accidentally did right with the other?
I think the problem is that you are managing three tables with with three helpers, but only using one database. SQLiteOpenHelper manages on database, not one table. For example, it checks to see whether the database, not table, exists when it starts. It already does, so onCreate() does not fire.
I would manage all tables with one helper.
Let me see if I get this right. You are trying to create one database with three tables. But when you create the database, you create just one table; you are somehow instantiating the same database at a different place and wonder why its onCreate method doesn't get called. Is this a correct interpretation?
My strategy would be to try and create all three tables in the single onCreate() method.
If you are working with multiple tables, then you have to create all of the tables at once. If you have run your application first and later you update your database, then it will not upgrade your DB.
Now delete your application, then run it again.
There is one more solution but it is not proper. You can declare onOpen method in which you can call onCreate. And add IF NOT EXISTS before table name in your create table string. – Sourabh just now edit
I am creating my SQLite database for my App at runtime if it does not exist and insert rows if it does. Since it is supposed to be created at runtime and I have implemented it by creating a subclass of SQLiteOpenHelper and overriding the onCreate() method -
"Do I need to put anything in the /assets folder of my project?"
I am not using any Content Provider "Do I need to add any tags in the AndroidManifest.xml?"
Here is what I have done. The strings have been defined properly and I do not get any runtime exceptions.
Implementation of the SQLiteOpenHelper subclass.
public class MyDB extends SQLiteOpenHelper {
public MyDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION );
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(USERAUTH_TABLE_CREATE);
db.execSQL(USERPREF_TABLE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and
recreate.");
db.execSQL("DROP TABLE IF EXISTS " + USERAUTH_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + USERPREF_TABLE_NAME);
onCreate(db);
}
}
Here is where I create an instance of the MyDB subclass of the SQLiteOpenHelper.
MyDB tdb = new MyDB(Activity.this);
SQLiteDatabase db = tdb.getReadableDatabase();
Everything runs and when I go to the sqlite shell and write the following query
select * from table_name - it just tells me no such record exist. I set breakpoints and it seems after the getReadableDatabase() is called the #Override OnCreate() method is never executed which is where I execute the Create table SQLs. I have tried getWritableDatabase()
as well.
I dont understand why the tables are not being created. If anyone can help that would be awesome.
Thanks.
Query Text String#1
private static final String USERAUTH_TABLE_CREATE =
"CREATE TABLE " + USERAUTH_TABLE_NAME + " (" +
"number INTEGER NOT NULL," +
"dip TEXT NOT NULL," +
"email TEXT NOT NULL," +
"password TEXT NOT NULL," +
"flag INTEGER" + ");" ;
Query Text String #2
private static final String USERPREF_TABLE_CREATE =
"CREATE TABLE " + USERPREF_TABLE_NAME + " (" +
"tpd TEXT NOT NULL ," +
"cat TEXT NOT NULL" + ");";
If onCreate() is not being called, then the database has already been created for your app. The quickest way to solve it is to delete your project on the emulator (Settings --> Applications --> Your application), and then restart your application. Alternatively you could use ADB to just drop your database -- it's up to you. Restarting the app after dropping the database will call onCreate() because the database does not exist, and then your table creation sql will be run. onCreate() is only called if your database DOES NOT exist (so pretty much the first time you call the database in your code.
"Do I need to put anything in the /assets folder of my project?"
No
"Do I need to add any tags in the AndroidManifest.xml?"
No
Your syntax is ok ... could you paste the query you are making for creating tables ?
This might be a silly question, but have you defined the DATABASE_NAME and DATABASE_VERSION variables?
Issue resolved. Code was working all the way once again. sqlite shell was not showing me the tables and the database. When I kept my app running on the emulator and navigated to data > data > your-package-name > databases > your-database-file using DDMS the system shows me the SQLite DB was created fine. I have checked the tables are there as well.
Thank you all guys!!
This simple application will create a data base and 1 table w and at the end it will
retrieve the value which u have enetered and vl show in textBox.
SQLiteDatabase myDB= null;
String TableName="Profile";
String ShowData="";
/* This function create new database if not exists. */
try {
myDB = openOrCreateDatabase("DataBase.db",SQLiteDatabase.CREATE_IF_NECESSARY, null);
/* Create a Table in the Database. */
myDB.execSQL("CREATE TABLE IF NOT EXISTS "+ TableName + " (id INT(4),firstname VARCHAR,lastname VARCHAR);");
/* Insert data to a Table*/
//myDB.execSQL("INSERT INTO "+ TableName +"(id, firstname, lastname) "+ " VALUES (1, 'Pir', 'Fahim');");
Toast.makeText(this," DATA BASE HAVE BEEN CREATED ", Toast.LENGTH_SHORT).show();
/*Fetch data from database table */
Cursor c = myDB.rawQuery("SELECT* FROM " + TableName , null);
int id = c.getColumnIndex("id");
int fristName = c.getColumnIndex("firstname");
int lastName = c.getColumnIndex("lastname");
// Check result.
c.moveToFirst();
if (c != null) {
// Loop through all Results
do {
int personId = c.getInt(id);
String FirstName = c.getString(fristName);
String LastName = c.getString(lastName);
ShowData =ShowData +personId+" .) " +FirstName+" "+LastName+"\n";
txt.append("********************"+"\n"+personId+"\n"+FirstName+"\n"+LastName+"\n");
// Toast.makeText(this," RESULT 2 IS = "+ ShowData, Toast.LENGTH_LONG).show();
}
while(c.moveToNext());
}
// Toast.makeText(this," RESULT 2 IS = "+ ShowData, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(this, "Error = "+e.getMessage(), Toast.LENGTH_LONG).show();
}
finally
{
if (myDB != null)
myDB.close();
}
I'm trying to create a table in android database, but when i try to run the application the LogCat returns the following error:
08-22 02:39:29.098: ERROR/AndroidRuntime(277): Caused by: android.database.sqlite.SQLiteException: near "auto_increment": syntax error: CREATE TABLE words(id INTEGER PRIMARY KEY, word TEXT, count INTEGER not null auto_increment)
The code for this error is this:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, word TEXT, count INTEGER not null auto_increment)");
}
And there is a error on this line too, the one between arrows:
public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
-->this.db = openHelper.getWritableDatabase();<--
this.insertStmt = this.db.compileStatement(INSERT);
this.updateStmt = this.db.compileStatement(UPDATE);
}
Ps: The codes before is from DataHelper class.
and erro at this line (the logcat just say the line of the class, dont say the error):
this.dh = new DataHelper(this);
Ps: DataHelper is the class that manage the database.
Change auto_increment to autoincrement and you should be good. Simple syntax error :)
There are two problems in count INTEGER not null auto_increment
as mentioned by smith324, auto_increment is spelled wrong
more importantly, count has to be primary key, if you want to have it auto-increment by sqlite rules.