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


i try insert arraylist<latlng> list1 lot of values this: (99.9999999,99.9999999) table in database mysql, exacly 1 column, this:

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

... 1 column.

in opinion, have method this:

string sql = "insert table1 values("; for(string s : list1) {     sql = sql+"'"+s+"'";  }     sql = sql+")"; stmt.executeupdate(sql); 

but android studio underlines string s , says:

incompatible types required: com.google.android.gms.maps.model.latlng found: java.lang.string 

in opinion, android studio trying me: need convert values arraylist<latlng> list1 string !
possible convert values arraylist in 1 method ?

bad way of doing it:

you can convert data string following way:

for(latlng s : list1) {     string sql = "insert table1 values('"+s+"');     stmt.executeupdate(sql); } 

that is, don't have specific convert it. i'm assuming have method tostring() implemented in latlng class give objects of latlng type meaningful string representation.

good way of doing it:

string sql = "insert table1 values(?)"; preparedstatement stmt = dbconnection.preparestatement(sql); for(latlng s : list1){     stmt.setstring(1, s); // insert 's' in place of 1st '?'     stmt.addbatch(); } stmt.executebatch(); 

in last case preparing batch of commands send @ once database. better sending many sql statements because end having lot less overhead. also, you're not concatenating sql statement yourself. give initial sql statement '?' placeholder , insert values 'setstring()' or 'setint()' or whatever type of want insert.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -