无法在数据库上创建表(Android)

时间:2012-08-18 08:01:12

标签: android database

我在使用Android创建表时遇到了问题。

我有桌子,但它是旧版本所以我不得不放弃它。在此之后,我无法创建另一个表,我无法弄清楚原因。我收到错误“没有这样的表user_login”

这是我的数据库类

public class Database  extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DB_name = "aeglea";
private static final String TABLE_NAME = "user_login";
//private static final String KEY_ID = "id";
private static final String KEY_UID = "uid";
private static final String KEY_NAME = "name";
private static final String KEY_LAST = "last";
private static final String KEY_EMAIL = "email";
private static final String KEY_USERNAME = "username";

public Database(Context context) {
    super(context, DB_name, null, DATABASE_VERSION);
}


public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + KEY_UID + " INTEGER PRIMARY KEY,"
            + KEY_NAME + " TEXT,"
            + KEY_LAST + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE,"
            + KEY_USERNAME + " TEXT UNIQUE" + ")";

    db.execSQL(CREATE_LOGIN_TABLE);


}


public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
     // Create tables again
    onCreate(db);

}

public void addUser(int uid, String name, String last, String email, String username) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_UID, uid); // user id
    values.put(KEY_NAME, name); // Name
    values.put(KEY_LAST, last); //last
    values.put(KEY_EMAIL, email); // Email
    values.put(KEY_USERNAME, username); // User name

    // Inserting Row
    db.insert(TABLE_NAME, null, values);
    db.close(); // Closing database connection
}

public void resetTables(){
    SQLiteDatabase db = this.getWritableDatabase();
    // Delete All Rows
    db.delete(TABLE_NAME, null, null);
    db.close();
}

/**
 * Getting user data from database
 * */
public HashMap<String, String> getUserDetails(){
    HashMap<String,String> user = new HashMap<String,String>();
    String query = "SELECT  * FROM " + TABLE_NAME;//make query

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);//do query
    // Move to first row
    cursor.moveToFirst();
    if(cursor.getCount() > 0){
        user.put("uid", cursor.getString(1));
        user.put("name", cursor.getString(2));
        user.put("last",cursor.getString(3));
        user.put("email", cursor.getString(4));
        user.put("username", cursor.getString(5));

    }
    cursor.close();
    db.close();
    // return user
    return user;
}


/**
 * Getting user login status
 * return true if rows are there in table
 * */
public int getRowCount() {
    String countQuery = "SELECT  * FROM " + TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();


    Cursor cursor = db.rawQuery(countQuery, null);
    int rowCount = cursor.getCount();
    db.close();
    cursor.close();

    // return row count
    return rowCount;
}

}

这是我使用数据库的主要类(查看底部)

public class login2 extends Activity {
private EditText un,pw;
TextView error;
Button ok;
private JSONObject jo2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    un=(EditText)findViewById(R.id.et_un);
    pw=(EditText)findViewById(R.id.et_pw);
    ok=(Button)findViewById(R.id.btn_login);
    error=(TextView)findViewById(R.id.tv_error);

    ok.setOnClickListener(new View.OnClickListener() {

    private Database db = new Database(getApplicationContext());    

        public void onClick(View v) {
            // TODO Auto-generated method stub

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            //String valid = "1";
            JSONObject response = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://ironis.dyndns.org/aeglea/android/log.php", postParameters);
               // String res=response.toString();
               // res = res.trim();
                //res= res.replaceAll("\\s+","");                            
                //error.setText(res);

               if(response.getString("success").equals("1")){
                    error.setText(response.getString("last"));
                    //store user to database in success

                    //reset older entries
                    db.resetTables();
                    //add user
                    db.addUser(Integer.parseInt(response.getString("id")), response.getString("name"), response.getString("last"), response.getString("email"), response.getString("username"));

               }else{
                    error.setText(response.getString("error_msg"));
                    } 
            } catch (Exception e) {
                un.setText(e.toString());
            }

        }
    });
}

}

1 个答案:

答案 0 :(得分:2)

您应该更改数据库版本,如:

private static final int DATABASE_VERSION = 2

ithink如果你改变版本,你不需要删除旧表。新表将取代它。

也许有效。