Try to convert Arraylist<LatLng> to string before insert to database

半世苍凉 提交于 2019-12-25 08:06:51

问题


I try to insert my ArrayList<LatLng> list1 with a lot of values like this: (99.9999999,99.9999999) to table in database MySQL, exacly to 1 column, something like this:

row 1 (99.9999999,99.9999999)  
row 2 (99.9999999,99.9999999)  
row 3 (99.9999999,99.9999999)

... all to 1 column.

In my opinion, currently i have a good method for this:

String sql = "INSERT INTO table1 VALUES(";
for(String s : list1) {
    sql = sql+"'"+s+"'"; 
}    
sql = sql+")";
stmt.executeUpdate(sql);

but Android Studio underlines String s and says:

Incompatible types
Required: com.google.android.gms.maps.model.LatLng
Found: java.lang.String

In my opinion, Android Studio trying to say me: you need to convert all values from ArrayList<LatLng> list1 to String !
Is it possible to convert all values from my ArrayList in one method ?


回答1:


Bad way of doing it:

You can convert your data to string the following way:

for(LatLng s : list1)
{
    String sql = "INSERT INTO table1 VALUES('"+s+"');
    stmt.executeUpdate(sql);
}

That is, you don't have to do anything specific to convert it. I'm assuming you have the method toString() implemented in your LatLng class to give objects of LatLng type a meaningful string representation.

Good way of doing it:

String sql = "INSERT INTO table1 VALUES(?)";
PreparedStatement stmt = dbConnection.prepareStatement(sql);
for(LatLng s : list1){
    stmt.setString(1, s); // insert 's' in place of the 1st '?'
    stmt.addBatch();
}
stmt.executeBatch();

In this last case you are preparing a batch of commands to send at once to your database. This is much better than sending many sql statements because you end up having a lot less overhead. Also, you're not concatenating the sql statement by yourself. You give the initial sql statement with a '?' placeholder and then you insert the values with 'setString()' or 'setInt()' or whatever the type is of what you want to insert.




回答2:


The error is telling you that the ArrayList does not contain strings, not that you must convert to a string. You can not implicitly convert an array entry of type LatLng to a string as you have done.

Your for statement should be:

for(LatLng ll : list1)

You should store the LatLng value in a point datatype , assuming you are using PostgreSQL/PostGIS. As noted by @juergen d in your comments it's not a good practice to store a comma delimited string in a single column or quote the string yourself.

Here's a link explaining how to store the LatLng data as a Point in PostGIS. Alternatively, you could store the lat and lng separately in 'double precision' datatypes.




回答3:


  1. Use the toString method on the LatLng instances to convert them to strings.
  2. Do not attempt to quote parameters in SQL queries yourself; use the API's support for placeholders for that purpose. Trying to quote strings yourself makes it trivial for attackers to destroy or manipulate your database to their own ends.


来源:https://stackoverflow.com/questions/40442503/try-to-convert-arraylistlatlng-to-string-before-insert-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!