【发布时间】:2015-10-23 23:20:39
【问题描述】:
大家好,我正在尝试制作一个嵌套循环,第一个循环中的循环应该重新开始,而不是重复 161。正如您在图片中看到的那样,它失败了。而且我不知道如何解决它。
这是我的代码示例,我在失败的代码中留下了注释:
int category_a = 1;
String childname_a = "A";
int category_b = 2;
String childname_b = "B";
int category_c = 3;
String childname_c = "C";
String description = "Indsæt beskrivelse her";
try
{
db.beginTransaction();
String sql = "INSERT INTO child (_idchild, category, childname, description) VALUES (?, ?, ?, ?)";
SQLiteStatement statement = db.compileStatement(sql);
for (int i = 1; i < 162; i++)
{
statement.clearBindings();
statement.bindLong(1, i);
statement.bindLong(2, category_a); //+ i
statement.bindString(3, childname_a + i);
statement.bindString(4, description);
statement.executeInsert();
}
for (int j = 162; j < 325; j++)
{
statement.clearBindings();
statement.bindLong(1, j);
statement.bindLong(2, category_b); //+ i
// It fails here, and do not start from 1 and count up to 162 as you can see.
for (int h = 1; h < 162; h++)
{
statement.bindString(3, childname_b + h);
}
statement.bindString(4, description);
statement.executeInsert();
}
db.setTransactionSuccessful(); // This commits the transaction if there were no exceptions
}
catch (Exception e)
{
Log.w("Exception:", e);
} finally {
db.endTransaction();
}
the b section should start from 1 and go up, but et fails at repeating 161 as you can see.
【问题讨论】:
-
这条线对我来说没有意义。
statement.bindString(3, childname_b + h);。它将覆盖每次迭代中的值,最终将其设置为 161。也许您可以告诉我们是否还有更多列要绑定这些变量。 -
删除内部 for 循环并将其更改为
statement.bindString(3, childname_b + j);,而不是像第一个 for 循环中所做的那样。