0

编辑:可能值得补充的是,我在单独的 java 类中创建了 4/5 个表,这是正确的做事方式还是应该在 1 个类中创建所有表?

好的,我有多个表正在运行并在我的应用程序上工作。如果我想添加一个到目前为止一直在工作的新表,我被告知要从应用程序中擦除数据。

还添加了 if 语句检查也有效的 oldversion==1 方法。

我试图添加一个新表,突然我的一半表不起作用。有些会,有些不会,而且非常不一致。

这段代码是我用来在擦除之前成功运行我的表的代码,但是它不会创建表,因为它找不到名为“Achievements”的表

编辑 2:我已将所有表格归为 1 类,但仍然出现相同的错误:

MyDb.java

包 com.gofitness.www;

      import java.sql.SQLException;

        import android.content.ContentValues;
          import android.database.Cursor;
         import android.database.sqlite.SQLiteDatabase;
         import android.database.sqlite.SQLiteOpenHelper;
         import android.util.Log;

          public class MyDb {
public static final String KEY_USERID = "userid";
     public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";
    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";
public static final String KEY_ACHID = "AchievementID";
public static final String KEY_COMPLETED = "Completed";
public static final String KEY_ACH = "Achievement";
public static final String TAG = "MyDb";



private static final String DATABASE_NAME = "GoFitness";    
private static final int DATABASE_VERSION = 1;

private static final String Create_Users =
    "create table users (userid integer primary key autoincrement, "
    + "username text not null, password text not null);";


     private static final String Create_Stats =
      "create table stats (userid integer primary key autoincrement, "
     + KEY_USERNAME + " 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);";



    private static final String Create_Achievements =
    "create table achievements (AchievementID integer primary key autoincrement, "
    + "Achievement text not null);";

     private static final String Create_UserAchievements =
     "create table userachievements (userid integer, "
     + "AchievementID integer not null, Completed text not null);";




     private Context context;
     private DatabaseHelper DBHelper;
      private SQLiteDatabase db;

     public MyDb(Context ctx) 
    {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
    DatabaseHelper(Context context) 
    {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
     }

     @Override
     public void onCreate(SQLiteDatabase db) 
{
    db.execSQL(Create_Users);
    db.execSQL(Create_Stats);
    db.execSQL(Create_Achievements);
    db.execSQL(Create_UserAchievements);
      }


 @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                       int newVersion) 
 {
     Log.w(TAG, "Upgrading database from version " + oldVersion 
           + " to "
           + newVersion + ", which will destroy all old data");
     db.execSQL("DROP TABLE IF EXISTS users,stats,achievements,userachievements");
     onCreate(db);
       }
      }


     public MyDb open() throws SQLException 
    {
    db = DBHelper.getWritableDatabase();
     return this;
        } 

       //---closes the database---    
      public void close() 
       {
       DBHelper.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_USERNAME,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 db.insert("stats", null, cv);
          }

    public long createUA(String check, String checkpass, String completedDB) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_ACHID,check);
        cv.put(KEY_USERID,checkpass);
        cv.put(KEY_COMPLETED,completedDB);

        return db.insert("userachievements",null, cv);

    }


    public long createAch(String check) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_ACH,check);

        return db.insert("achievements",null, cv);

    }




    public long createUser(String check, String checkpass) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_USERNAME,check);
        cv.put(KEY_PASSWORD,checkpass);
        return db.insert("users",null, cv);

    }



    public String getName(String s) {
        // TODO Auto-generated method stub
        Cursor c = db.rawQuery("SELECT * FROM " +
                "users" +
                " where username = "+ "'"+ s+"'" , null);

        if (c != null ) {
            c.moveToFirst();

                 int iUsername = c.getColumnIndex(KEY_USERNAME);
                    String Name = c.getString(iUsername);


                    return Name;

            }
        return null;
    }


    public String getPass(String s) {
        // TODO Auto-generated method stub
        Cursor c = db.rawQuery("SELECT * FROM " +
                "users" +
                " where username = "+ "'"+ s+"'" , null);

        if (c != null ) {
            c.moveToFirst();

                 int iPassword = c.getColumnIndex(KEY_PASSWORD);
                    String Pass = c.getString(iPassword);


                    return Pass;



            }
        {
            return null;

        }
    }

    public String getId(String s) {
        // TODO Auto-generated method stub
        Cursor c = db.rawQuery("SELECT * FROM " +
                "users" +
                " where username = "+ "'"+ s+"'" , null);

        if (c != null ) {
            c.moveToFirst();

                 int iPassword = c.getColumnIndex(KEY_USERID);
                    String ID = c.getString(iPassword);


                    return ID;



            }
        {
            return null;

        }
    }



          }

成就.java

    package com.gofitness.www;

       import android.app.Activity;
      import android.content.Intent;
         import android.os.Bundle;
      import android.view.View;
        import android.widget.Button;
       import android.widget.EditText;
       import android.widget.TextView;

      public class Achievements extends Activity  {


Button submit;
TextView invalid;




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

 Button submit = (Button) findViewById(R.id.submit);
 final EditText username = (EditText) findViewById(R.id.etUsername);
 final EditText password = (EditText) findViewById(R.id.etPassword);
 final TextView invalid = (TextView) findViewById(R.id.invalid);
 submit.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
         boolean diditwork = true;
        try {
        String check = username.getText().toString();
        String checkpass = password.getText().toString();

    AddAchievements entry = new AddAchievements(Achievements.this);
    entry.open();
    entry.createEntry(check, checkpass);
    entry.close();
    }catch (Exception e){
            diditwork=false;
            }finally{
        if(diditwork){
        invalid.setText("WERKZ");
        }
            }


        }

AddAchievements.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 AddAchievements {

public static final String KEY_ROWID = "AchievementID";
public static final String KEY_NAME = "Achievement";


private static final String DATABASE_NAME = "gofitness";
private static final String DATABASE_TABLE = "Achievements";
private static final int DATABASE_VERSION = 2;


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 db) {
        // TODO Auto-generated method stub
        db.execSQL(
                "CREATE TABLE " + DATABASE_TABLE + "(" + 
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        KEY_NAME + " TEXT NOT NULL);"

                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if (oldVersion==1){


                db.execSQL("DROP TABLE IF EXISTS " +   
                DATABASE_TABLE);
                onCreate(db);
                }}

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

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


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

public long createEntry(String check, String checkpass) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME,check);

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

}

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

    String[] columns = new String[]{KEY_ROWID, KEY_NAME};
    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);


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

    result = result + c.getString(iRow) + " " + c.getString(iUser) +"\n";
    }
    return result;
}

public String getName(String s) {
    // TODO Auto-generated method stub
    Cursor c = ourDatabase.rawQuery("SELECT * FROM " +
            DATABASE_TABLE +
            " where username = "+ "'"+ s+"'" , null);

    if (c != null ) {
        c.moveToFirst();

             int iUsername = c.getColumnIndex(KEY_NAME);
                String Name = c.getString(iUsername);


                return Name;

        }
    return null;
}


    }

如果有人能指出我为什么这些不起作用,我将非常感激。谢谢

  });


}

}

4

0 回答 0