2017-02-21 72 views
0

我不知道哪裏出了問題,但是當我點擊登錄時,這些值將不會顯示。這是我的profile.java無法在我的SQLite數據庫中顯示我的數據[Android]

TextView vName = (TextView) findViewById(R.id.tvName); 
    TextView vEmail = (TextView) findViewById(R.id.tvEmail); 
    TextView vMobile = (TextView) findViewById(R.id.tvMobile); 

    Contact contact = new Contact(); 
    vName.setText(contact.getName().toString()); 
    vEmail.setText(contact.getEmail().toString()); 
    vMobile.setText(contact.getMobile().toString()); 

    Button btnOut = (Button) findViewById(R.id.btnOut); 
    btnOut.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent Outent = new Intent(Profile.this,MainActivity.class); 
      startActivity(Outent); 
      finish(); 
     } 
    }); 

這裏是我的數據庫代碼,我試圖使我DatabaseHelper.java的方法但它仍然沒有奏效。我想要像你們這樣的專業人士提供一些幫助。 DatabaseHelper.java

private static final String TABLE_CREATE = "create table contacts(id integer primary key not null ,"+ 
     "name text not null, email text not null, pass text not null, mobile text not null);"; 

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


@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(TABLE_CREATE); 
    this.db =db; 
} 

public void insertContact(Contact c){ 
    db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 

    String query = "select * from contacts"; 
    Cursor cursor = db.rawQuery(query,null); 
    int count = cursor.getCount(); 


    values.put(COLUMN_ID,count); 
    values.put(COLUMN_NAME,c.getName()); 
    values.put(COLUMN_EMAIL,c.getEmail()); 
    values.put(COLUMN_PASS, c.getPass()); 
    values.put(COLUMN_MOBILE, c.getMobile()); 

    db.insert(TABLE_NAME,null,values); 
    db.close(); 
} 

public Cursor getAllData(){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select email,name,mobile from"+TABLE_NAME,null); 
    return res; 
} 

public String searchPass(String email){ 
    db = this.getReadableDatabase(); 
    String query = "select email, pass from " + TABLE_NAME; 
    Cursor cursor = db.rawQuery(query,null); 
    String a, b; 
    b = "not found"; 
    if(cursor.moveToFirst()){ 
     do{ 
      a = cursor.getString(0); 

      if(a.equals(email)){ 
       b = cursor.getString(1); 
       break; 
      } 
     }while(cursor.moveToNext()); 
    } 
    return b; 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    String query = "DROP TABLE IF EXISTS" + TABLE_NAME; 
    db.execSQL(query); 
    this.onCreate(db); 
} 
+3

哪裏是數據庫的部分? – Fusselchen

回答

0

你是不是叫你的個人資料活動getAllData()

ProfileActivity

List<Contact> contacts = db.getAllData();   
     for (Contact cn : contacts) { 
      String name = cn.getName(); 
      vName.setText(name); 
      // follow by getEmail and getMobile 
     } 

DatabaseHelper

// Getting All Data 
    public List<Contact> getAllData() { 
     List<Contact> contactList = new ArrayList<Contact>(); 
     // Select All Query 
     String selectQuery = "SELECT email,name,mobile from"+TABLE_NAME; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       Contact contact = new Contact(); 
       contact.setName((cursor.getString(0)); 
       contact.setEmail(cursor.getString(1)); 
       contact.setMobile(cursor.getString(2)); 
       contactList.add(contact); 
      } while (cursor.moveToNext()); 
     } 

     return contactList; 
    } 

希望,它的準確性和有用。

+0

我試了一下代碼,然後當我點擊登錄按鈕時彈出這個錯誤。 「android.database.sqlite.SQLiteException:沒有這樣的列:電子郵件(代碼1):,而編譯:選擇電子郵件,名稱,手機從聯繫人」 – Phantom

+0

檢查您的數據庫,請確保您的表有一列名爲電子郵件 –

+0

我檢查我的數據庫我的表有一個名爲email的列。 – Phantom

相關問題