validate user name and password from existing data base when click login button android
dear i have two buttons in my main view "signup" and "login" when i click signup then a next view loaded with two text box field "user name" and "password" and a submitt button when i click on button i am sending data to data base and return to my main activity by using finish () in on buttonclick() event. now when i click on login button it opens next view user name and password when i fill it and click submitt button it force close ..i use validate() method in my content provider database...so i am not able to find the bug pls see my code and help me pls...thanks in adv.and one another question is that how to check wheather data is inserted in database or not ...
my content provider class is
public class MyContentProvider extends ContentProvider {
private SQLiteDatabase sqlDB;
public DatabaseHelper dbHelper;
private static final String DATABASE_NAME = "Users.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "User";
private static final String TAG = "MyContentProvider";
public static class DatabaseHelper extends SQLiteOpenHelper {
//public String USER_NAME;
//public String PASSWORDS;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// create table to store user names
db.execSQL("Create table "
+ TABLE_NAME
+ "( _id INTEGER PRIMARY KEY AUTOINCREMENT, USER_NAME TEXT,PASSWORDS TEXT);");
}
public boolean validateUser(String username, String password){
Cursor c = getReadableDatabase().rawQuery(
"SELECT * FROM " + TABLE_NAME + " WHERE "
+ MyUsers.User.USER_NAME + "='" + username +"'AND "+MyUsers.User.Pass+"='"+password+"'" , null);
if (c.getCount()>0)
return true;
return false;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
@Override
public int delete(Uri uri, String s, String[] as) {
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
// get database to insert records
sqlDB = dbHelper.getWritableDatabase();
// insert record in user table and get the row number of recently inserted record
long rowId = sqlDB.insert(TABLE_NAME, "",contentvalues);
if (rowId > 0) {
Uri rowUri = ContentUris.appendId(
MyUsers.User.CONTENT_URI.buildUpon(), rowId).build();
getContext().getContentResolver().notifyChange(rowUri, null);
return rowUri;
}
throw new SQLException("Failed to insert row into " + uri);
}
@Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext());
return (dbHelper == null) ? false : true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
SQLiteDatabase db = dbHelper.getReadableDatabase();
qb.setTables(TABLE_NAME);
Cursor c = qb.query(db, projection, selection, null, null, null,
sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues contentvalues, String s,
String[] as) {
return 0;
}
MYUser class is
public class MyUsers {
public static DatabaseHelper dbHelper;
static final String TABLE_NAME = "User";
public static final String AUTHORITY = "com.linkwithweb.providers.MyContentProvider";
// BaseColumn contains _id.
public static final class User implements BaseColumns {
public static final Uri CONTENT_URI = Uri
.parse("content://com.linkwithweb.providers.MyContentProvider");
// Table column
public static final String USER_NAME = "USER_NAME";
public static final String Pass = "Pass";
}
loginpage class is:
public class loginpage extends Activity
{
// DatabaseHelper dbHelper;
DatabaseHelper dbHelper=new DatabaseHelper(loginpage.this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText etext = (EditText) findViewById(R.id.editText);
String Name=( etext.getText().toString());
EditText etext2 = (EditText) findViewById(R.id.editText3);
String Password =( etext2.getText().toString());
Log.i("saurabh ..",Name);
if (dbHelper.validateUser(Name,Password))
{
Intent i = new Intent(loginpage.this,welcome.class);
startActivity(i);
}
//else
//Toast.makeText(getBaseContext(), "Wrong username/password", Toast.LENGTH_LONG).show();
}
});
}
signup page is:::
public class signform extends Activity
{
MyAdapter mAdapter=null;
DatabaseHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginform);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText etext = (EditText) findViewById(R.id.editText1);
String Names=( etext.getText().toString());
EditText etext2 = (EditText) findViewById(R.id.editText3);
String Password =( etext2.getText().toString());
**mAdapter.insertRecord(Names,Password)**;
finish();
}
});
mAdapter = new MyAdapter(this);
}
class MyAdapter extends BaseAdapter implements OnClickListener
{
private LayoutInflater inflater;
public MyAdapter(Context ctx) {
super();
this.inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void insertRecord(String unames,String Passw) {
ContentValues values = new ContentValues();
values.put(MyUsers.User.USER_NAME, unames);
values.put(MyUsers.User.Pass, Passw);
getConten开发者_Go百科tResolver().insert(MyUsers.User.CONTENT_URI, values);
notifyDataSetChanged();
Log.i("check for data insertion.......", "data inserted succesfully");
}
pls check why login is not performed on button click..thanks
pls see my log cat after modifying
07-01 12:17:32.911: ERROR/AndroidRuntime(600): Uncaught handler: thread main exiting due to uncaught exception
07-01 12:17:32.940: ERROR/AndroidRuntime(600): android.database.sqlite.SQLiteException: no such column: Pass: , while compiling: SELECT * FROM User WHERE USER_NAME='first names'AND Pass='last names'
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1220)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1193)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at com.linkwithweb.providers.MyContentProvider$DatabaseHelper.validateUser(MyContentProvider.java:45)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at loginapp.page.loginpage$1.onClick(loginpage.java:29)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.View.performClick(View.java:2364)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.View.onTouchEvent(View.java:4179)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.widget.TextView.onTouchEvent(TextView.java:6540)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.View.dispatchTouchEvent(View.java:3709)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.os.Looper.loop(Looper.java:123)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.app.ActivityThread.main(ActivityThread.java:4363)
In your loginpage Activity you called dbHelper.validateUser(Name,Password).But you didn't worked with the dbHelper.
write
dbHelper=new DatabaseHelper(this);
before calling dbHelper.validateUser();
When you Created Table you Defined column as PASSWORD.That is your column name is PASSWORD.But when you made query you were quering like
"SELECT * FROM " + TABLE_NAME + " WHERE "
+ MyUsers.User.USER_NAME + "='" + username +"'AND "+MyUsers.User.Pass+
The value of MyUsers.User.Pass is Pass.not PASSWORD.so you had the problem.Please correct the column name.Hopefully you are done
精彩评论