保存到sqlite数据库时出错

时间:2012-06-05 06:14:17

标签: android database sqlite

我的错误日志

    06-05 05:56:04.502: D/HealthlyLifestyleAppMain(278): Opened DB in onCreate
    06-05 05:56:04.522: D/HealthlyLifestyleAppMain(278): Loading DB contents: 29 items
    06-05 05:56:04.541: D/HealthlyLifestyleAppMain(278): Loading DB contents: 0 items
    06-05 05:56:04.541: D/HealthlyLifestyleAppMain(278): Closed DB in onCreate        
    06-05 05:56:06.451: D/HealthlyLifestyleAppMain(278): Opened DB in onPause
    06-05 05:56:06.532: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2888
    06-05 05:56:06.551: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2948
    06-05 05:56:06.571: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef29c0
    06-05 05:56:06.591: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2a38
    06-05 05:56:06.611: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2ab0
    06-05 05:56:06.621: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2b28
    06-05 05:56:06.641: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2ba0
    06-05 05:56:06.661: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2c18
    06-05 05:56:06.681: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2c90
    06-05 05:56:06.701: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2d08
    06-05 05:56:06.721: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2d80
    06-05 05:56:06.751: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2df8
    06-05 05:56:06.761: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2e70
    06-05 05:56:06.771: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2f48
    06-05 05:56:06.822: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef2fc0
    06-05 05:56:06.831: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3038
    06-05 05:56:06.875: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef30b0
    06-05 05:56:06.892: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3128
    06-05 05:56:06.901: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef31a0
    06-05 05:56:06.912: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3298
    06-05 05:56:06.931: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3310
    06-05 05:56:06.961: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3388
    06-05 05:56:06.971: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3400
    06-05 05:56:06.981: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3478
    06-05 05:56:06.991: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef34f0
    06-05 05:56:07.021: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3568
    06-05 05:56:07.051: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef35e0
    06-05 05:56:07.062: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3658
    06-05 05:56:07.071: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44ef3788
    06-05 05:56:07.081: D/HealthyLifestyleAppMain(278): Saved item: sg.edu.tp.iit.mns.LogItem@44f1ee10
    06-05 05:56:07.111: D/HealthlyLifestyleAppMain(278): Closed DB in onPause
    06-05 05:56:15.462: D/dalvikvm(278): GC_FOR_MALLOC freed 4966 objects / 257248 bytes in 118ms
    06-05 05:56:59.081: W/KeyCharacterMap(278): No keyboard for id 0
    06-05 05:56:59.081: W/KeyCharacterMap(278): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
    06-05 05:57:38.452: I/Database(278): sqlite returned: error code = 1, msg = table fruitTable has no column named _fruitcalorie
    06-05 05:57:38.552: E/Database(278): Error inserting _fruitname=Apple _fruitnum=2 _fruitcalorie=160
    06-05 05:57:38.552: E/Database(278): android.database.sqlite.SQLiteException: table fruitTable has no column named _fruitcalorie: , while compiling: INSERT INTO fruitTable(_fruitname, _fruitnum, _fruitcalorie) VALUES(?, ?, ?);
    06-05 05:57:38.552: E/Database(278):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
    06-05 05:57:38.552: E/Database(278):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
    06-05 05:57:38.552: E/Database(278):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
    06-05 05:57:38.552: E/Database(278):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
    06-05 05:57:38.552: E/Database(278):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
    06-05 05:57:38.552: E/Database(278):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
    06-05 05:57:38.552: E/Database(278):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
    06-05 05:57:38.552: E/Database(278):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
    06-05 05:57:38.552: E/Database(278):    at sg.edu.tp.iit.mns.FruitDB.createEntry(FruitDB.java:74)
    06-05 05:57:38.552: E/Database(278):    at sg.edu.tp.iit.mns.Fruit$3$1.launchIntent2(Fruit.java:133)
    06-05 05:57:38.552: E/Database(278):    at sg.edu.tp.iit.mns.Fruit$3$1.onClick(Fruit.java:120)
    06-05 05:57:38.552: E/Database(278):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
    06-05 05:57:38.552: E/Database(278):    at android.os.Handler.dispatchMessage(Handler.java:99)
    06-05 05:57:38.552: E/Database(278):    at android.os.Looper.loop(Looper.java:123)
    06-05 05:57:38.552: E/Database(278):    at android.app.ActivityThread.main(ActivityThread.java:4627)
    06-05 05:57:38.552: E/Database(278):    at java.lang.reflect.Method.invokeNative(Native Method)
    06-05 05:57:38.552: E/Database(278):    at java.lang.reflect.Method.invoke(Method.java:521)
    06-05 05:57:38.552: E/Database(278):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    06-05 05:57:38.552: E/Database(278):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    06-05 05:57:38.552: E/Database(278):    at dalvik.system.NativeStart.main(Native Method)

