SQLiteOpenHelper - 索引 locations_index 已经 -Android?

SQLiteOpenHelper - index locations_index already -Android?

我有下面的代码,我正在用这个 class 创建一个数据库,但是当我 运行 应用程序让我崩溃时:

android.database.sqlite.SQLiteException: index locations_index already exists (code 1): , while compiling: CREATE INDEX locations_index ON ReportAct_tbl (Count, Date, Time, Lat, Lng, UserCode, LatLng)

我的 class :

public class Query_A extends SQLiteOpenHelper {

    // database version
    private static final int DATABASE_VERSION = 7;

    // database name
    protected static final String DATABASE_NAME = "MarketingDB";

    // table details
    public String tableName = "Marketing_Users";
    public String fieldID = "ID";
    public String fieldName = "Name";
    public String fieldFamily = "Family";
    public String fieldUserID = "UserID";


    // table details
    public String tableName2 = "ReportAct_tbl";
    public String fieldRepID = "RepID";
    public String fieldCount = "Count";
    public String fieldDate = "Date";
    public String fieldTime = "Time";
    public String fieldLat = "Lat";
    public String fieldLng = "Lng";
    public String fieldUserCode = "UserCode";
    public String fieldLatLng = "LatLng";

    public Query_A(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql1 = "";

        sql1 += "CREATE TABLE " + tableName;
        sql1 += " ( ";
        sql1 += fieldID + " INTEGER PRIMARY KEY AUTOINCREMENT, ";
        sql1 += fieldName + " TEXT, ";
        sql1 += fieldFamily + " TEXT, ";
        sql1 += fieldUserID + " INTEGER ";
        sql1 += " ) ";

        db.execSQL(sql1);


        String sql2 = "";


        sql2 += "CREATE TABLE " + tableName2;
        sql2 += " ( ";
        sql2 += fieldRepID + " INTEGER PRIMARY KEY AUTOINCREMENT, ";
        sql2 += fieldCount + " TEXT, ";
        sql2 += fieldDate + " TEXT, ";
        sql2 += fieldTime + " TEXT, ";
        sql2 += fieldLat + " TEXT, ";
        sql2 += fieldLng + " TEXT, ";
        sql2 += fieldUserCode + " TEXT, ";
        sql2 += fieldLatLng + " TEXT ";
        sql2 += " ) ";

        db.execSQL(sql2);

        String INDEX = "CREATE UNIQUE INDEX locations_index ON "
                + tableName + " (Name, Family, UserID)";


        String INDEX2 = "CREATE INDEX locations_index ON "
                + tableName2 + " (Count, Date, Time, Lat, Lng, UserCode, LatLng)";
        db.execSQL(INDEX);
        db.execSQL(INDEX2);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + tableName;
        db.execSQL(sql);

        String sql2 = "DROP TABLE IF EXISTS " + tableName2;
        db.execSQL(sql2);

        onCreate(db);
    }

    // insert data using transaction and prepared statement
    public void insertFast(List<Marketing_Code_A> values) {

        String sql = " INSERT INTO " + tableName + " ( Name, Family, UserID ) VALUES ( ?, ?, ? )";

        SQLiteDatabase db = this.getWritableDatabase();

        db.beginTransactionNonExclusive();

        SQLiteStatement stmt = db.compileStatement(sql);

        for (int x = 0; x < values.size(); x++) {

            stmt.bindString(1, values.get(x).getName());
            stmt.bindString(2, values.get(x).getFamily());
            stmt.bindLong(3, values.get(x).getMarketCode());

            stmt.execute();
            stmt.clearBindings();

        }

        db.setTransactionSuccessful();
        db.endTransaction();

        db.close();
    }

    // deletes all records
    public void deleteRecords() {

        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("delete from " + tableName);
        db.close();
    }

    public List<Marketing_Code_A> selectFast() {
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.rawQuery("select * from " + tableName + " order by UserID; ", null);
        cursor.moveToFirst();

        List<Marketing_Code_A> MarkC = new ArrayList<>();
        try {
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        Marketing_Code_A MC = new Marketing_Code_A();
                        MC.setName(cursor.getString(cursor.getColumnIndex("Name")));
                        MC.setFamily(cursor.getString(cursor.getColumnIndex("Family")));
                        MC.setMarketCode(cursor.getInt(cursor.getColumnIndex("UserID")));
                        MarkC.add(MC);
                    } while (cursor.moveToNext());
                }
            }
        } catch (Exception ex) {
            return null;
        } finally {
            cursor.close();
            db.close();
        }
        return MarkC;

    }

    public void InsertFast2(List<Marketing_Points_B> values) {

        String sql = "INSERT INTO " + tableName2 + " ( Count, Date, Time, Lat, Lng, UserCode, LatLng ) VALUES ( ?, ?, ?, ?, ?, ?, ? )";

        SQLiteDatabase db = this.getWritableDatabase();

        db.beginTransactionNonExclusive();

        SQLiteStatement stmt = db.compileStatement(sql);

        for (int i = 0; i < values.size(); i++) {
            stmt.bindString(1, values.get(i).getCounts());
            stmt.bindString(2, values.get(i).getDate());
            stmt.bindString(3, values.get(i).getTime());
            stmt.bindString(4, String.valueOf(values.get(i).getLat()));
            stmt.bindString(5, String.valueOf(values.get(i).getLng()));
            stmt.bindString(6, values.get(i).getUserCode());
            stmt.bindString(7, String.valueOf(values.get(i).getmPosition()));
            stmt.execute();
            stmt.clearBindings();
        }

        db.setTransactionSuccessful();
        db.endTransaction();

        db.close();
    }

    public void deleteRecords2() {

        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("delete from " + tableName2);
        db.close();
    }

    public List<Marketing_Points_B> selectFast2() {
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.rawQuery("select * from " + tableName2 + " group by LatLng; ", null);
        cursor.moveToFirst();

        List<Marketing_Points_B> MarkP = new ArrayList<>();
        try {
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        Marketing_Points_B MP = new Marketing_Points_B();
                        MP.setLat(cursor.getDouble(cursor.getColumnIndex("Lat")));
                        MP.setLng(cursor.getDouble(cursor.getColumnIndex("Lng")));
                        MP.setUserCode(cursor.getString(cursor.getColumnIndex("UserCode")));
                        MarkP.add(MP);
                    } while (cursor.moveToNext());
                }
            }
        } catch (Exception ex) {
            return null;
        } finally {
            cursor.close();
            db.close();
        }
        return MarkP;

    }
}

我解决了我的问题,我改变了:

    String INDEX = "CREATE UNIQUE INDEX locations_index1 ON "
            + tableName + " (Name, Family, UserID)";


    String INDEX2 = "CREATE INDEX locations_index2 ON "
            + tableName2 + " (Count, Date, Time, Lat, Lng, UserCode, LatLng)";

这里:

locations_indexlocations_index1

locations_indexlocations_index2

我看到了:Android example source code file (DatabaseHelper.java)