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

标签: sqlite Java Android
发布时间: 2016/10/10 22:42:08
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

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

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;
    }
}

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

解决方法 1:

只是你运行的语句首先以检查名称是否存在像︰

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;
}
赞助商