在为android编程时,在sqlite数据库中更新无法正常工作

时间:2014-10-03 16:16:31

标签: android sqlite

我实际上是android编程的初学者。当我尝试更新我的数据库时,它不会更新。 我自己找不到错误。请帮我!!!  这是我的代码:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.style.UpdateAppearance;



public class Inventorydb{
public static final String KEY_name = "persons_firstname";
public static final String KEY_id = "dob";
public static final String KEY_price = "price";
public static final String KEY_quan = "quan";

private static final String DATABASE_NAME = "HotorNo.db";
private static final String DATABASE_TABLE = "peopletable";
private static final int DATABASE_VERSION = 1;

private DbHelper OurHelper;
private Context OurContext;
private SQLiteDatabase OurDatabase;

public static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL("CREATE TABLE " + DATABASE_TABLE + "( " +
                // KEY_ROWID + " INTEGER AUTOINCREMENT," +
                KEY_name + " TEXT NOT NULL," +
                KEY_id + " TEXT NOT NULL," +
                KEY_price + " TEXT, "+
                KEY_quan + " TEXT,"+
                " PRIMARY KEY( " + KEY_id + " )" +
                " ) ;"
                );
    }

    @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 Inventorydb(Context c) {
    OurContext = c ;
}

public Inventorydb open() throws SQLException{
    OurHelper = new DbHelper(OurContext);
    OurDatabase = OurHelper.getWritableDatabase();
    return this;
}

public void close(){
    OurHelper.close();
   }

    public long createEntry(String name, String date,String d,String g) {
    // TODO Auto-generated method stub

    ContentValues cv = new ContentValues();

    name = name.trim();
    date = date.trim();
    d=d.trim();
    g=g.trim();
    if(!name.contentEquals(""))
    cv.put(KEY_name, name);

    if(!date.contentEquals(""))
    cv.put(KEY_id, date);

    cv.put(KEY_price, d);
    cv.put(KEY_quan, g);

    return OurDatabase.insert(DATABASE_TABLE, null, cv);
        }

    public String getData() {
    // TODO Auto-generated method stub
    String result = " ";

    String[] columns = new String[]{KEY_name, KEY_id,KEY_price,KEY_quan};

    Cursor c = OurDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

    if(c==null)
        return result;

    int iName = c.getColumnIndex(KEY_name);
    // int iRow = c.getColumnIndex(KEY_ROWID);
    int iD = c.getColumnIndex(KEY_id);
    int ip=c.getColumnIndex(KEY_price);
    int iq=c.getColumnIndex(KEY_quan);
    for( c.moveToFirst(); !c.isAfterLast() ; c.moveToNext() )
    {
        result = result + c.getString(iName) + "\t" +
                c.getString(iD) + "\t"+c.getString(ip)+"\t"+c.getString(iq)+"\t"+"\n" ;
    }

    return result;
}
public String getprice(String iid) {
    // TODO Auto-generated method stub
    String[] columns=new String[]{KEY_id,KEY_name,KEY_price,KEY_quan};
    iid=iid.trim();
    String price,quantity,temp="";
    Cursor c=OurDatabase.query(DATABASE_TABLE, columns,null, null, null, null, null);
    if(c!=null){
        for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
             temp=c.getString(1);
            if(temp.contains(iid)){
              price=c.getString(2);
              return price;
            }
        }
    }
    return null;
}

public String getquan(String iid) {
    // TODO Auto-generated method stub
    String[] columns=new String[]{KEY_id,KEY_name,KEY_price,KEY_quan};
    iid=iid.trim();
    String quantity,temp="";
    Cursor c=OurDatabase.query(DATABASE_TABLE, columns,null, null, null, null, null);
    if(c!=null){
        for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
             temp=c.getString(1);
            if(temp.contains(iid)){
              quantity=c.getString(3);
              return quantity;
            }
        }
    }
    return null;
}

public String getname(String iid) {
    // TODO Auto-generated method stub
    String[] columns=new String[]{KEY_id,KEY_name,KEY_price,KEY_quan};
    iid=iid.trim();
    String quantity,temp="";
    Cursor c=OurDatabase.query(DATABASE_TABLE, columns,null, null, null, null, null);
    if(c!=null){
        for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
             temp=c.getString(1);
            if(temp.contains(iid)){
              quantity=c.getString(0);
              return quantity;
            }
        }
    }
    return null;
 }

 public void updatetable(String l, String price, String name, int i) {
    // TODO Auto-generated method stub
    ContentValues cvup=new ContentValues();
    cvup.put(KEY_id, l);
    cvup.put(KEY_name, name);
    cvup.put(KEY_price, price);
    cvup.put(KEY_quan, i);
    OurDatabase.update(DATABASE_TABLE, cvup,  KEY_quan  + "=" + l, null);
 }
}

