0

我正在尝试将值保存在 Android 中的 SQLite DB 中,但出现错误...下面的链接上附有快照

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

这是我尝试保存在数据库中的方法

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 回答