因为我是android新手。我正在开发基于单品库存管理的小应用程序。用户可以添加/删除库存,并可以检查当前库存。我试图从数据库中获取单个数据的代码,但不准确。库存已成功添加/删除到数据库中。但是当我杀死或重新启动应用程序时,我需要的最后一个股票价值无法找到它们。
这是我的数据库文件DataBaseHandler.java
package com.example.stockmanager;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHandler extends SQLiteOpenHelper{
private SQLiteDatabase sql=null;
private static String CREATE_TABLE="";
public DataBaseHandler(Context context)
{
super(context, Util.DATABASE_NAME, null, Util.DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
CREATE_TABLE = "CREATE TABLE " +Util.STOCK_TABLE + "("
+Util.USER_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "
+Util.CURRENT_STOCK +" TEXT NOT NULL, "
+Util.DATE +" TEXT NOT NULL, "
+Util.TIME +" TEXT NOT NULL" + ")";
db.execSQL(CREATE_TABLE);
Log.i("Authentication", ""+CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " +Util.STOCK_TABLE);
}
public void insertDetails(Model m)
{
sql=this.getWritableDatabase();
ContentValues cn=new ContentValues();
cn.put(Util.CURRENT_STOCK, m.getCurrentStock());
cn.put(Util.DATE, m.getDate().toString());
cn.put(Util.TIME, m.getTime().toString());
long insertId=sql.insert(Util.STOCK_TABLE, null, cn);
Log.i("new raw inserted", ""+insertId);
}
public List<Model> getAllDetails()
{
List<Model> show=new ArrayList<Model>();
sql=this.getReadableDatabase();
Cursor cursor=null;
String query="SELECT * FROM" +Util.STOCK_TABLE;
cursor=sql.rawQuery(query, null);
cursor.moveToFirst();
while(!cursor.isAfterLast())
{
Model m=new Model();
m.setCurrentStock(cursor.getString(0));
m.setDate(cursor.getString(1));
m.setTime(cursor.getString(2));
show.add(m);
cursor.moveToNext();
}
cursor.close();
sql.close();
return show;
}
public Model getLastStock(int id) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query(Util.STOCK_TABLE, new String[] {Util.CURRENT_STOCK}, Util.USER_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Model m = new Model();
m.setCurrentStock(cursor.getString(0));
Log.i("Message of Current Stock", ""+m);
return m;
}
}
方法getLastStock()被编码为获取最后输入的股票值以进行进一步操作。
这是我的java文件,用于添加/删除库存Add_Remove.java
package com.example.stockmanager;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Add_Remove extends Activity implements OnClickListener{
private Button btn1,btn2;
private EditText edt;
private DataBaseHandler data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__remove);
btn1=(Button)findViewById(R.id.button4);
btn2=(Button)findViewById(R.id.button5);
edt=(EditText)findViewById(R.id.editText1);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add__remove, menu);
return true;
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String number=edt.getText().toString();
int count1=Integer.parseInt(number);
Model m=new Model();
data=new DataBaseHandler(getApplicationContext());
String ss= m.getCurrentStock();
Log.i("CURRENT_STOCK", ""+ss);
int count2;
//While Stock is Added
if(v.equals(btn1))
{
Calendar ci = Calendar.getInstance();
//Getting Date
String CiDate = "" + ci.get(Calendar.DAY_OF_MONTH) + "-" +
(ci.get(Calendar.MONTH) + 1) + "-" +
ci.get(Calendar.YEAR);
//Getting Time
String CiTime =ci.get(Calendar.HOUR) + ":" +
ci.get(Calendar.MINUTE) + ":" +
ci.get(Calendar.SECOND);
if(ss==null)
{
count2=0;
Log.i("Count1", ""+count1);
Log.i("Count2", ""+count2);
int add=count1+count2;
Log.i("ADDED VALUE:", ""+add);
String stock=String.valueOf(add);
m.setCurrentStock(stock);
m.setDate(CiDate);
m.setTime(CiTime);
data.insertDetails(m);
Toast.makeText(this, "Added Successfully", Toast.LENGTH_SHORT).show();
}
else
{
count2=Integer.parseInt(ss);
Log.i("Count1", ""+count1);
Log.i("Count2", ""+count2);
int add=count1+count2;
Log.i("ADDED VALUE:", ""+add);
String stock=String.valueOf(add);
m.setCurrentStock(stock);
m.setDate(CiDate);
m.setTime(CiTime);
data.insertDetails(m);
Toast.makeText(this, "Added Successfully", Toast.LENGTH_SHORT).show();
}
}
//While Stock is Deducted
if(v.equals(btn2))
{
Calendar ci = Calendar.getInstance();
//Getting Date
String CiDate = "" + ci.get(Calendar.DAY_OF_MONTH) + "-" +
(ci.get(Calendar.MONTH) + 1) + "-" +
ci.get(Calendar.YEAR);
//Getting Time
String CiTime =ci.get(Calendar.HOUR) + ":" +
ci.get(Calendar.MINUTE) + ":" +
ci.get(Calendar.SECOND);
if(ss==null)
{
count2=0;
Log.i("Count1", ""+count1);
Log.i("Count2", ""+count2);
int add=count1+count2;
Log.i("ADDED VALUE:", ""+add);
String stock=String.valueOf(add);
m.setCurrentStock(stock);
m.setDate(CiDate);
m.setTime(CiTime);
data.insertDetails(m);
Toast.makeText(this, "Added Successfully", Toast.LENGTH_SHORT).show();
}
else
{
count2=Integer.parseInt(ss);
Log.i("Count1", ""+count1);
Log.i("Count2", ""+count2);
int add=count1-count2;
Log.i("ADDED VALUE:", ""+add);
String stock=String.valueOf(add);
m.setCurrentStock(stock);
m.setDate(CiDate);
m.setTime(CiTime);
data.insertDetails(m);
Toast.makeText(this, "Deducted Successfully", Toast.LENGTH_SHORT).show();
} }
}
}
帮助我使用正确的数据库代码从数据库中获取最后一个库存值。这样我就可以进一步添加或删除库存。
答案 0 :(得分:0)
应该是这样的:
select max(USER_ID), CURRENT_STOCK from STOCK_TABLE group by CURRENT_STOCK