SQLite列名不是唯一错误?

时间:2014-07-15 15:31:06

标签: android sqlite

我正在使用数据库帮助程序类为我的应用程序创建数据库,然后我尝试在另一个类中插入数据。但是我收到了一个错误。

我在Logcat中收到以下错误,不知道如何解决它? :

07-15 16:20:30.348: E/SQLiteDatabase(16270): Error inserting score=0 date=1405437630327 name=ry
07-15 16:20:30.348: E/SQLiteDatabase(16270): android.database.sqlite.SQLiteConstraintException: column name is not unique (code 19)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:976)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1591)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1461)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at com.example.multapply.DatabaseHelper.addScore(DatabaseHelper.java:84)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at com.example.multapply.RandomTest.onClick(RandomTest.java:161)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.view.View.performClick(View.java:4633)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.view.View$PerformClick.run(View.java:19330)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.os.Handler.handleCallback(Handler.java:733)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.os.Looper.loop(Looper.java:157)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at android.app.ActivityThread.main(ActivityThread.java:5356)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at java.lang.reflect.Method.invokeNative(Native Method)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at java.lang.reflect.Method.invoke(Method.java:515)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-15 16:20:30.348: E/SQLiteDatabase(16270):    at dalvik.system.NativeStart.main(Native Method)

相关代码:

数据库助手类:

package com.example.multapply;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

    public class DatabaseHelper extends SQLiteOpenHelper {

        // Database Version
        private static final int DATABASE_VERSION = 3;

        // Database Name
        private  final static String DATABASE_NAME = "MultapplyDatabase"; 

        // Contacts table name
        private static final String TABLE_SCORE = "scores";

        // Contacts Table Columns names
        private static final String COL_NAME = "name";
        private static final String COL_SCORE = "score";
        private static final String COL_DATE = "date";



        /**
         * Constructor
         * @param context
         */
        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }


        /**
         * Method that creates the database
         */
        @Override
        public void onCreate(SQLiteDatabase db) {

            //NOTE: may need to alter the below to take out everything after INTEGER
            String CREATE_TABLE_SCORE = "CREATE TABLE " + TABLE_SCORE + "("
                    + COL_NAME + " STRING PRIMARY KEY," + COL_SCORE + " INTEGER," + COL_DATE + " LONG" + ")";
            db.execSQL(CREATE_TABLE_SCORE);


        }

        /**
         * Method that upgrades the database
         */
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            // Drop older table if existed 
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE); 

            // Create tables again
            onCreate(db);


        }

        /**
         * All CRUD operations
         */
        // Adding new score details (Name, score, date)
        void addScore(Score score) {
            SQLiteDatabase db = this.getWritableDatabase();

            //ContentValues- holds the values.
            ContentValues values = new ContentValues();
            values.put(COL_NAME, score.getName()); 
            values.put(COL_SCORE, score.getScore()); 
            values.put(COL_DATE, score.getDate());


            // Inserting Row (i.e. the values that were entered from above
            db.insert(TABLE_SCORE, null, values);
            db.close(); // Closing database connection

    }
        /**
         * Method will return a single Name and score
         * @param id
         * @return
         */
        // Getting single contact
        Score getScore(String name) {
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cursor = db.query(TABLE_SCORE, new String[] { COL_NAME,
                    COL_SCORE, COL_DATE}, COL_NAME + "=?",
                    new String[] { String.valueOf(name) }, null, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();

            Score score = new Score(cursor.getString(0),Integer.parseInt(cursor.getString(1)),cursor.getLong(2));
            // return contact
            return score;
        }

        /**
         * Method will return a list of all the scores
         * @return
         */
        // Getting All Contacts
        public List<Score> getAllScores() {
            List<Score> scoreList = new ArrayList<Score>();
            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_SCORE;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Score score = new Score();
                    score.setName(cursor.getString(0));
                    score.setScore(Integer.parseInt(cursor.getString(1)));
                    score.setDate(cursor.getLong(2));
                    // Adding contact to list
                    scoreList.add(score);
                } while (cursor.moveToNext());
            }

            // return contact list
            return scoreList;
        }

    }

与将数据输入数据库相关的代码:

/**
             * CRUD Operations
             * */
            // Inserting Contacts
            Log.d("Insert: ", "Inserting ..");

            db.addScore(new Score(UserName.getUserName(), score, System
                    .currentTimeMillis()));

            // Reading all contacts
            Log.d("Reading: ", "Reading all contacts..");
            List<Score> scores = db.getAllScores();

            for (Score s : scores) {
                String log = "Name: " + s.getName() + " ,Score: "
                        + s.getScore() + "Date: " + s.getDate();
                // Writing Contacts to log
                Log.d("Details: ", log);
            }

        }

    }

2 个答案:

答案 0 :(得分:3)

这意味着您正在尝试使用已在数据库中保存或使用的“名称”列中的参数,因为“name”是主键

答案 1 :(得分:0)

您违反了UNIQUE约束,因为您的列名称主键。这是SQLiteConstraintException的原因。