1

I have a ListView with multiple rows. When I insert the entries in the ListView into an sql database, all the entries get inserted into a single row.

I get all the entries from the view with a String builder.

How can I solve this problem?

StringBuilder sb = new StringBuilder();

StringBuilder sb2 = new StringBuilder();

StringBuilder sb3 = new StringBuilder();
//get data form text view

for(int i=0; i<simpleAdapter.getCount(); i++) {
    String a = ((TextView) findViewById(R.id.al)).getText().toString();
    String b = ((TextView) findViewById(R.id.sh)).getText().toString();
    String c = ((TextView) findViewById(R.id.acc)).getText().toString();

    simpleAdapter.getItem(i).toString();
    sb.append(a);
    sb.append("\n");
    sb2.append(b);
    sb2.append("\n");
    sb3.append(c);
    sb3.append("\n");

}

String text = sb.toString();
String text2 = sb2.toString();
String text3 = sb3.toString();

//my sql connection insert query
try {
    Connection con = connectionClass.CONN();
    if (con == null) {
        Toast.makeText(getApplicationContext(), "Error in connection with SQL server", Toast.LENGTH_LONG).show();
    } else {

        String query = "INSERT INTO suigen.TransAlmiraShelf (Almira, Shelf, AccessionNo) VALUES('" + String.valueOf(text) + "','" + String.valueOf(text2) + "','" + String.valueOf(text3) + "')";

        Statement stmt = con.createStatement();
        stmt.execute(query);
        Toast.makeText(Main13Activity.this, "Your Data Successfully Saved", Toast.LENGTH_LONG).show();
    }

}
catch (Exception ex) {
     Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_LONG).show();
}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44

3 Answers3

0

Get the list of Values form the table usng JDBC

Connection connection = DriverManager.getConnection("URL", "USERNAME", 
"PASSWORD");
PreparedStatement statement = connection.prepareStatement("select * from 
table");
ResultSet resultSet = statement.executeQuery();

if (resultSet != null) {
while (resultSet.next()) {
    ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {

        int type = resultSetMetaData.getColumnType(i);
        if (type == Types.VARCHAR || type == Types.CHAR) {
             System.out.println(resultSet.getString(i));
        } else {
             System.out.println(resultSet.getLong(i));
        }
    }

     System.out.println("-----------");
}

}

sivaprakash
  • 528
  • 6
  • 15
0

The problem is that you are adding all the information into the string builders before you insert into the sql database.

You can fix this by getting the information and immediately insert it into the database:

ListView listView =  ((ListView) findViewById(R.id.listView));
try {
    Connection con = connectionClass.CONN();//first you do the connection
    if (con == null) {
        Toast.makeText(getApplicationContext(), "Error in connection with SQL server", Toast.LENGTH_LONG).show();
    } else {
        for(int i=0; i<simpleAdapter.getCount(); i++) {//then create the information you're going 
            View row = simpleAdapter.getView(i, null, listView);
            String a = ((TextView) row.findViewById(R.id.al)).getText().toString();
            String b = ((TextView) row.findViewById(R.id.sh)).getText().toString();
            String c = ((TextView) row.findViewById(R.id.acc)).getText().toString();


            sb.append(a);
            sb.append("\n");
            sb2.append(b);
            sb2.append("\n");
            sb3.append(c);
            sb3.append("\n");



            String text = sb.toString();
            String text2 = sb2.toString();
            String text3 = sb3.toString();



            String query = "INSERT INTO suigen.TransAlmiraShelf (Almira, Shelf, AccessionNo) VALUES('" + String.valueOf(text) + "','" + String.valueOf(text2) + "','" + String.valueOf(text3) + "')";

            Statement stmt = con.createStatement();
            stmt.execute(query);
            Toast.makeText(Main13Activity.this, "Your Data Successfully Saved", Toast.LENGTH_LONG).show();
        }
    }

}
catch (Exception ex) {
     Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_LONG).show();
}

The reason you got all the data in one row is that you used the string builder to build a string that contains all the information from the ListView, and not just a single row in the view. Then you put it all in the database.

The solution is to insert into the database one at a time.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
0
how to get row value on every time 

StringBuilder sb = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();
                StringBuilder sb3 = new StringBuilder();
                   for(int i=0; i<simpleAdapter.getCount(); i++) {
                    String a = ((TextView) findViewById(R.id.al)).getText().toString();
                    String b = ((TextView) findViewById(R.id.sh)).getText().toString();
                    String c = ((TextView) findViewById(R.id.acc)).getText().toString();
                    simpleAdapter.getItem(i).toString();
                    sb.append(a);
                    sb.append("\n");
                    sb2.append(b);
                    sb2.append("\n");
                    sb3.append(c);
                    sb3.append("\n");

                    String text = sb.toString();
                    String text2 = sb2.toString();
                    String text3 = sb3.toString();
}}