如何存储 ArrayList在 SQLite 中?
首先,我以前从未使用过数据库。阅读多篇文章后,将我的程序收集的数据存储在 SQLite 数据库中听起来是个好主意。目前,我的问题涉及存储 GeoPoints 的 ArrayList。我知道这不能存储在 sqlite 数据库中。我已经阅读了其他一些类似的问题,发现解决方案可能是创建一个单独的表,并将所有点存储在该表的两列中。
在我的数据库适配器中,我写道:
// Make a New GeoPoint Table
public void geopointTable(String routeName)
{
String GeoTable = "CREATE TABLE " + routeName + "geopoints (_id INTEGER PRIMARY KEY, "
+ "KEY_GEOLONG REAL, "
+ "KEY_GEOLAT REAL);";
db.execSQL(GeoTable);
}
// Insert into GeoPoint Table
public long insertGeopoints (double longitude, double latitude, String routeName)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_GEOLONG, longitude);
initialValues.put(KEY_GEOLAT, latitude);
return db.insert(routeName + "geopoints", null, initialValues);
}
在收集地理点并将其存储在数组中的主要活动中,我写道:
db.open();
db.geopointTable(routeName);
Iterator<GeoPoint> i = points.iterator();
while (i.hasNext()) {
db.insertGeopoints(i.next().getLongitudeE6(), i.next()
.getLatitudeE6(), routeName);
这会导致致命异常。我想,最后,我希望在我的行中有一列,其中包含托管地理点的表的名称。有谁知道更简单的方法来做到这一点,或者也许可以看到代码的问题。
First and foremost, I've never worked with databases before. After reading through multiple post, it sounded a good idea to store the data that my program is gathering in an SQLite database. At the moment, my problem deals with storing an ArrayList of GeoPoints. I know that this can not be stored in an sqlite database. I've read through some of the other like questions and found that a solution may be to create a seperate table, and store all of the points in two columns of that table.
In my Database Adapter, I wrote:
// Make a New GeoPoint Table
public void geopointTable(String routeName)
{
String GeoTable = "CREATE TABLE " + routeName + "geopoints (_id INTEGER PRIMARY KEY, "
+ "KEY_GEOLONG REAL, "
+ "KEY_GEOLAT REAL);";
db.execSQL(GeoTable);
}
// Insert into GeoPoint Table
public long insertGeopoints (double longitude, double latitude, String routeName)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_GEOLONG, longitude);
initialValues.put(KEY_GEOLAT, latitude);
return db.insert(routeName + "geopoints", null, initialValues);
}
In my main activity that gathers the geopoints and stores them in an array, I wrote:
db.open();
db.geopointTable(routeName);
Iterator<GeoPoint> i = points.iterator();
while (i.hasNext()) {
db.insertGeopoints(i.next().getLongitudeE6(), i.next()
.getLatitudeE6(), routeName);
This results in a fatal exception. I guess, in the end, I'd like to have a column in my row that hosts the name of the table that hosts the geopoints. Does anyone know an easier way to do this, or perhaps can see the problem(s) with the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
i.next() 将迭代器移动到下一个元素,因此对于每次迭代,您都将从一个元素插入经度,并从下一个元素插入纬度。如果数组中的元素数量为奇数,则会出现 OutOfBound 异常
关于 SQL:您可能应该对所有路由使用一张表:
i.next() moves iterator to the next element, so for each iteration, you are inserting longitude from one elem, and latitude from next. If number of elements in array is odd, you are getting OutOfBound exception
About SQL: You probably should use one table for all routes: