列ID不是唯一的

时间:2014-08-21 10:23:09

标签: android database sqlite

我已阅读并知道如果列id(主键)不唯一,则会产生上述错误。 但据我所知,这不是我的情况。

我创建了一个名为Databasehandler的类,其中包含许多函数(包含5个表)。然后我创建了其他使用DBH中的函数的类/活动。

我添加了两个报告部分,一个是最新的,另一个是旧报告。

现在,当我想要使用某个函数时,它会返回“列id不唯一(代码19)”错误。

这是我的代码的一部分。

我真的很困惑,感谢任何帮助。

package com.amirkh.taghzie;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
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;
import android.view.Gravity;
import android.widget.Toast;

public class DataBaseHand extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Taghziemanager";
    private static final String TABALE_BMI = "bmitable";
    private static final String TABALE_OLDREPORT = "oldreport";
    private static final String TABALE_WATER = "water";
    private static final String TABALE_OLDWATER = "oldwater";
    private static final String TABALE_INTAKECAL = "intakecal";
    private static final String KEY_CALORIENEED = "neededcalorie";
    private static final String TABALE_FOODS = "foods";
    private static final String TABALE_DAILY = "daily";
    private static final String KEY_ID = "id";
    private static final String KEY_ROID = "roid";
    private static final String KEY_DATEE = "datee";
    private static final String KEY_IDC = "_id";
    private static final String KEY_FOODNAME = "name";
    private static final String KEY_UNITS = "units";
    private static final String KEY_CALORIE = "calorie";
    private static final String KEY_COMMENT = "comment";
    private static final String KEY_WATER = "waterdaily";
    private static final String KEY_BMI = "bmidata";
    private static final String KEY_FOODUNIT = "foodunit";
    private static final String KEY_TOTALCALORIE = "totalcalorie";
    private static String wa = "0";   
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("PRAGMA foreign_keys=ON;");

    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABALE_BMI + " (" + KEY_ID
                + " INTEGER PRIMARY KEY, " + KEY_BMI + " TEXT, "
                + KEY_TOTALCALORIE + " TEXT);");
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABALE_INTAKECAL + " ("
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_CALORIENEED + " TEXT);");

        db.execSQL("INSERT INTO intakecal VALUES ('1','2500');");
                db.execSQL("CREATE TABLE IF NOT EXISTS " + TABALE_WATER + " (" + KEY_ID
                        + " INTEGER PRIMARY KEY, " + KEY_WATER + " TEXT);");
                db.execSQL("INSERT INTO water VALUES ('1','0');");
                db.execSQL("CREATE TABLE IF NOT EXISTS " + TABALE_DAILY + " ("
                + KEY_IDC + " INTEGER PRIMARY KEY, " + KEY_FOODNAME + " TEXT, "
                + KEY_FOODUNIT + " TEXT, " + KEY_ROID + " TEXT, " + KEY_DATEE
                + " TEXT );");
    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABALE_FOODS + " (" + KEY_ID
                + " INTEGER PRIMARY KEY, " + KEY_FOODNAME + " TEXT, "
                + KEY_COMMENT + " TEXT, " + KEY_CALORIE + " TEXT, "
                + KEY_FOODUNIT + " TEXT);");     

这是一个函数:

    public List<Foods> getfoodlist() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABALE_FOODS, null);
        List<Foods> fl = new ArrayList<Foods>();
        if (cursor.moveToFirst()) {
            do {
                Foods food = new Foods("", "", "", "");
                food.setId(cursor.getString(0));
                food.setNamee(cursor.getString(1));
                food.setCommeent(cursor.getString(2));
                food.setCalor(cursor.getString(3));
                food.setUnit(cursor.getString(4));
                fl.add(food);
            } while (cursor.moveToNext());
        }

        return fl;

    }

以及我使用的一项活动:

 public class Report extends ActionBarActivity {
    DataBaseHand dbh = new DataBaseHand(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        Button btnback = (Button) findViewById(R.id.btnreportback);
            dbh.ResetDataBase();

        Button btnoldreport=(Button) findViewById(R.id.btnreportyesterday);
        Button btnreport=(Button) findViewById(R.id.btnreportreport);
        List<EatenFood> fl = dbh.GetEatenlist();

我在fl上犯了错误。 (最后一行)或使用我的数据库的任何其他功能。

任何帮助将不胜感激,如果您需要更多信息,代码本身太长,请告诉我。

编辑:

输入来自用户的所有5个表,即:

     public void addCalorie(String calorieNeeded) {

        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DROP TABLE IF EXISTS " + TABALE_INTAKECAL);
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABALE_INTAKECAL + " ("
                + KEY_ID + " TEXT PRIMARY KEY, " + KEY_CALORIENEED + " TEXT);");

        ContentValues content = new ContentValues();
        content.put(KEY_CALORIENEED, calorieNeeded);
        db.insert(TABALE_INTAKECAL, null, content);
            }

编辑2: 将所有TEXT PRIMARY KEY更改为INTEGER PRIMARY KEY(因为它们是自动增量)仍然没有运气

编辑3:Logcat:

 08-21 11:56:13.146: E/AndroidRuntime(2987): FATAL EXCEPTION: main
 08-21 11:56:13.146: E/AndroidRuntime(2987): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amirkh.taghzie/com.amirkh.taghzie.Foodlist}: android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.os.Handler.dispatchMessage(Handler.java:99)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.os.Looper.loop(Looper.java:137)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.app.ActivityThread.main(ActivityThread.java:5041)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at java.lang.reflect.Method.invokeNative(Native Method)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at java.lang.reflect.Method.invoke(Method.java:511)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at dalvik.system.NativeStart.main(Native Method)
 08-21 11:56:13.146: E/AndroidRuntime(2987): Caused by: android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:727)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at com.amirkh.taghzie.DataBaseHand.onCreate(DataBaseHand.java:84)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at com.amirkh.taghzie.DataBaseHand.getfoodlist(DataBaseHand.java:135)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at com.amirkh.taghzie.Foodlist.onCreate(Foodlist.java:44)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.app.Activity.performCreate(Activity.java:5104)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 08-21 11:56:13.146: E/AndroidRuntime(2987):    ... 11 more

1 个答案:

答案 0 :(得分:1)

堆栈跟踪表示您正在尝试将非唯一值插入第84行DataBaseHand.onCreate()中的唯一/主键列。

之前为什么它“有效”可能是因为你已经拥有了一个数据库。数据库助手onCreate()仅在刚从头创建数据库文件时运行一次。如果更改数据库文件名,卸载应用程序或清除应用程序的数据,则不会有数据库文件,因此再次调用onCreate()并且问题变得明显。