2012-04-23 97 views
0

當我執行下面的代碼時,即使我用預定義的字符串替換usertry,它也總是會給我「無效」。在我的查詢中是否有任何語法問題?Sqlite查詢語法?

支票登錄方法是另一類檢查有效登錄

public void checklogin(String usertry){ 


    DatabaseHelper dbh = new DatabaseHelper(LoginActivity.this); 
    DatabaseAdapter dba = new DatabaseAdapter(LoginActivity.this); 
    db= dbh.getReadableDatabase(); 
    TextView usernametry = (TextView) findViewById(R.id.usernametry); 
    usertry = usernametry.getText().toString(); 

    Cursor mCursor = db.query(dba.TABLE_USERS,new String[]{dba.COLUMN_ID,dba.COLUMN_NAME}, dba.COLUMN_NAME + "=" + "?", new String[]{usertry},null,null,null,null); 

    if (mCursor.moveToFirst()){ 
     Toast.makeText(LoginActivity.this, "Yaaaay", Toast.LENGTH_LONG).show(); 
    } 

     Toast.makeText(LoginActivity.this, "invalid", Toast.LENGTH_LONG).show();     
     } 
} 

這裏是我的數據庫適配器類:

public class DatabaseAdapter { 

public static final String COLUMN_ID = "ID"; 
public static final String COLUMN_NAME = "Name"; 
public static final String COLUMN_AGE = "Age"; 
public static final String COLUMN_GENDER = "Gender"; 
public static final String COLUMN_WEIGHT = "Weight"; 
public static final String COLUMN_HEIGHT = "Height"; 
public static final String COLUMN_ACTIVITY = "Activitylevel"; 
public static final String COLUMN_CALORIES = "numCal"; 
public static final String FOOD_ID = "food_ID"; 
public static final String FOOD_NAME = "food_name"; 
public static final String FOOD_CALS = "food_cals"; 
public static final String FOOD_FAT = "food_fat"; 
public static final String FOOD_PRO = "food_protein"; 
public static final String FOOD_CRAB = "food_crabs"; 

public static final String TABLE_USERS = "users"; 
public static final String TABLE_FOOD = "food"; 
private static final int DATABASE_VERSION = 1; 
private static final String DATABASE_NAME = "nutriwellness.db"; 

private static final String CREATE_TABLE_USERS = "create table users(id integer primary key autoincrement, " 
     + "Name text, Age integer, Gender text, Weight text, Height text, Activitylevel text, numCal float);"; 

private static final String CREATE_TABLE_FOOD = "create table food(food_ID integer primary key autoincrement," 
     + "food_name text,food_cals integer, food_fat integer, food_protein integer, food_crabs integer);"; 

private SQLiteDatabase database; 
private DatabaseHelper dbHelper; 

private String[] allColumnsofuserstable = { COLUMN_ID, COLUMN_NAME, 
     COLUMN_AGE, COLUMN_GENDER, COLUMN_WEIGHT, COLUMN_HEIGHT }; 

private String[] allColumnoffoodtable = { FOOD_ID, FOOD_NAME, FOOD_FAT, 
     FOOD_PRO, FOOD_CRAB }; 


public DatabaseAdapter(Context context) { 

    dbHelper = new DatabaseHelper(context); 
} 

public static class DatabaseHelper extends SQLiteOpenHelper { 

    public DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, 5); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     try { 
      db.execSQL(CREATE_TABLE_USERS); 
      db.execSQL(CREATE_TABLE_FOOD); 
     } catch (SQLException e) { 

      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(DatabaseHelper.class.getName(), 
       "Upgrading database from version " + oldVersion 
         + "to version " + newVersion); 

     db.execSQL("DROP TABLE IF EXISTS users"); 
     onCreate(db); 

    } 

} 

public DatabaseAdapter open() throws SQLException { 
    database = dbHelper.getWritableDatabase(); 
    return this; 
} 

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

// create a user 
public long createUser(String name, String age, String gender, 
     String weight, String height, String level, float calnum) { 

    ContentValues values = new ContentValues(); 
    values.put(COLUMN_NAME, name); 
    values.put(COLUMN_AGE, age); 

    values.put(COLUMN_GENDER, gender); 

    values.put(COLUMN_WEIGHT, weight); 
    values.put(COLUMN_HEIGHT, height); 
    values.put(COLUMN_ACTIVITY, level); 
    values.put(COLUMN_CALORIES, calnum); 

    return database.insert(TABLE_USERS, COLUMN_ID, values); 
} 

