数据库:如何知道数据库中是否已存在该记录?

时间:2015-02-02 14:45:02

标签: android database sqlite


我知道这个问题已被提出。但是我无法从所有存在的例子中理解。
我创建app,在我的应用程序中有一个注册表。
当我点击提交按钮然后我想要检查该记录是否已存在于数据库中。如果没有,则插入并且如果存在则生成错误消息。

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

    }

}

0 个答案:

没有答案