1

What are the possible reasons for a failed insertion into an sqlite3 database? The trace from the SQLiteException isn't especially helpful.

Is there any way to obtain better information about what caused the exception?

Where the table is created (sorry for any wonky spacing):

http://pastebin.com/QqUgdDz0

Where the values are parsed in endElement of a SAX parser:

http://pastebin.com/iN1M8QMe

Where the insert is called:

http://pastebin.com/xUqrmexw

Insert case in the provider:

case SUBMATERIAL_SINGLE_URI:
            rowId = db.insert(jobName+JobMetaData.SubmaterialTableMetaData.TABLE_NAME_SUFFIX,
                              JobMetaData.SubmaterialTableMetaData.TYPE, validValues);
            if (rowId > 0)
            {
                Uri submaterialUri = ContentUris.withAppendedId(JobMetaData.SubmaterialTableMetaData.CONTENT_URI,
                        rowId);
                getContext().getContentResolver().notifyChange(submaterialUri, null);
                return submaterialUri;
            }
            break;

Stacktrace:

System.err( 8048): android.database.SQLException: Failed to insert row into content://dsndata.sds2mobile.jobprovider/C_1/submaterial/NULL
System.err( 8048):  at dsndata.sds2mobile.JobProvider.insert(JobProvider.java:341)
System.err( 8048):  at android.content.ContentProvider$Transport.insert(ContentProvider.java:180)
System.err( 8048):  at android.content.ContentResolver.insert(ContentResolver.java:587)
System.err( 8048):  at dsndata.sds2mobile.parser.MobileParser.endElement(MobileParser.java:227)
System.err( 8048):  at org.xml.sax.helpers.XMLReaderAdapter.endElement(XMLReaderAdapter.java:355)
System.err( 8048):  at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:160)
System.err( 8048):  at org.apache.harmony.xml.ExpatParser.append(Native Method)
System.err( 8048):  at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:505)
System.err( 8048):  at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:492)
System.err( 8048):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:308)
System.err( 8048):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:264)
System.err( 8048):  at org.xml.sax.helpers.XMLReaderAdapter.parse(XMLReaderAdapter.java:225)
System.err( 8048):  at javax.xml.parsers.SAXParser.parse(SAXParser.java:361)
System.err( 8048):  at dsndata.sds2mobile.parser.MobileParser.parse(MobileParser.java:120)
System.err( 8048):  at dsndata.sds2mobile.UI.onClick(UI.java:90)
System.err( 8048):  at android.view.View.performClick(View.java:2408)
System.err( 8048):  at android.view.View$PerformClick.run(View.java:8817)
System.err( 8048):  at android.os.Handler.handleCallback(Handler.java:587)
System.err( 8048):  at android.os.Handler.dispatchMessage(Handler.java:92)
System.err( 8048):  at android.os.Looper.loop(Looper.java:144)
System.err( 8048):  at android.app.ActivityThread.main(ActivityThread.java:4937)
System.err( 8048):  at java.lang.reflect.Method.invokeNative(Native Method)
System.err( 8048):  at java.lang.reflect.Method.invoke(Method.java:521)
System.err( 8048):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
System.err( 8048):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
System.err( 8048):  at dalvik.system.NativeStart.main(Native Method)

I should note that NULL in the URI here is a string value and is acceptable according to the URI matching scheme. This is a test run and isn't using real values yet.

Update: It turns out that my partner (who was producing the XML) left out a few items. Got it all figured out.

Thanks for your help! I appreciate the time.

3
  • Can you show it to us? And the code around the insertion? Commented Jan 21, 2011 at 21:19
  • stacktrace? Also, you can format code snippets within your question using the {} button. This is preferable to outside links. Commented Jan 21, 2011 at 21:52
  • Could you please provide code snippets from your "insert()" function in your ContentProvider (JobProvider?!?) ? Commented Jan 22, 2011 at 10:00

3 Answers 3

3

A few:

  1. Out of space in storage being used by SQLite db
  2. No permission to use sdcard and using an sdcard backed db
  3. Requesting to insert a row on a read only copy of a database

There could be many more.

Sign up to request clarification or add additional context in comments.

Comments

2

I got a similar problem, in my case the insert was done just after a delete of the whole table.

Believe it or not, the rowId returned was actually 0, so the row WAS inserted but the code at the content provider thought it was not.

From the documentation of SQliteDatabase:insert

the row ID of the newly inserted row, or -1 if an error occurred

The code validating rowId > 0 (which is the one used on Android examples BTW) is incorrect

It should be:

if (rowId < 0) {
   throw new SQLException(FAILED_TO_INSERT_ROW_ERROR + uri);
}
else {
    Uri returnUri = ContentUris.withAppendedId(Table.getContentUri(), rowId);
    getContext().getContentResolver().notifyChange(returnUri, null);
    return returnUri;
}

Comments

0

Maybe you're trying to insert a row that violates a "PRIMARY KEY" or "UNIQUE" restriction (I see, though, that you don't have any "UNIQUE" fields in your database)?

1 Comment

Thanks for the "insert()" snippet! Can you say something about the "validValues" ContentValues? If I'm not completely wrong I think you have a "primary key" id-field in your table. Does the ContentValues ("validValuse") contain data for the id-column? Perhaps you're trying to insert an id that already exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.