Android 如何在同一个SQLite数据库的不同表中保存两组信息?
我正在开发一个预算应用程序,我想请用户添加收入和支出,然后我想在概览活动中获得这两组数据的总和。我在网上进行了研究,发现将这些值放在两个不同的表中是最好的方法。(如果你有任何其他想法,请告诉我。)Android 如何在同一个SQLite数据库的不同表中保存两组信息?,android,sqlite,Android,Sqlite,我正在开发一个预算应用程序,我想请用户添加收入和支出,然后我想在概览活动中获得这两组数据的总和。我在网上进行了研究,发现将这些值放在两个不同的表中是最好的方法。(如果你有任何其他想法,请告诉我。) 我如何将收入和支出放在不同的表格上,然后如何将它们联系起来以获得总额? 我已将费用数据保存在一个表中,但不知道如何制作另一个收入表。请原谅我不知道最好的方法,因为我对使用SQLite还不熟悉 添加费用。java import android.content.Intent; import android
我如何将收入和支出放在不同的表格上,然后如何将它们联系起来以获得总额?
我已将费用数据保存在一个表中,但不知道如何制作另一个收入表。请原谅我不知道最好的方法,因为我对使用SQLite还不熟悉 添加费用。java
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class add_expense extends AppCompatActivity {
DatabaseHelper myDbexpense;
EditText editAmountexpense, editDateexpense, editNotesexpense;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_expense);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_expense);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Add Expense");
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_back));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myDbexpense = new DatabaseHelper(this);
editAmountexpense = (EditText) findViewById(R.id.amount_expense);
editDateexpense = (EditText) findViewById(R.id.date_expense);
editNotesexpense = (EditText) findViewById(R.id.notes_expense);
AddData();
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
spinner = (Spinner) findViewById(R.id.categorydropdown_expense);
adapter = ArrayAdapter.createFromResource(this, R.array.expensecategory_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected as category", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
}
public void AddData(){
FloatingActionButton fab2 = (FloatingActionButton) findViewById(R.id.fab_tick_expense);
fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean isInserted;
if (myDbexpense.insertData(
editAmountexpense.getText().toString(),
editDateexpense.getText().toString(),
editNotesexpense.getText().toString())) {
isInserted = true;
Intent intent=new Intent(add_expense.this,MainActivity.class);
startActivity(intent);
}
else isInserted = false;
if(isInserted == true)
Toast.makeText(add_expense.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(add_expense.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
});
}
}
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
public static final String DATABASE_NAME = "expense.db";
public static final String TABLE_NAME = "expense_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "AMOUNT";
public static final String COL_3 = "DATE";
public static final String COL_4 = "NOTES";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 3);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String amount, String date, String notes) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, amount);
contentValues.put(COL_3, date);
contentValues.put(COL_4, notes);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL_1 + " FROM " + TABLE_NAME + " WHERE " + COL_2 + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public boolean updateData(String id, String amount, String date, String notes) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, amount);
contentValues.put(COL_3, date);
contentValues.put(COL_4, notes);
db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});
return true;
}
public Integer deleteData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?", new String[]{id});
}
}
在
DatabaseHelper
类的onCreate
方法中创建两个表,并相应地处理插入/读取。或者,如果要使用单个表,请使用负值表示支出,正值表示收入
有很多方法可以解决这个问题。您只需要设计并决定如何实现它