我的java文件

    public class Fruit extends Activity implements OnClickListener, OnItemSelectedListener {
private TextView tvFruit, tvNo,tvtotal;
Context context=this;   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //for fullscreen view
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.fruit);

    tvFruit = (TextView) findViewById(R.id.tvfruit);
    tvNo = (TextView) findViewById(R.id.tvno);
    tvtotal = (TextView)findViewById(R.id.tvtotal);


    final Spinner fruits = (Spinner)findViewById(R.id.spin_fruit);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this,R.array.fruits,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    fruits.setAdapter(adapter);
    fruits.setOnItemSelectedListener(this);

    fruits.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
            // TODO Auto-generated method stub
            String selectedItem = fruits.getSelectedItem().toString();
            tvFruit.setText(selectedItem);


        }


    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

    });

    final Spinner num = (Spinner)findViewById(R.id.spin_no);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            this,R.array.total,android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    num.setAdapter(adapter1);
    num.setOnItemSelectedListener(this);

    num.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
                // TODO Auto-generated method stub
                String selectedItem = num.getSelectedItem().toString();
                tvNo.setText(selectedItem);
                tvtotal.setText(String.valueOf(calculateCalories()));
            }

        public int calculateCalories() {
            int[] calorie = getResources().getIntArray(R.array.calorie);
            return Integer.parseInt((String) num.getSelectedItem()) * calorie[fruits.getSelectedItemPosition()];
        }



        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

        });

    Button save = (Button) findViewById(R.id.bsave);
    save.setTextColor(Color.BLUE);
    save.setOnClickListener(new View.OnClickListener() {


            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                // Get the subject details and show it in an alertdialog
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Success");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        launchIntent2();
                    }

                    private void launchIntent2() {
                        // TODO Auto-generated method stub

                        try{
                        String fruit =tvFruit.getText().toString();
                        String number =tvNo.getText().toString();
                        String calorie =tvtotal.getText().toString();

                        FruitDB entry = new FruitDB(Fruit.this);
                        entry.open();
                        entry.createEntry(fruit, number, calorie);
                        entry.close();
                        }
                        catch(Exception e){

                        }
                    }
                });


                builder.setNegativeButton("View Log", new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface dialog, int which) { // this part is done for the negative button 
                                                                             // if we want it to link to new intent
                        launchIntent();
                    }

                });
                builder.create().show();
            }

            //making the "View Log" button in dialog box to go to new intent FruitLog.class
            private void launchIntent(){
                Intent i = new Intent(Fruit.this, FruitLog.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }


        });

    }

我的数据库文件:

 public class FruitDB extends Activity{

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "_fruitname";
public static final String KEY_NUMBER = "_fruitnum";
public static final String KEY_CALORIE = "_fruitcalorie";

private static final String DATABASE_NAME = "Fruitdb";
private static final String DATABASE_TABLE = "fruitTable";
private static final int DATABASE_VERSION = 1;

private DbHelper newHelper;
private final Context ourContext1;
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_NAME + " TEXT NOT NULL, " +
                    KEY_NUMBER + " TEXT NOT NULL, " +
                    KEY_CALORIE + " 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 FruitDB(Context c){
    ourContext1 = c;
}

public FruitDB open()throws SQLException{
    newHelper = new DbHelper(ourContext1);
    ourDatabase = newHelper.getWritableDatabase();
    return this;
}

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

public long createEntry(String fruit, String number, String calorie) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, fruit);
    cv.put(KEY_NUMBER, number);
    cv.put(KEY_CALORIE, calorie);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

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

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iRating = c.getColumnIndex(KEY_NUMBER);
    int iCalorie = c.getColumnIndex(KEY_CALORIE);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        //result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iRating) + " \n";
        result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iRating) + " " + c.getString(iCalorie) + " \n";
                }
    return result;
}   
}
当我试图将总卡路里插入我的数据库时,我有错误。在总卡路里之前,它正在工作。我不确定错误。有人请帮帮我。

1 个答案:

答案 0 :(得分:1)

如果您更新了表格结构并添加了新列,那么您需要卸载应用程序并重新安装它以实现更改。

要卸载转到设置 - >应用程序 - >管理应用程序 - >选择您的应用程序 - &gt;卸载/ ClearData