尝试将Arraylist< LatLng>在插入数据库之前进行字符串 [英] Try to convert Arraylist<LatLng> to string before insert to database
问题描述
我尝试插入我的 ArrayList< LatLng> list1
有很多像这样的值:(99.9999999,99.9999999)
到数据库MySQL中的表,非常类似于1列,如下所示:< (99.9999999,99.9999999)
第2行(99.9999999,99.9999999)
第3行(99.9999999,第99.9999999,99.9999999)
第2行(99.9999999,99.9999999)
第3行99.9999999)
...全部为1列。
在我看来,目前我有一个很好的方法:
String sql =INSERT INTO table1 VALUES ;
for(String s:list1){
sql = sql +'+ s +';
}
sql = sql +);
stmt.executeUpdate(sql);
但Android Studio下划线字符串s
:
不兼容的类型
必需:com.google.android.gms.maps.model.LatLng
发现:java.lang.String
在我看来,Android Studio试图对我说:你需要将所有值从转换为ArrayList< LatLng> list1
到字符串
!
是否有可能从我的 ArrayList
在一个方法中?
您可以将数据转换为字符串,如下所示: 也就是说,你不需要做任何具体的转换操作,我假设你有方法toString )在您的LatLng类中实现,为LatLng类型的对象提供有意义的字符串表示形式。
for(LatLng s :list1)
{
String sql =INSERT INTO table1 VALUES('+ s +');
stmt.executeUpdate(sql);
}
$ b
String sql =INSERT INTO table1 VALUES(?);
PreparedStatement stmt = dbConnection.prepareStatement(sql);
for(LatLng s:list1){
stmt.setString(1,s); //插入's'代替第一个'?'
stmt.addBatch();
}
stmt.executeBatch();
在这最后一种情况下,您正在准备一批命令立即发送到您的数据库。这比发送很多sql语句要好得多,因为你的开销要少得多。另外,你并不是自己连接sql语句。你给一个'?'占位符的初始sql语句,然后插入值为'setString()'或'setInt()'或任何类型是你想要插入。
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 ?
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.
这篇关于尝试将Arraylist< LatLng>在插入数据库之前进行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!