2013-01-01 41 views
0

我想保存在SQLite数据库中的Android值检索SQLite数据库值,但我得到一个错误......快照附加以下链接存储和Android的

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

PS:这种方法,当我使用它没有数组.. – AbdulSaleem

回答

0

你得到插入错误的行中的数据得到的冲突进行保存。试试这些,并用这些替换您的插入代码::

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

希望这些可能会有所帮助!

+0

谢谢...这是chk = db1.insertWithOnConflict(manageAppDBHelper.TABLE2,null,values,SQLiteDatabase.CONFLICT_IGNORE); – AbdulSaleem

+0

@vikalp帕特尔先生,但它又造成了问题。你能看到https://www.dropbox.com/s/w1uq4ebwra9rcyt/pp.png https://www.dropbox.com/s/6vpw30cauqt9nrq/qq.png – AbdulSaleem

+0

我的意思是当我从这个创建的数据库得到的值 – AbdulSaleem