所以,我想从我的SQLite数据库拉称为totalWater一个整数,但是当我试图把它然后将其添加到另一个号码,这表明,我从数据库中抽取的数量是零。我不知道如果我揪错了,或者如果我连其存储到数据库中是错误的。
So I am trying to pull an integer called "totalWater" from my SQLite database but when I try to pull it and then add it to another number, it shows that the number that I pulled from the database is zero. I'm not sure if I am pulling wrong, or if I am even storing the value into the database wrong.
这是我的code处理拉,并与数据库推:
here is my code dealing with pulling and pushing with the database:
//pull data
data = new Database(SetupTimerPC1.this);
data.open();
totalWater = data.getWaterAmt();
data.close();
//add new data to old
totalWater+=waterAmt;
//push data
data.open();
data.waterAmountEntry(totalWater);
data.close();
这里在数据库中的功能处理waterAmt:
here is the function in the database dealing with waterAmt:
public int getWaterAmt()
{
int waterAmount = 0;
String[] columns = new String[] {KEY_ROWID, KEY_ROCK, KEY_METEORROCK, KEY_WATER,
KEY_POPULATION, KEY_SHOVEL, KEY_BUCKET, KEY_PICKAXE, KEY_SHOVELEXP,
KEY_BUCKETEXP, KEY_PICKAXEEXP};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
if (c != null && c.getCount() > 0)
{
c.moveToFirst();
iWaterAmount = c.getColumnIndex(KEY_WATER);
waterAmount = c.getInt(iWaterAmount);
return waterAmount;
}//end if return 0;
}//end getWaterAmt function
和这里是我的waterAmountEntry功能进入的水量到数据库:
and here is my waterAmountEntry function to enter the amount of water into the database:
public long waterAmountEntry(int waterAmt)
{
ContentValues cv = new ContentValues();
cv.put(KEY_WATER, waterAmt);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}//end waterAmountEntry function
任何帮助将AP preciated。谢谢你。
Any help will be appreciated. Thanks.
添加一个for循环,在c.moveToFirst()通过c.movetonext开始!c.isafterlast()和增量的条件()里面我if语句检查是否光标在空让我来解决这个问题:
Adding a for loop that started at c.moveToFirst() with a condition of !c.isafterlast() and increment by c.movetonext() inside of my if statement checking if the cursor was null allowed me to fix the issue:
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
iWaterAmount = c.getColumnIndex(KEY_WATER);
waterAmount = c.getInt(iWaterAmount);
}//end for
c.close();