2017-03-11 11 views
-1

私は知らない私は、全体のことをリメイクしようとするが、それはまだ問題 を持っている誰もが、私はそれを解決しようとすることができますが、それはこれを示して SQLiteException:いいえ、そのようなコラム: SQLiteException::いいえ、そのような列:ユーザー名、ユーザー名(アンドロイドスタジオ)

public class DbHelper extends SQLiteOpenHelper { 

    public static final String TAG = DbHelper.class.getSimpleName(); 
    public static final String DB_NAME = "myapp.db"; 
    public static final int DB_VERSION = 1; 

    public static final String USER_TABLE = "users"; 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_USERNAME = "username"; 
    public static final String COLUMN_PASS = "password"; 

    /* 
    create table users(
     id integer primary key autoincrement, 
     email text, 
     password text); 
    */ 
    public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "(" 
      + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + COLUMN_USERNAME + " TEXT, " 
      + COLUMN_PASS + " TEXT);"; 

    public DbHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_TABLE_USERS); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE); 
     onCreate(db); 
    } 

    /** 
    * Storing user details in database 
    * */ 
    public void addUser(String username, String password) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(COLUMN_USERNAME, username); 
     values.put(COLUMN_PASS, password); 

     long id = db.insert(USER_TABLE, null, values); 
     db.close(); 

     Log.d(TAG, "user inserted" + id); 
    } 

    public boolean getUser(String username, String pass){ 
     //HashMap<String, String> user = new HashMap<String, String>(); 
     String selectQuery = "select * from " + USER_TABLE + " where " + 
       COLUMN_USERNAME + " = " + "'"+username+"'" + " and " + COLUMN_PASS + " = " + "'"+pass+"'"; 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     // Move to first row 
     cursor.moveToFirst(); 
     if (cursor.getCount() > 0) { 

      return true; 
     } 
     cursor.close(); 
     db.close(); 

     return false; 
    } 
} 

エラーログ:

FATAL EXCEPTION: main 
    Process: pixelfotress.musicplayer, PID: 2538 
    android.database.sqlite.SQLiteException: no such column: username (code 1): , while compiling: select * from users where username =? and password = ? 
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318) 
    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257) 
    at pixelfotress.musicplayer.DbHelper.getUser(DbHelper.java:68) 
    at pixelfotress.musicplayer.LoginActivity.login(LoginActivity.java:54) 
    at pixelfotress.musicplayer.LoginActivity.onClick(LoginActivity.java:40) 
    at android.view.View.performClick(View.java:5610) 
    at android.view.View$PerformClick.run(View.java:22265) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
+0

テーブルを作成して削除するJDBCコードが表示されることは決してありません。私が扱っているJavaアプリは、アプリの起動時にすぐに利用できます。ユーザーはスキーマを操作しません。私はこのデザインに対して勧めたいと思う。 – duffymo

答えて

1

変更この

String selectQuery = "select * from " + USER_TABLE + " where " + 
     COLUMN_USERNAME + " = " + "'"+username+"'" + " and " + COLUMN_PASS + " = " + "'"+pass+"'"; 

to this

Cursor c = db.rawQuery("select * from " + USER_TABLE + " where "+COLUMN_USERNAME + " =? "+ " and " + COLUMN_PASS + " = ?" , new String[]{username,pass}); 
+1

なぜそれが問題を解決しますか? – JonZarate

+0

aditya desai私はあなたのコードを試しましたが、登録した後にアプリケーションを強制的に閉じようとしました –

+0

エラーログを投稿することができます –

関連する問題