2016-09-22 47 views
0

數據庫已經存在這是我DBhelper.java碼...幫我檢查分貝,而存儲數據檢查名稱中使用的是Android

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

如何檢查名稱是否已經存在於數據庫從文本字段插入值?

+0

想要插入數據如果它不存在,如果存在無視插入,是你想要實現的嗎? – Enzokie

+0

您應該嘗試插入並處理在違反密鑰完整性約束條件時導致的失敗。分兩步做是浪費和容易出錯的。 – EJP

回答

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

你能解釋一下嗎? – Sebastian

+0

最新的虛假狀況..?如何顯示它。 – Sebastian

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"); 

這將爲 「ABC」 搜索在聯繫人中。

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

沒有成功,哪個答案? –