0

我有以下代码,但我在重复时遇到了一些麻烦。如何修改代码以使其不允许在数据库中出现重复?我正在使用它来为 AutoCompleteTextView 供电,因此重复项没有帮助。非常感谢 !

package com.giantflyingsaucer;

import android.database.*;
import android.database.sqlite.*;
import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

public class SQLiteCountryAssistant extends SQLiteOpenHelper
{
private static final String DB_NAME = "usingsqlite.db";
private static final int DB_VERSION_NUMBER = 1;
private static final String DB_TABLE_NAME = "countries";
private static final String DB_COLUMN_1_NAME = "country_name";

private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME +
                        " (_id integer primary key autoincrement, country_name text not null);)";

private SQLiteDatabase sqliteDBInstance = null;

public SQLiteCountryAssistant(Context context)
{
    super(context, DB_NAME, null, DB_VERSION_NUMBER);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    // TODO: Implement onUpgrade
}

@Override
public void onCreate(SQLiteDatabase sqliteDBInstance)
{
    Log.i("onCreate", "Creating the database...");
    sqliteDBInstance.execSQL(DB_CREATE_SCRIPT);
}

public void openDB() throws SQLException
{
    Log.i("openDB", "Checking sqliteDBInstance...");
    if(this.sqliteDBInstance == null)
    {
        Log.i("openDB", "Creating sqliteDBInstance...");
        this.sqliteDBInstance = this.getWritableDatabase();

    }
}

public void closeDB()
{
    if(this.sqliteDBInstance != null)
    {
        if(this.sqliteDBInstance.isOpen())
            this.sqliteDBInstance.close();
    }
}

public long insertCountry(String countryName)
{
    ContentValues contentValues = new ContentValues();
    contentValues.put(DB_COLUMN_1_NAME, countryName);
    Log.i(this.toString() + " - insertCountry", "Inserting: " + countryName);
    return this.sqliteDBInstance.insert(DB_TABLE_NAME, null, contentValues);
}

public boolean removeCountry(String countryName)
{
    int result = this.sqliteDBInstance.delete(DB_TABLE_NAME, "country_name='" + countryName + "'", null);

    if(result > 0)
        return true;
    else
        return false;
}

public long updateCountry(String oldCountryName, String newCountryName)
{
    ContentValues contentValues = new ContentValues();
    contentValues.put(DB_COLUMN_1_NAME, newCountryName);
    return this.sqliteDBInstance.update(DB_TABLE_NAME, contentValues, "country_name='" + oldCountryName + "'", null);
}

public String[] getAllCountries()
{
    Cursor cursor = this.sqliteDBInstance.query(DB_TABLE_NAME, new String[] {DB_COLUMN_1_NAME}, null, null, null, null, null);

    if(cursor.getCount() >0)
    {
        String[] str = new String[cursor.getCount()];
        int i = 0;

        while (cursor.moveToNext())
        {
             str[i] = cursor.getString(cursor.getColumnIndex(DB_COLUMN_1_NAME));
             i++;
         }
        return str;
    }
    else
    {
        return new String[] {};
    }
}
}
4

3 回答 3

1

一种选择是在实际执行插入之前removeCountry从您的方法中调用。insertCountry或者,您可以将数据库结构修改为country_name text not null unique. 如果您未能通过您需要适当地捕获和处理的约束,SQLite 将抛出异常。

最后,另一种选择是尝试在数据库中查询相关字符串,如果存在,则不要插入,如果不存在则插入。

于 2013-06-15T18:10:27.163 回答
1

您可以使用函数来查看数据是否已存在于数据库中。

   public boolean checkIfDataExists(String country) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.query(TABLE_NAME, columns, null, null, null, null, null);
    int iCountry = c.getColumnIndex(KEY_COUNTRY);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        if (country.equals(c.getString(iCountry))) {
            db.close();
            return true;
        }
    }
    db.close();
    return false;
}

if如果条件不成立,则在语句中调用此函数并执行插入指令。

于 2013-06-15T18:16:08.747 回答
0

我试过了,效果很好,至少到目前为止。

 private static final String DB_CREATE_SCRIPT = "create table " + DB_TABLE_NAME +
                        " (_id integer primary key autoincrement, country_name text UNIQUE);)";

添加了 UNIQUE 而不是not null

于 2013-06-15T18:30:38.527 回答