簡體   English   中英

在Android中存儲和檢索SQLite數據庫值

[英]Storing and retrieving SQLite database values in Android

我正在嘗試將值保存到Android中的SQLite數據庫中,但是出現錯誤...快照附在下面的鏈接中

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

當行中的數據發生沖突時,您將收到插入錯誤。 試試這些,並用以下代碼替換您的插入代碼:

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

希望這些對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM