0

已经在代码中创建了一个数据库,与我要粘贴的代码非常相似。我保持数据库名称相同,并想创建一个新表。谁能提供一些帮助,谢谢。我得到的错误是:

            04-28 00:03:10.182: I/Database(310): sqlite returned: error code = 1, msg =  
           no such table: stats
            04-28 00:03:10.202: E/Database(310): Error inserting username= height=  
              weight= protein= age= fats= calories= carbs=
              04-28 00:03:10.202: E/Database(310):  
              android.database.sqlite.SQLiteException: no such table: stats: , while  
              compiling: INSERT INTO stats(username, height, weight, protein, age, 
             fats, calories, carbs) VALUES(?, ?, ?, ?, ?, ?, ?, ?);

AddStats.Java

 package com.gofitness.www;

  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteDatabase.CursorFactory;
  import android.database.sqlite.SQLiteOpenHelper;

  public class AddStats {

public static final String KEY_ROWID = "userid";
public static final String KEY_NAME = "username";
public static final String KEY_WEIGHT = "weight";
public static final String KEY_HEIGHT = "height";
public static final String KEY_AGE = "age";
public static final String KEY_CALORIES = "calories";
public static final String KEY_PROTEIN = "protein";
public static final String KEY_CARBS = "carbs";
public static final String KEY_FATS = "fats";

private static final String DATABASE_NAME = "gofitness";
private static final String DATABASE_TABLE = "stats";
private static final int DATABASE_VERSION = 1;


private DbHelper ourHelper;
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 dbstats) {
        // TODO Auto-generated method stub
        dbstats.execSQL(
                "CREATE TABLE " + DATABASE_TABLE + "(" + 
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        KEY_NAME + " TEXT NOT NULL, " +
        KEY_AGE + " INT NOT NULL, " +
        KEY_CALORIES + " INT NOT NULL, " +
        KEY_WEIGHT + " INT NOT NULL, " +
        KEY_HEIGHT + " INT NOT NULL, " +
        KEY_PROTEIN + " INT NOT NULL, " +
        KEY_FATS + " INT NOT NULL, " +
        KEY_CARBS + " INT NOT NULL);"

                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase dbstats, int oldVersion, int n    
             newVersion) {
        // TODO Auto-generated method stub
        dbstats.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(dbstats);


    }

}
public AddStats(Context c)
{
    ourContext = c; 
}

public AddStats open(){
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;


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

public long createEntry(String usernamevar, String agevar,String weightvar,String        
    heightvar,String caloriesvar,
        String proteinvar, String carbsvar, String fatvar) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME,usernamevar);
    cv.put(KEY_AGE,agevar);
    cv.put(KEY_WEIGHT,weightvar);
    cv.put(KEY_HEIGHT,heightvar);
    cv.put(KEY_CALORIES,caloriesvar);
    cv.put(KEY_PROTEIN,proteinvar);
    cv.put(KEY_CARBS,carbsvar);
    cv.put(KEY_FATS,fatvar);

    return ourDatabase.insert(DATABASE_TABLE,null, cv);

}

public String getData() {
    // TODO Auto-generated method stub

    String[] columns = new String[]{KEY_ROWID, KEY_NAME,   
        KEY_AGE,KEY_WEIGHT,KEY_HEIGHT, KEY_CALORIES,KEY_PROTEIN,KEY_CARBS,KEY_FATS};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, 
         null, null);
    String result = "";
    int iRow = c.getColumnIndex(KEY_ROWID);
    int iUser = c.getColumnIndex(KEY_NAME);
    int iAge = c.getColumnIndex(KEY_AGE);
    int iWeight = c.getColumnIndex(KEY_WEIGHT);
    int iHeight = c.getColumnIndex(KEY_HEIGHT);
    int iCalories = c.getColumnIndex(KEY_CALORIES);
    int iProtein = c.getColumnIndex(KEY_PROTEIN);
    int iCarb = c.getColumnIndex(KEY_CARBS);
    int iFat = c.getColumnIndex(KEY_FATS);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){

    result = result + c.getString(iRow) + " " + c.getString(iUser) + " " +   
            c.getString(iAge) + c.getString(iWeight)
            + c.getString(iHeight) + c.getString(iCalories)+    
             c.getString(iProtein) + c.getString(iCarb) + c.getString(iFat) +"\n";
    }
    return result;
}

}

AddStatsForm.Java

       package com.gofitness.www;

      import android.app.Activity;



        import android.os.Bundle;
         import android.view.View;
        import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.EditText;
       import android.widget.TextView;


      public class AddStatsForm extends Activity implements OnClickListener {

EditText username, age, weight,height, calories, protein, carbs, fat;
TextView success;
Button submit;



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

     submit = (Button) findViewById(R.id.bEnter);
      username = (EditText) findViewById(R.id.etUsername);
     age = (EditText) findViewById(R.id.etAge);
     weight = (EditText) findViewById(R.id.etWeight);
     height = (EditText) findViewById(R.id.etHeight);
   calories = (EditText) findViewById(R.id.etCalories);
     protein = (EditText) findViewById(R.id.etProtein);
     carbs = (EditText) findViewById(R.id.etCarbs);
      fat = (EditText) findViewById(R.id.etFat);
      success = (TextView) findViewById(R.id.tvSuccess);





     submit.setOnClickListener(this);

}

 @Override
    public void onClick(View view) {


            String usernamevar = username.getText().toString();
            String agevar = age.getText().toString();
            String weightvar = weight.getText().toString();
            String heightvar = height.getText().toString();
            String caloriesvar = calories.getText().toString();
            String proteinvar = protein.getText().toString();
            String carbsvar = carbs.getText().toString();
            String fatvar = fat.getText().toString();

            AddStats entry = new AddStats(AddStatsForm.this);
            entry.open();
            entry.createEntry(usernamevar ,agevar, weightvar,   h        
                heightvar, caloriesvar, proteinvar, carbsvar, fatvar);
            entry.close();


        }}
4

1 回答 1

0

DATABASE_VERSION变量增加到至少2. 当您的应用程序更新并再次运行时,当它尝试打开数据库时,系统将识别出现有数据库的版本号低于您指定的版本号。它会打电话onUpgrade()给你旧版本和新版本。使用该信息对数据库进行适当的更改。

目前,您的onUpgrade方法只是删除现有表并onCreate再次调用,这将起作用,但不会保留任何现有数据。您可能会考虑这样的事情:

@Override
public void onUpgrade(SQLiteDatabase dbstats, int oldVersion, int newVersion) {
    if (oldVersion == 1) {
        db.execSql(/* create second table */);
    }
}
于 2013-04-28T00:54:23.770 回答