这是我使用此java代码的Activity类:

   public class Userdata extends Activity implements View.OnClickListener{
   EditText id,total,number;
   Button another;
    int result=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userdata);
    id=(EditText)findViewById(R.id.etid);
    number=(EditText)findViewById(R.id.etnoofitem);
    total=(EditText)findViewById(R.id.etamount);
    another=(Button)findViewById(R.id.addanother);
    another.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.addanother:String iid,nofitems;
    iid=id.getText().toString();
    nofitems=number.getText().toString();
    Inventorydb entry=new Inventorydb(this);
    entry.open();
    String price=entry.getprice(iid);
    String quantity=entry.getquan(iid);
    int t=Integer.parseInt(quantity);
    String name=entry.getname(iid);
    int q=Integer.parseInt(nofitems);
    long l=Long.parseLong(iid);
    int h=t-q;
    entry.updatetable(iid,price,name,h);
    entry.close();
    int p=Integer.parseInt(price);

    int temp=p*q;
    result=result+temp;
    total.setText(" "+result  );
    id.setText("");
    number.setText("");
    }

  }
}

我正在为我的迷你项目创建一个库存数据库。我已经尝试过使用execsql和rawquery语句,但那些也没有用。我认为有些事情我不能正确理解。

3 个答案:

答案 0 :(得分:0)

OurDatabase.update(DATABASE_TABLE, cvup,  KEY_quan  + "=" + l, null);

ID列由KEY_id命名。 此外,字符串值必须在SQL中引用,或者更好地作为参数给出:

OurDatabase.update(DATABASE_TABLE, cvup, KEY_d + "=?", new String[]{l});

答案 1 :(得分:0)

您发布的代码将更新“KEY_quan”字段等于“l”参数的所有记录,该参数实际上是KEY_id。可能没有符合此条件的记录(或多个记录)。

您应该在调用SQLiteDatabase.update时更改'whereClause'参数,以根据'KEY_id'而不是'KEY_quan'来识别单个现有记录。

您真的想更新主键吗?

如果没有,您应该将ID字段保存在变量或不可编辑的TextView中,然后重写'updatetable'函数,如下所示:

public void updatetable(String l, String price, String name, int i) {
    ContentValues cvup=new ContentValues();
    cvup.put(KEY_name, name);
    cvup.put(KEY_price, price);
    cvup.put(KEY_quan, i);
    OurDatabase.update(DATABASE_TABLE, cvup,  KEY_id  + "=?", new String[]{l});
 }

如果是这样,您应该将原始ID字段保存在变量中,然后重写'updatetable'函数,如下所示:

public void updatetable(String oldId, String newId, String price, String name, int i) {
    ContentValues cvup=new ContentValues();
    cvup.put(KEY_id, newId);
    cvup.put(KEY_name, name);
    cvup.put(KEY_price, price);
    cvup.put(KEY_quan, i);
    OurDatabase.update(DATABASE_TABLE, cvup,  KEY_id  + "=?", new String[]{oldId});
 }

如果您只想更新KEY_quan字段:

public void updateQuantity(String iid, int quan) {
        ContentValues cvup=new ContentValues();
        cvup.put(KEY_quan, quan);
        OurDatabase.update(DATABASE_TABLE, cvup,  KEY_id  + "=?", new String[]{iid});
     }

作为旁注,当你想要检索值时,你应该再次识别你想要的记录,而不是循环遍历整个表。

public String getquan(String iid) {
        Cursor c=OurDatabase.query(DATABASE_TABLE, new String[]{KEY_quan}, KEY_id+"=?", new String[]{iid}, null, null, null);
        try {
            c.moveToFirst();
            return c.getString(0);
        } finally {
            c.close();
        }
    }

答案 2 :(得分:-1)

这有时会发生在SQLite数据库中,它无法识别您所做的更新,只需更改数据库的名称就像, private static final String DATABASE_NAME =" HotorNoabc.db" 然后运行你的应用程序。