0

Android の SQLite DB に値を保存しようとしていますが、エラーが発生します...下のリンクにスナップショットが添付されています

https://www.dropbox.com/s/bb71am98riscutn/aa.png

これが私がdbに保存しようとする方法です

long chk = 0;
SQLiteDatabase db1 = userInstance.getWritableDatabase();
ContentValues values = new ContentValues();

for (int i = 0; i < p; i++) // p is the max limit  
{ 
    values.put(manageAppDBHelper.DBAppName[i], AppName[i]); // manageAppDBHelper
                                                            // is a
                                                            // class
}

chk = db1.insert(manageAppDBHelper.TABLE2, null, values);
Log.v("check", chk + "");
if (chk != -1) {
    Toast.makeText(getBaseContext(), "DATABASE updated ... ",
            Toast.LENGTH_LONG).show();
}
db1.close(); // Closing database connection

/** Helper to the database, manages versions and creation */
public class manageAppDBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Table name
public static final String TABLE2 = "appliances";
private static final String DATABASE_NAME = "mbpto_user8.db";

public static final String[] DBAppName = { "Appliance1", "Appliance2",
        "Appliance3", "Appliance4", "Appliance5", "Appliance6",
        "Appliance7", "Appliance8", "Appliance9", "Appliance10",
        "Appliance11", "Appliance12", "Appliance13", "Appliance14",
        "Appliance15", "Appliance16", "Appliance17", "Appliance18",
        "Appliance19", "Appliance20", "Appliance21", "Appliance22",
        "Appliance23", "Appliance24", "Appliance25", "Appliance26",
        "Appliance27", "Appliance28", "Appliance29", "Appliance30" }; // 30nullvalues

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

@Override
public void onCreate(SQLiteDatabase db) {

    String sql = "create table " + TABLE2 + "( " + BaseColumns._ID
            + " integer primary key autoincrement, " + DBAppName[0]
            + " text not null," + DBAppName[1] + " text not null, "
            + DBAppName[2] + " text not null," + DBAppName[3]
            + " text not null," + DBAppName[4] + " text not null, "
            + DBAppName[5] + " text not null," + DBAppName[6]
            + " text not null," + DBAppName[7] + " text not null, "
            + DBAppName[8] + " text not null," + DBAppName[9]
            + " text not null," + DBAppName[10] + " text not null, "
            + DBAppName[11] + " text not null," + DBAppName[12]
            + " text not null," + DBAppName[13] + " text not null, "
            + DBAppName[14] + " text not null," + DBAppName[15]
            + " text not null," + DBAppName[16] + " text not null, "
            + DBAppName[17] + " text not null," + DBAppName[18]
            + " text not null," + DBAppName[19] + " text not null, "
            + DBAppName[20] + " text not null," + DBAppName[21]
            + " text not null," + DBAppName[22] + " text not null, "
            + DBAppName[23] + " text not null," + DBAppName[24]
            + " text not null," + DBAppName[25] + " text not null, "
            + DBAppName[26] + " text not null," + DBAppName[27]
            + " text not null," + DBAppName[28] + " text not null, "
            + DBAppName[29] + " text not null," + DBAppName[30]
            + " text not null); ";

    Log.d("Updating Appliance Database", "onCreate: " + sql);
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    if (oldVersion >= newVersion)
        return;

    String sql = null;
    if (oldVersion == 1)
        sql = "alter table " + TABLE2 + " add note text;";
    if (oldVersion == 2)
        sql = "";

    Log.d("Adding Appliance Database", "onUpgrade   : " + sql);
    if (sql != null)
        db.execSQL(sql);
}
}
4

1 に答える 1

0

行のデータが競合するため、挿入エラーが発生しています。これらを試して、挿入コードをこれらに置き換えてください::

 chk = db1.insertWithOnConflict(manageAppDBHelper.TABLE2, null, values, CONFLICT_IGNORE);

これらが役立つことを願っています!

于 2013-01-01T18:15:24.640 に答える