2012-03-01 91 views
0

嗨,朋友。我試圖在複選框選中時將產品添加到SQLlite數據庫。 這裏是我的SQL類:SQLite dB Android永遠不會更新

package com.pizzeria.uno; 

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.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 



public class SQLManager { 

public static final String KEY_ROWID = "_id"; 
public static final String KEY_PRODUCT = "product"; 
public static final String KEY_QUANTITY = "quantity"; 
public static final String KEY_PRICE = "price"; 

private static final String DATABASE_NAME = "PedidoDb"; 
private static final String DATABASE_TABLE = "PedidoTable"; 
private static final int DATABASE_VERSION = 1; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

private 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 PRIMARY KEY AUTOINCREMENT, " + 
       KEY_PRODUCT + " TEXT NOT NULL, " + 
       KEY_QUANTITY + " TEXT NOT NULL, " + 
       KEY_PRICE + " TEXT NOT NULL);" 

     ); 

    } 

    @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 SQLManager(Context c){ 

    ourContext = c; 

} 

public SQLManager open() throws SQLException{ 

    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
} 

    public void close(){ 

    ourHelper.close(); 

} 

    public long createEntry(String product, String quantity, String price) { 
// TODO Auto-generated method stub 
ContentValues cv = new ContentValues(); 
cv.put(KEY_PRODUCT, product); 
cv.put(KEY_QUANTITY, quantity); 
cv.put(KEY_PRICE, price); 
return ourDatabase.insert(DATABASE_TABLE, null, cv); 
    } 

    public String getData() { 
// TODO Auto-generated method stub 
String[] columns = new String[]{ KEY_ROWID, KEY_PRODUCT, KEY_QUANTITY, KEY_PRICE}; 
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null,  null); 
String result = ""; 

int iRow = c.getColumnIndex(KEY_ROWID); 
int iProduct = c.getColumnIndex(KEY_PRODUCT); 
int iQuantity = c.getColumnIndex(KEY_QUANTITY); 
int iPrice = c.getColumnIndex(KEY_PRICE); 

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
    result = result + c.getString(iRow) + " " + c.getString(iProduct) + " " + c.getString(iQuantity) + " " + c.getString(iPrice) + "\n"; 
} 

return result; 
    } 

    } 

這裏是我嘗試將數據添加到數據庫:

  package com.pizzeria.uno; 

      import android.app.Activity; 
      import android.app.Dialog; 
      import android.content.Intent; 
      import android.os.Bundle; 
      import android.view.View; 
      import android.widget.Button; 
      import android.widget.CheckBox; 
      import android.widget.CompoundButton; 
      import android.widget.CompoundButton.OnCheckedChangeListener; 
      import android.widget.TextView; 

      import com.pizzeriabritannia.com.R; 

    public class Vindaloo extends Activity { 


CheckBox cb; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.vindaloo); 


final CheckBox cb = (CheckBox)findViewById(R.id.checkBox1); 
cb.setText("Añadelo a mi pedido"); 
final Dialog d = new Dialog(this); 
final TextView tv = new TextView(this); 

cb.setOnCheckedChangeListener(
     new CheckBox.OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, 
                 boolean isChecked) { 
       if (isChecked) { 
        { 
         boolean didItWork = true; 
         try{ 
          String product = "Pork Vindaloo"; 
          String quantity = "1"; 
          String price = "25"; 

          SQLManager entry = new SQLManager(Vindaloo.this); 
          entry.open(); 
          entry.createEntry(product, quantity, price); 
          entry.close(); 

         }catch (Exception e){ 
          didItWork = false; 
          String error = e.toString(); 
          d.setTitle("Fuck"); 
          tv.setText(error); 
          d.setContentView(tv); 
          d.show(); 

         }finally{ 
          if (didItWork){ 

         d.setTitle("Producto Añadido"); 
         tv.setText("Success"); 
         d.setContentView(tv); 
         d.show(); 
          } 
         } 
        } 
       } 
       else { 
        cb.setText("Checkbox desmarcado!"); 
      } 
     } 
    }); 



    // TODO Auto-generated method stub 

} 

} 

最後它是用來查看德數據庫活動:

 package com.pizzeria.uno; 

     import android.app.Activity; 
     import android.os.Bundle; 
     import android.widget.TextView; 
     import com.pizzeriabritannia.com.R; 

     public class Visual extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.visual); 

    TextView tv = (TextView) findViewById(R.id.tvSQLinfo); 
    SQLManager info = new SQLManager(this); 
    info.open(); 
    String data= info.getData(); 
    info.close(); 
    tv.setText(data); 

    } 
} 

問題是,當我嘗試查看數據庫時,沒有添加任何內容。這很奇怪,因爲當我檢查de checkBox時,我可以看到帶有文本「成功」的對話框,就好像它做得很好。 任何想法?謝謝!

+0

嘗試調試! – Navdroid 2012-03-01 12:50:12

+0

當然,我試過調試,但沒有辦法... – Aldridge1991 2012-03-01 13:35:13

回答

1

你也可以試試。

public Integer createEntry(String product, String quantity, String price) { 

ContentValues cv = new ContentValues(); 
cv.put(KEY_PRODUCT, product); 
cv.put(KEY_QUANTITY, quantity); 
cv.put(KEY_PRICE, price); 

Integer id = (int) ourDatabase.insert(DATABASE_TABLE, null, cv) 
return id; 

}

檢查數據庫創建!

+0

謝謝你們倆!我的錯誤是一件愚蠢的事情。沒有看到這一點,只是dBView XML文件的錯誤。 – Aldridge1991 2012-03-01 21:25:24

+0

此信息可以被關閉。 – Aldridge1991 2012-03-01 21:25:41