gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:20:51 27 4
gpt4 key购买 nike

这是我的 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;
}
}

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

最佳答案

只需先运行一条语句来检查该名称是否存在,例如:

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

关于java - 使用 Android 检查名称是否已存在于数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636768/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com