在SQLite问题中求和

时间:2014-04-19 14:05:59

标签: java android eclipse

我需要帮助总结我的数据库中的一列(animalName)中的所有值。我已经尝试并参考了许多其他例子,但还没有弄清楚它是如何工作的或已经完成的。这是我的数据库类

public class DBController  extends SQLiteOpenHelper {
private static final String LOGCAT = null;


public DBController(Context applicationcontext) {
    super(applicationcontext, "androidsqlite.db", null, 4);
    Log.d(LOGCAT,"Created");
}

@Override
public void onCreate(SQLiteDatabase database) {
    String query;
    query = "CREATE TABLE animals ( animalId INTEGER PRIMARY KEY, animalName INTEGER, animalColor TEXT, animalType TEXT)";
    database.execSQL(query);
    Log.d(LOGCAT,"animals Created");

}


@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    String query;
    query = "DROP TABLE IF EXISTS animals";
    database.execSQL(query);
    onCreate(database);
}

public void insertAnimal(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("animalName", queryValues.get("animalName"));
    values.put("animalColor", queryValues.get("animalColor"));
    values.put("animalType", queryValues.get("animalType"));
    database.insert("animals", null, values);
    database.close();
}

public int updateAnimal(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();    
    ContentValues values = new ContentValues();
    values.put("animalName", queryValues.get("animalName"));
    values.put("animalColor", queryValues.get("animalColor"));
    values.put("animalType", queryValues.get("animalType"));
    return database.update("animals", values, "animalId" + " = ?", new String[] { queryValues.get("animalId") });
    //String updateQuery = "Update  words set txtWord='"+word+"' where txtWord='"+ oldWord +"'";
    //Log.d(LOGCAT,updateQuery);
    //database.rawQuery(updateQuery, null);
    //return database.update("words", values, "txtWord  = ?", new String[] { word });
}

public void deleteAnimal(String id) {
    Log.d(LOGCAT,"delete");
    SQLiteDatabase database = this.getWritableDatabase();    
    String deleteQuery = "DELETE FROM  animals where animalId='"+ id +"'";
    Log.d("query",deleteQuery);     
    database.execSQL(deleteQuery);
}

public ArrayList<HashMap<String, String>> getAllAnimals() {
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT  * FROM animals";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("animalId", cursor.getString(0));
            map.put("animalName", cursor.getString(1));
            map.put("animalColor", cursor.getString(2));
            map.put("animalType", cursor.getString(3));
            wordList.add(map);
        } while (cursor.moveToNext());
    }

    // return contact list
    return wordList;
}



public HashMap<String, String> getAnimalInfo(String id) {
    HashMap<String, String> wordList = new HashMap<String, String>();
    SQLiteDatabase database = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM animals where animalId='"+id+"'";
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
                //HashMap<String, String> map = new HashMap<String, String>();
            wordList.put("animalName", cursor.getString(1));
            wordList.put("animalColor", cursor.getString(2));
            wordList.put("animalType", cursor.getString(3));
               //wordList.add(map);
        } while (cursor.moveToNext());
    }                   
return wordList;
}

public Cursor getTotal() {
    SQLiteDatabase database = this.getReadableDatabase();
    return database.rawQuery(
        "SELECT SUM(animalName) as sum FROM animals", null);
}

}

这是我的主要活动类

public class MainActivity extends ListActivity {
Cursor cursor;
Intent intent;
TextView animalId;
DBController controller = new DBController(this);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    ArrayList<HashMap<String, String>> animalList =  controller.getAllAnimals();
    if(animalList.size()!=0) {
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
              @Override 
              public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                  animalId = (TextView) view.findViewById(R.id.animalId);
                  String valAnimalId = animalId.getText().toString();                     
                  Intent  objIndent = new Intent(getApplicationContext(),EditAnimal.class);
                  objIndent.putExtra("animalId", valAnimalId); 
                  startActivity(objIndent); 
              }
        }); 
        ListAdapter adapter = new SimpleAdapter( MainActivity.this,animalList, R.layout.view_animal_entry, new String[] { "animalId","animalName","animalColor","animalType"}, new int[] {R.id.animalId, R.id.animalName, R.id.animalColor, R.id.animalType}); 
        setListAdapter(adapter);
    }
}
public void showAddForm(View view) {
    Intent objIntent = new Intent(getApplicationContext(), NewAnimal.class);
    startActivity(objIntent);
}

}

如果有人可以通过一些解释帮我解决这个问题,那将是很棒的。谢谢

2 个答案:

答案 0 :(得分:1)

试试这种方式

Cursor c = db.rawQuery("select sum(animalName) from animals;",null);
 if(c.moveToFirst())
  total = c.getInt(0);
else
  total = -1;
c.close();

答案 1 :(得分:0)

总结名称没有意义。如果您想知道有多少,请使用COUNT而不是SUM。