// delete a user by ID 
public boolean deleteUser(long rowId) { 
    return database.delete(TABLE_USERS, COLUMN_ID + "=" + rowId, null) > 0; 
} 

// retrieve a list of all users 
public Cursor getAllUsers() { 
    return database.query(TABLE_USERS, allColumnsofuserstable, null, null, 
      null, null, null); 

} 

// retrieve a particular user 
public Cursor getUser(long rowId) throws SQLException { 
    Cursor mCursor = database.query(true, TABLE_USERS, 
      allColumnsofuserstable, COLUMN_ID + "=" + rowId, null, null, 
      null, null, null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 

} 

// update a user 
public boolean updateUser(long rowId, String Name, int age, String gender, 
     String weight, String height, String level, float calnum) { 
    ContentValues values = new ContentValues(); 
    values.put(COLUMN_NAME, Name); 
    values.put(COLUMN_AGE, age); 

    values.put(COLUMN_GENDER, gender); 

    values.put(COLUMN_WEIGHT, weight); 
    values.put(COLUMN_HEIGHT, height); 
    values.put(COLUMN_ACTIVITY, level); 
    values.put(COLUMN_CALORIES, calnum); 
    return database.update(TABLE_USERS, values, COLUMN_ID + "=" + rowId, 
      null) > 0; 
} 


// FOOD METHODS ! 

//Create a new food row 
public long createFood(String name, int food_cal, int food_fat, int food_proteins, int food_carbs){ 
    ContentValues Fvalues = new ContentValues(); 
    Fvalues.put(FOOD_NAME, name); 
    Fvalues.put(FOOD_CALS, food_cal); 
Fvalues.put(FOOD_FAT, food_fat); 
    Fvalues.put(FOOD_PRO, food_proteins); 
    Fvalues.put(FOOD_CRAB, food_carbs); 
    return database.insert(TABLE_FOOD, FOOD_ID, Fvalues);  
} 


// delete a Food by ID 
public boolean deleteFood(long rowId) { 
    return database.delete(TABLE_FOOD, FOOD_ID + "=" + rowId, null) > 0; 
} 
// retrieve a list of all FOOD 
public Cursor getAllfood() { 
    return database.query(TABLE_FOOD, allColumnoffoodtable, null, null, 
      null, null, null); 

} 

// Retrieve a specific Food 


public Cursor getFood(long rowId) throws SQLException { 
    Cursor mCursor = database.query(true, TABLE_FOOD, 
      allColumnoffoodtable, FOOD_ID + "=" + rowId, null, null, 
      null, null, null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 

} 

// Update a food 

public boolean updateFood(long rowId,String name, int food_cal, int food_fat, int food_proteins, int food_carbs){ 
    ContentValues Fvalues = new ContentValues(); 
    Fvalues.put(FOOD_NAME, name); 
    Fvalues.put(FOOD_CALS, food_cal); 
Fvalues.put(FOOD_FAT, food_fat); 
    Fvalues.put(FOOD_PRO, food_proteins); 
    Fvalues.put(FOOD_CRAB, food_carbs); 
    return database.update(TABLE_FOOD, Fvalues, FOOD_ID+"="+rowId, null)>0; 
} 
} 

回答

0

顯然,這麪包的消息總是會被證明,即使你查詢工作。你應該嘗試這樣的:

if (mCursor.moveToFirst()){ 
     Toast.makeText(LoginActivity.this, "Yaaaay", Toast.LENGTH_LONG).show(); 
    }else{ 
     Toast.makeText(LoginActivity.this, "invalid", Toast.LENGTH_LONG).show(); 
    } 

但我想這不是真正的問題,但我沒有看到任何其他問題。也許我們需要看到你的DatabaseAdapter類。

+0

我已經插入了DBadapter。我一直在尋找這個錯誤,整個一天.. – callback 2012-04-23 05:15:09

+0

你檢查你的_database_是否越來越加載** DDMS ** – RMH 2012-04-23 07:29:35

+0

數據庫工作正常。我已經保存並用Sqlite Browser閱讀。 – callback 2012-04-23 13:16:46