在Android中存储和检索SQLite数据库值

时间:2013-01-01 11:34:56

标签: android database insert android-sqlite

我正在尝试在Android中的SQLite数据库中保存值,但是我收到错误...快照附在下面的链接上

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);
}
}

1 个答案:

答案 0 :(得分:0)

当行中的数据发生冲突时,您会收到插入错误。试试这些并用这些::

替换你的插入代码
 chk = db1.insertWithOnConflict(manageAppDBHelper.TABLE2, null, values, CONFLICT_IGNORE);

希望这些可能有所帮助!