2016-09-09 85 views
0

MY活動代碼它顯示了這樣的錯誤:停留在此產生的原因:android.database.sqlite.SQLiteException:沒有這樣的柱:UNAME(碼1):在編譯:選擇UNAME,通過fromcontacts

Caused by: android.database.sqlite.SQLiteException: no such column: uname (code 1): , while compiling: select uname,pass fromcontacts

一旦打開,我的應用程序就會崩潰。

package eminence.bbpsgangarammarg; 

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

public class DatabaseHelper extends SQLiteOpenHelper{ 

private static final int DATABSE_VERSION =1; 
private static final String DATABSE_NAME = "contacts.db"; 
private static final String TABLE_NAME = "contacts"; 
private static final String COLUMN_ID= "ID"; 
private static final String COLUMN_NAME= "name"; 
private static final String COLUMN_EMAIL= "email"; 
private static final String COLUMN_UNAME= "uname"; 
private static final String COLUMN_PASSWORD= "pass"; 
SQLiteDatabase db; 

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

public DatabaseHelper(Context context) 
{ 
    super(context, DATABSE_NAME , null, DATABSE_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_UNAME , c.getUname()); 
    values.put(COLUMN_PASSWORD , c.getPass()); 

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


public String searchPass(String uname) { 
    db = this.getReadableDatabase(); 
    String query = "select uname,pass from" + TABLE_NAME; 

    Cursor cursor = db.rawQuery(query, null); 
    String a, b; 
    b = "not found"; 
    if (cursor.moveToFirst()) { 
     do { 
      a = cursor.getString(0); 
      b = cursor.getString(1); 

      if (a.equals(uname)) { 
       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); 
} 
} 
+0

如果您正在計數你的聯繫人,會不會更好'從聯繫人中選擇count(*)c,並閱讀單個記錄? – halfer

回答

1

你的第二行更改爲在searchPass()方法如下

String query = "select uname,pass from " + TABLE_NAME; 

因爲最後你的查詢被錯誤地變成如下這是不對的

"select uname,pass fromcontacts" 

"fromcontacts"不存在,它應該是"from contacts"

+0

@Lakshay Grover對你有幫助嗎? – Nikhil

相關問題