lundi 2 février 2015

Database : How to know that record is already exist in dabase or not?



I know that this question is already asked. But I can't understand from all exist examples.

I m create app,

In my App there is a registration form.

when I click on submit button then first I want to check that record is already in database or not. if not then Insert and If exist generate Error message.


SignUpActivity.java



signUp.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
display_name = displayName.getText().toString();
email_id = email.getText().toString();
pass = password.getText().toString();
c_pass = confirmPassword.getText().toString();

if (display_name.trim().isEmpty()) {

displayName.setError("Please enter valid display name.");
} else if (email_id.trim().isEmpty() || !isValidEmail(email_id)) {
email.setError("Please enter valid Email address.");
} else if (pass.trim().isEmpty() || !isValidPassword(pass)) {
password.setError("Please enter valid password.");
} else if (c_pass.trim().isEmpty() || !c_pass.equals(pass)) {
confirmPassword
.setError("Please enter same password again.");
} else {

dbHelper.insertDataTable(display_name, email_id, pass);
Toast.makeText(getApplicationContext(),
"Successfully Done", Toast.LENGTH_LONG).show();
dumpDataBase();
}

}
});


DatabseHelper.java



package com.example.vizsafe;

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 String DATABASE_NAME = "QDB.db";
private static final int DATABASE_VERSION = 1;

Cursor cur;
String getRecord;
SQLiteDatabase db;
// Table Names
private static final String USER_TABLE = "user_data";

// Table Create Statements
// USER_TABLE table create statement
private static final String CREATE_TABLE_USER = "CREATE TABLE "
+ USER_TABLE
+ " (display_name text,email_id text PRIMARY KEY,password text)";

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

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_USER);
// TODO Auto-generated method stub

}

public void insertDataTable(String display_name, String email_id,
String pass) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
cur = db.query(USER_TABLE, null, null, null, null, null, null);
cur.moveToFirst();

values.put("display_name", display_name);
values.put("email_id", email_id);
values.put("password", pass);

db.insert(USER_TABLE, null, values);
db.close();

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

Aucun commentaire:

Enregistrer un commentaire