Reputation: 2088
DBAdapter.java
public void onCreate(SQLiteDatabase db) {
db.execSQL(" CREATE TABLE " + DATABASE_TABLE + " (" + KEY_FIRSTNAME
+ " TEXT NOT NULL, " + KEY_MIDDLENAME + " TEXT NOT NULL, "
+ KEY_LASTNAME + " TEXT NOT NULL, " + KEY_USERNAME
+ " TEXT NOT NULL, " + KEY_MAIL_ID + " TEXT NOT NULL, "
+ KEY_PASSWORD + " TEXT NOT NULL, " + KEY_CONFIRM
+ " TEXT NOT NULL, " + KEY_DATE_OF_BIRTH
+ " INTEGER NOT NULL " + KEY_COUNTRY + " TEXT NOT NULL "
+ ");");
}
public String getSinlgeEntry(String userName) {
Cursor cursor = mDB.query(DATABASE_TABLE,null,"KEY_USERNAME=?",new String[]{userName},null,null,null);
if (cursor.getCount() < 1) // UserName Not Exist
return "NOT EXIST";
cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("KEY_PASSWORD"));
return password;
}
MainActivity.java
dbadapter = new DBAdapter(MainActivity.this);
dbadapter.open();
String stored_password = dbadapter.getSinlgeEntry(username);
if(password.equals(stored_password))
{
Toast.makeText(MainActivity.this,"It is logging in",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,Profile_view.class);
startActivity(intent);
}
Logcat:
12-06 14:52:20.213: E/AndroidRuntime(9431): FATAL EXCEPTION: main
12-06 14:52:20.213: E/AndroidRuntime(9431): android.database.sqlite.SQLiteException: no such column: KEY_USERNAME: , while compiling: SELECT * FROM Saturday_Table_1129 WHERE KEY_USERNAME=?
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:147)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:368)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:131)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:95)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1534)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1414)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1370)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1450)
12-06 14:52:20.213: E/AndroidRuntime(9431): at com.example.signup.DBAdapter.getSinlgeEntry(DBAdapter.java:175)
12-06 14:52:20.213: E/AndroidRuntime(9431): at com.example.signup.MainActivity$1.onClick(MainActivity.java:55)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.view.View.performClick(View.java:3100)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.view.View$PerformClick.run(View.java:11644)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.os.Handler.handleCallback(Handler.java:587)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.os.Looper.loop(Looper.java:126)
12-06 14:52:20.213: E/AndroidRuntime(9431): at android.app.ActivityThread.main(ActivityThread.java:3997)
12-06 14:52:20.213: E/AndroidRuntime(9431): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 14:52:20.213: E/AndroidRuntime(9431): at java.lang.reflect.Method.invoke(Method.java:491)
12-06 14:52:20.213: E/AndroidRuntime(9431): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-06 14:52:20.213: E/AndroidRuntime(9431): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-06 14:52:20.213: E/AndroidRuntime(9431): at dalvik.system.NativeStart.main(Native Method)
12-06 14:52:28.734: I/Process(9431): Sending signal. PID: 9431 SIG: 9
I am validating the username and password from the database. When i compile,it says no such column but i have defined the KEY_USERNAME column. The database has the username that I am trying to use for validation but it doesn't work. Help will be much appreciated.
Upvotes: 1
Views: 306
Reputation: 33505
Change yours with this:
mDB.query(DATABASE_TABLE, null, KEY_USERNAME + "= ?", new String[] {userName});
You have declared column name in variable KEY_USERNAME
but you added it as String literal (and it was translated as text KEY_USERNAME) and not as variable that contains another text. This was reason of Exception.
Update:
Same mistake you did here:
cursor.getString(cursor.getColumnIndex("KEY_PASSWORD"));
Here you again added constant as String literal, you need to change it in same way as above:
cursor.getColumnIndex(KEY_PASSWORD)
Now it should works properly.
Upvotes: 4
Reputation: 581
Your Query is wrong You have wrote "KEY_USERNAME=?" this is wrong
public String getSinlgeEntry(String userName) {
Cursor cursor = mDB.query(DATABASE_TABLE,null,KEY_USERNAME + " + "= ?",new String[]{userName},null,null,null);
if (cursor.getCount() < 1) // UserName Not Exist
return "NOT EXIST";
cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("KEY_PASSWORD"));
return
}
check this will work
Upvotes: 0
Reputation: 1748
Its simple you just need to use the parameter you create not added in quotation like this:
Cursor cursor = mDB.query(DATABASE_TABLE,null,KEY_USERNAME+" =?",new String[]{userName},null,null,null);
Upvotes: 0
Reputation: 17922
You are using the constant KEY_USERNAME
to create your table, but the String "KEY_USERNAME=?"
in your sql statement later on.
You'll probably want to use the constant in your sql statement like this: KEY_USERNAME+"=?"
Upvotes: 3