划分ArrayLists输出

时间:2013-12-15 17:00:43

标签: java sqlite arraylist

我正在尝试提出一种动态解决方案,用于将要实现的值划分为SQLite数据库。

例如,下面的输出应该在每个Px之前进行划分(xP之后的值)

输出:

[P1, Nut, Red, 12, London, P2, Bolt, Green, 17, Paris, P3, Screw, Blue, 17, Rome, P4, Screw, Red, 14, London, P5, Cam, Blue, 12, Paris, P6, Cog, Red, 19, London]

我想要的是像

这样的输出
"INSERT INTO " + tableName + " VALUES (P1, Nut, Red, 12, London)"

String query;
query = "INSERT INTO " + tableName + " VALUES (";
for(int i=0; i< tableFields.size(); i++)
{ 
//For every value and column it creates a new line shown below so it becomes like        
// 'SNO' = 'S8' and so on.
query+= "'" + tableValues.get(i) + "',";
}
 //removes the last comma.
 query = query.substring(0, query.length()-1);
 query+= ")";

4 个答案:

答案 0 :(得分:1)

我假设你的输出是String。我还假设每个Px后面都会有4个值,用逗号和空格分隔。

然后你可以使用String方法将String.split(String)拆分成一个数组,并遍历数组并打印数组的前五个元素,然后打印后五个元素,依此类推。

这有效:

String tableName = "aaa"; // just for the sake of testing it
String[] splittedOutput = yourOutputString.split(", ");
int valuesOnLine = 5; // EDIT
for (int i = 0; i < splittedOutput.length; i++) {
    if (i % valuesOnLine == 0) { // EDIT
        // 0 % 5 == 0, 5 % 5 == 0 etc., so we get here on every 5th element
        // which are the Px's, so we need to print the beginning of the line
        System.out.print("INSERT INTO " + tableName + " VALUES (");
    }
    System.out.print(splittedOutput[i]);
    if (i % valuesOnLine == valuesOnLine - 1) { // EDIT
        // we get here on the 4th, 9th, 14th, ... element, which means that
        // it's the last element that we'll print on that line
        System.out.println(")");
    } else {
        // if the condition above is not true, it's just some element
        // in the middle so we just print a comma and a space
        System.out.print(", ");
    }
}

输出结果为:

INSERT INTO aaa VALUES (P1, Nut, Red, 12, London)
INSERT INTO aaa VALUES (P2, Bolt, Green, 17, Paris)
INSERT INTO aaa VALUES (P3, Screw, Blue, 17, Rome)
INSERT INTO aaa VALUES (P4, Screw, Red, 14, London)
INSERT INTO aaa VALUES (P5, Cam, Blue, 12, Paris)
INSERT INTO aaa VALUES (P6, Cog, Red, 19, London)

如果您想在线上使用不同数量的值,请修改valuesOnLine变量。

答案 1 :(得分:0)

试试这个....

List<String> list =new ArrayList<String>();

Say list的值为

[P1, Nut, Red, 12, London, P2, Bolt, Green, 17, Paris, P3, Screw, Blue, 17, Rome, P4, Screw, Red, 14, London, P5, Cam, Blue, 12, Paris, P6, Cog, Red, 19, London]

然后从P1到P2的计数将是5

int count=0;
for(int i=0;i<list.size();i++) 
{
  if(list.get(i).equals("P2"))
   count=i; // count will be 5
}

    Connection conn =null;
    PreparedStatement st = conn.prepareStatement("insert into table xx  values(?,?,?)");

    for(int j=0;j<list.size();) {
       for(int k=1;k<=count;k++,j++){
        st.setString(k, list.get(j)); 
        st.execute();
       }
    }

答案 2 :(得分:0)

你可能不需要检查它是否以Px开头, 假设

  • 值的顺序始终相同
  • 序列中没有缺失值
  • 每4个值后插入的新记录

         List<String> list =new ArrayList<String>();
        //list --> add values
        Connection conn =null;
        PreparedStatement st = conn.prepareStatement("insert into table xx  values(?,?,?,?)");
    
        for(int i=0;i<list.size();) {
            st.setString(1, list.get(i++)); 
            st.setString(2, list.get(i++)); //or setInt(..)
            st.setString(3, list.get(i++));
            st.setString(4, list.get(i++));
            st.execute();
        }
    

这又是为了让我们了解如何相应地修改进程,连接部分,setX和execute方法

答案 3 :(得分:0)

试试这个......  让arraylist成为......

int i = 0;

while(i&lt; al.size()) {

String sql =“INSERT INTO”+ tableName +“VALUES(al.get(i),al.get(++ i),al.get(++ i),al.get(++ i), al.get(++ i))的“;

我++;

}