SQLiteException failed to insert row
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.
A few:
- Out of space in storage being used by SQLite db
- No permission to use sdcard and using an sdcard backed db
- Requesting to insert a row on a read only copy of a database
There could be many more.
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;
}
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)?
精彩评论