
private voID savetoDatabase( float[] tempValues ) { ContentValues values = new ContentValues(); // WM: Todo: add patIEnt ID and sensor type for (float tempVal : tempValues){ values.put( Datatable.ColUMN_DATA,tempVal ); } ContentValues[] cvArray = new ContentValues[1]; cvArray[0] = values; ContentResolver resolver = getContentResolver(); resolver.bulkInsert( HealthDevContentProvIDer.CONTENT_URI_DATA,cvArray); public int bulkInsert(Uri uri,ContentValues[] values){ int numInserted = 0; String table = null; int uriType = sURIMatcher.match(uri); switch (uriType) { case RAWinput_table: table = RAWinput_table_PATH; break; } db.beginTransaction(); try { for (ContentValues cv : values) { long newID = db.insertOrThrow(table,null,cv); if (newID <= 0) { throw new sqlException("Failed to insert row into " + uri); } } db.setTransactionSuccessful(); getContext().getContentResolver().notifyChange(uri,null); numInserted = values.length; } finally { db.endTransaction(); } return numInserted;}解决方法 我知道这将是粗鲁的,但只是丢掉这段代码.提供程序具有处理大多数sqlite *** 作的主要方法,并且您尝试将其中的三个(insert(),bulkInsert()和applyBatch())混合到某种类型的Frankenstein中.以下是主要错误: 1)此行values.put(Datatable.ColUMN_DATA,tempVal)未在每次迭代时插入新条目;它压倒了他们.在所有迭代之后,值仅包含数组的第700个浮点值.
2)正如@Karakuri记得的那样,cvArray中只有一个ContentValues实例. bulkInsert()doc说明了它的第二个参数:
An array of sets of column_name/value pairs to add to the database. This must not be null.
因此,cvArray必须包含要插入数据库的每个条目的ContentValues实例(一组).
3)不完全是一个错误,但你应该注意的事情.无法保证mtables将存在,并且尝试在不指定表的情况下进行 *** 作将抛出sqlException.
4)这三行基本没用:
if (newID <= 0) { throw new sqlException("Failed to insert row into " + uri);} 如果在插入 *** 作期间发生某些错误,insertOrThrow()已经抛出异常.如果要手动检查错误,请尝试insert()或insertWithOnConflict()(或者在try块中添加一个catch并在那里处理异常).
5)最后,有一个关于numInserted @petey指向的问题(并且没有必要重复).
最后一条建议:忘记bulkInsert()存在.我知道这将需要更多行代码,但使用applyBatch()可以获得更好的结果(更容易,因为您不必实现它). Wolfram Rittmeyer写了一系列关于交易的优秀文章,检查你是否有任何疑问.
最后但并非最不重要的(是的,我今天心情很好),这就是我将如何进行代码的基本实现:
@OverrIDepublic Uri insert(Uri uri,ContentValues values) { final sqliteDatabase db // Todo: retrIEve writable database final int match = matcher.match(uri); switch(match) { case RAWinput_table: long ID = db.insert(RAWinput_table,values); // Todo: add catch block to deal. getContext().getContentResolver().notifyChange(uri,false); return ContentUris.withAppendedID(uri,ID); default: throw new UnsupportedOperationException("UnkNown uri: " + uri); }}private voID savetoDatabase( float[] tempValues ) { ArrayList<ContentProvIDerOperation> operations = new ArrayList<ContentProvIDerOperation>(); for (float tempVal : tempValues){ operations.add(ContentProvIDerOperation .newInsert(HealthDevContentProvIDer.CONTENT_URI_DATA) .withValue(Datatable.ColUMN_DATA,tempVal).build(); .withValue() // Todo: add patIEnt ID .withValue() // Todo: add sensor type); }// WARNING!! ProvIDer operations (except query if you are using loaders) happen by default in the main thread!! getContentResolver().applyBatch(operations); } 总结 以上是内存溢出为你收集整理的android – 使用ContentValues数组批量插入全部内容,希望文章能够帮你解决android – 使用ContentValues数组批量插入所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)