2017-08-06 88 views
1

我有一个ListView多行。当我将ListView中的条目插入到sql数据库中时,所有条目都会插入到单个行中。所有的列表视图数据只插入一行

我使用String构建器获取视图中的所有条目。

我该如何解决这个问题?

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(); 
} 
+0

请参阅此链接。 https://stackoverflow.com/questions/42619923/how-to-insert-multiple-rows-into-sqlite-android – sivaprakash

+0

如何使用这种方法我不能理解在sql server请帮我 –

+0

这个解决方案是sqllite –

回答

0

获取值的列表形式表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("-----------"); 
} 

}

+0

无法正常工作我的查询是插入到非选择* –

+0

你是否得到了表格的值列表 – sivaprakash

+0

是的我从文本视图获取值的列表并将其放入sql服务器 –

0

问题是你插入到SQL数据库之前,您要添加的所有信息到字符串建设者。

您可以通过获取信息,解决这一问题,并立即将其插入到数据库:

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(); 
} 

你在一排获得了所有数据的原因是,您使用的字符串生成器生成包含字符串全部来自ListView的信息,而不仅仅是视图中的单个行。然后你把它全部放在数据库中。

解决方法是一次插入一个数据库。

+0

不是只工作第1行值获取并插入 –

+0

如何获取所有行并插入 –

+0

@PuneetKhattar ok ...保持 – ItamarG3

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(); 
}} 
相关问题