如何写一个共同code在Android的SQLite的插入数据 [英] How to write a common code for inserting data in android's Sqlite
问题描述
有关插入的sqlite presently我一定要遵循以下步骤:
For inserting into sqlite presently I have to follow these steps:
- 创建contentValues即
ContentValues contentValues =新ContentValues();
- 将列名和值
- 最后,调用
sqLiteDatabase.insert(DATABASE_NAME,空,contentValues)
- Create contentValues i.e.
ContentValues contentValues = new ContentValues();
- Put column_name and Value
- lastly, call
sqLiteDatabase.insert(DATABASE_NAME,null,contentValues)
问题只是在步骤2中,我们已经手动 COLUMNNAME
和 Columnvalue
为n次假设我有N列持续下去。
Problem is only in step 2,we have manually Columnname
and Columnvalue
for n number of times assuming I have n Columns to persist.
所以,我写了下面的方法思考我可以重复使用它:
So, I wrote the following method thinking I can reuse it:
public void insert(Map tableMap){
ContentValues contentValues = new ContentValues();
Iterator tableMapIterator = tableMap.entrySet().iterator();
while(tableMapIterator.hasNext()){
Map.Entry mapEntry = (Map.Entry)tableMapIterator.next();
contentValues.put((String)mapEntry.getKey(), mapEntry.getValue());
}
sqLiteDatabase.insert(DATABASE_NAME,null,contentValues)
}
但问题是,当我称之为 mapEntry.getValue()
,返回类型是Object为其 contentValues.put
没有定义。
But the problem is that when I call mapEntry.getValue()
, the return type is Object for which contentValues.put
is not defined.
那么,谁能告诉我什么解决办法,这样我可以有效地使用上述方法做数据插入。
So, can anyone tell me any workaround so that I can use the above approach efficiently to do the data insertion.
注意:我想写的方法,这样我可以SQLite中使用它的所有数据类型的
推荐答案
这将访问ContentMap的对象将通过此方法<一个验证href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/database/DatabaseUtils.java#DatabaseUtils.getTypeOfObject%28java.lang.Object%29\"相对=nofollow> DatabaseUtils.getTypeOfObject()
The objects that will access your ContentMap will be verified by this method DatabaseUtils.getTypeOfObject()
因此,如果把任何东西在ContentValue不是预期的类型中的一个,它会被认为是一个字符串,以及在<一href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/database/sqlite/SQLiteConnection.java#SQLiteConnection.bindArguments%28android.database.sqlite.SQLiteConnection.$p$pparedStatement%2Cjava.lang.Object%5B%5D%29\"相对=nofollow> bindArguments(),的toString()
将被调用就可以了。
Therefore, if you put anything in your ContentValue that is not one of the expected type, it will be assumed to be a String, and in bindArguments(), toString()
will be called on it.
现在,假设你所有的对象要么识别的有效类型,或有足够的字符串重新presentation(例如,一个文件
对象将给予其路径,这足以重建它,当你从数据库中提取它),有办法把一个任意地图在ContentValue。
Now, assuming that all your object are either recognized valid types, or have sufficient String representation (for instance, a File
object would give its path, which is sufficient to recreate it when you extract it from the database), there are ways to put an arbitrary Map in a ContentValue.
的琐碎的方法是使用反射来访问的<一href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/content/ContentValues.java#ContentValues.0mValues\"相对=nofollow>内部地图,这是一贯命名为 mValues
在Android的所有版本。
The trivial way is to use reflection to access the internal map, which is consistently named mValues
across all versions of android.
另外,较短的(但速度较慢),更清晰的方式,我发现,是使用包裹
机制。事实上,<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/content/ContentValues.java#ContentValues.writeToParcel%28android.os.Parcel%2Cint%29\"相对=nofollow> ContentValue.writeToParcel 的只写内部地图。
Another, shorter (but slower) and clearer way, I find, is to use the Parcel
mechanism. Indeed, ContentValue.writeToParcel only writes the internal map.
整个code是在这里:
The entire code is here:
Parcel parcel = obtain();
parcel.writeMap(map);
parcel.setDataPosition(0);
ContentValues values = ContentValues.CREATOR.createFromParcel(parcel);
在我的博客详细解释:的http://njzk2.word$p$pss.com/2013/05/31/map-to-contentvalues-abusing-parcelable/
这篇关于如何写一个共同code在Android的SQLite的插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!