java如何在SQLite中求和我的列值?
我知道这个问题已经被问过好几次了,但我就是找不到正确的语法来解决我的问题
我想总结一下我对“艾纳曼”的价值
这是我的SqlDbHelper类:
public class SqlDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_TABLE = "PHONE_CONTACTS";
public static final String COLUMN1 = "nr";
public static final String COLUMN2 = "name";
public static final String COLUMN3 = "einnahmen";
public static final String COLUMN4 = "ausgaben";
public static final String COLUMN5 = "datum";
private static final String SCRIPT_CREATE_DATABASE =
"create table "+ DATABASE_TABLE
+ " (" + COLUMN1+ " integer primary key autoincrement, "
+ COLUMN2+ " text not null, "
+ COLUMN3 + " text not null, "
+ COLUMN4 + " text not null, "
+ COLUMN5 + " text not null);";
public SqlDbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
这是我的主要活动:
public class BookActivity extends Activity {
SqlHandler sqlHandler;
ListView lvCustomList;
EditText etName, etEinnahmen, etAusgaben, etDatum;
ImageButton btn_add;
TextView gesamteinnahmen;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
gesamteinnahmen = (TextView) findViewById(R.id.tVgesamtEinnahmen);
lvCustomList = (ListView) findViewById(R.id.lv_custom_list);
etName = (EditText) findViewById(R.id.et_name);
etEinnahmen = (EditText) findViewById(R.id.et_einnahmen);
etAusgaben = (EditText) findViewById(R.id.et_ausgaben);
etDatum = (EditText) findViewById(R.id.et_datum);
btn_add = (ImageButton) findViewById(R.id.btn_add);
sqlHandler = new SqlHandler(this);
showList();
btn_add.setOnClickListener(new OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
String name = etName.getText().toString();
String einnahmen = etEinnahmen.getText().toString();
String ausgaben = etAusgaben.getText().toString();
String datum = etDatum.getText().toString();
if (TextUtils.isEmpty(name)) {etName.setError("Das Feld ist leer");return;}
else if (TextUtils.isEmpty(einnahmen)) {etEinnahmen.setError("Das Feld ist leer");return;
} else if (TextUtils.isEmpty(ausgaben)) {etAusgaben.setError("Das Feld ist leer");return;
} else if (TextUtils.isEmpty(datum)) {etDatum.setError("Das Feld ist leer");return;
} else {
Toast.makeText(BookActivity.this,"Daten wurden erfolgreich gespeichert", Toast.LENGTH_SHORT).show();
String query = "INSERT INTO PHONE_CONTACTS(name,einnahmen,ausgaben,datum) values " +
"('" + name + "','" + einnahmen +"','" + ausgaben + "','" + datum + "')";
sqlHandler.executeQuery(query);
showList();
etName.setText("");
etEinnahmen.setText("");
etAusgaben.setText("");
etDatum.setText("");
}
}
});
}
private void showList() {
ArrayList contactList = new ArrayList();
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
ContactListItems contactListItems = new ContactListItems();
contactListItems.setNr(c1.getString(c1.getColumnIndex("nr")));
contactListItems.setName(c1.getString(c1.getColumnIndex("name")));
contactListItems.setEinnahmen(c1.getString(c1.getColumnIndex("einnahmen")));
contactListItems.setAusgaben(c1.getString(c1.getColumnIndex("ausgaben")));
contactListItems.setDatum(c1.getString(c1.getColumnIndex("datum")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
ContactListAdapter contactListAdapter = new ContactListAdapter(
BookActivity.this, contactList);
lvCustomList.setAdapter(contactListAdapter);}
}
# 1 楼答案
在SqlDbHelper类中:
请注意,您的列当前声明为
text
列,而不是integer
或real
列。如果只在该列中存储数值数据,则应使用这些数值类型中的任何一种进行声明# 2 楼答案
我希望这段代码能帮助你
您的sql查询应该如下所示
query=“选择*,SUM(einnahmen)作为来自电话会议的SUM”