使用Android检查数据库中是否已存在名称

时间:2016-09-22 10:37:39

标签: java android sqlite

这是我的DBhelper.java代码...帮我在存储数据时检查数据库

package com.example.sebastian.dblist;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

public class DBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "MyDBName.db";
    public static final String CONTACTS_COLUMN_NAME = "name";
    public static final String CONTACTS_COLUMN_EMAIL = "email";


    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(
                "create table contacts " +
                        "(id integer primary key, name text,email text)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }

    public boolean insertContact(String name, String email) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);

        contentValues.put("email", email);
        db.insert("contacts", null, contentValues);
        return true;
    }

    public Cursor getData(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        return db.rawQuery("select * from contacts where id=" + id + "", null);
    }

    public boolean updateContact(Integer id, String name, String email) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("email", email);

        db.update("contacts", contentValues, "id = ? ", new String[]{Integer.toString(id)});
        return true;
    }

    public Integer deleteContact(Integer id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("contacts",
                "id = ? ",
                new String[]{Integer.toString(id)});
    }

    public ArrayList<String> getAllCotacts()
    {
        ArrayList<String> array_list = new ArrayList<>();


        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from contacts", null );
        res.moveToFirst();

        while(!res.isAfterLast()){
            array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)) +"\n"+res.getString(res.getColumnIndex(CONTACTS_COLUMN_EMAIL)));
            res.moveToNext();
        }
        return array_list;
    }
}

如何在从文本字段插入值时检查数据库中是否已存在名称?

2 个答案:

答案 0 :(得分:2)

首先运行一个语句来检查名称是否存在,如:

SELECT * FROM CONTACTS WHERE NAME = ?

如果结果为空,您可以添加联系人并返回true。 如果结果是> 0你返回false。

编辑: 您可以扩展insertContact - 方法,例如:

public boolean insertContact(String name, String email) {
SQLiteDatabase db = this.getWritableDatabase();

// Check if name exists
Cursor res =  db.rawQuery( "SELECT * FROM CONTACTS WHERE NAME = ? ", new String[]{ name } );

// If name doesn't exist -> add
if (res.getCount() == 0) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);

        contentValues.put("email", email);
        db.insert("contacts", null, contentValues);
        return true;
    }

    // else -> return false and print a Toast e.g.
    return false;
}

答案 1 :(得分:0)

您可以尝试以下代码

SQLiteDatabase db = this.getReadableDatabase();
Cursor cur=  db.rawQuery( "select name from contacts where NAME  = ?", new String[]{ "abc" } );

Cursor cur=  db.rawQuery( "select name from contacts where NAME LIKE " + "abc");

这将搜索&#34; abc&#34;在联系人。

if(cur.getCount() > 0){
    //name is present
}