如何在Sqlite中更新,插入和删除行?

时间:2019-07-02 07:58:23

标签: android sqlite

我是android开发的新手,我已经完成了一些教程,并使用sqlite Db编写了一个应用程序。

我有几个问题,如下所述 1.如何更新数据库中的行? 2.删除更新的行? 3.如何插入新行?

//下面的代码用于更新行

struct S{
        S* n
        int v;
        S(int V, S* N): n(N), v(V){};
        };

S s = S(10,new S(20,&s));
cout << (s.n->n->v);

删除特定行-下面的代码清除数据库中的全部而不是特定行

SQLiteDatabase db = dbHandler.getWritableDatabase();
for (int i = 0; i <= 100000; i = i + 100) {
    ContentValues cValues = new ContentValues();
    cValues.put(DbHandler.COLUMN_ID, i + 1);
    cValues.put(DbHandler.COLUMN_NAME, "Test");
    cValues.put(DbHandler.COLUMN_SERIALNUMBER, String.valueOf(i + 1));
    cValues.put(DbHandler.COLUMN_COUNTALL, 10);
    cValues.put(DbHandler.COLUMN_COUNTHIGH, i + 1);
    cValues.put(DbHandler.COLUMN_COUNTLOW, i + 1);
    cValues.put(DbHandler.COLUMN_RAWDATA, i + 1);
    db.update(DbHandler.TABLE_USERS, cValues, DbHandler.COLUMN_ID,new String[]{String.valueOf(i)});
}
db.close();

3 个答案:

答案 0 :(得分:2)

这是在SQLite中执行更新,插入和删除操作的方法

  1. insertData

    public long insertData() {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues values = new ContentValues();
    
      values.put(KEY_TITLE, "Title");
      values.put(KEY_ACTIVITY_TYPE,"Event Type");
      values.put(KEY_PLACE, "Event place");
      values.put(KEY, "VALUE");
      values.put(KEY_, "VALUE");
    
      long id = db.insert(TABLE_NAME, null, values);
      return id;
    }
    
  2. updateRow

    public int updateRow(String row_id) {
    
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    
    values.put(KEY_TITLE, "Title");
    values.put(KEY_ACTIVITY_TYPE,"Event Type");
    values.put(KEY_PLACE, "Event place");
    values.put(KEY, "VALUE");
    values.put(KEY_, "VALUE");
    
    return db.update(TABLE_NAME, values, KEY_ID + " = ?",
            new String[]{row_id});
    }
    
  3. deleteRow

    public int deleteRow(String row_id){     SQLiteDatabase db = this.getWritableDatabase();     ContentValues值=新的ContentValues();     return db.delete(TABLE_NAME,WHERE KEY_ID +“ =?”,新的String [] {row_id}); }

答案 1 :(得分:1)

您可以编写dao类(数据访问对象)并使用该类 这很简单,您只需在数据库上运行sql查询 这个template literal将为您提供帮助

例如插入

        val query = ("insert into " + DownloaderDatabase.TABLE_NAME + " " +
            "(" +
            ColumnModel.URL + "," +
            ColumnModel.FILE_NAME + "," +
            ColumnModel.STATUS + "," +
            ColumnModel.PERCENT + "," +
            ColumnModel.SIZE + "," +
            ColumnModel.TOTAL_SIZE
            + ")" +
            " VALUES(" + "\"" + fileModel.url + "\"" + ","
            + " " + "\"" + fileModel.fileName + "\"" + ","
            + " " + fileModel.status + ","
            + " " + fileModel.percent + ","
            + " " + fileModel.size + ","
            + " " + fileModel.totalSize + ")")
    database.writableDatabase.execSQL(query)
    database.close()

例如更新

  val query = "update " + DownloaderDatabase.TABLE_NAME + " " +
            " set " + ColumnModel.STATUS + " = " + status +
            " , " + ColumnModel.PERCENT + " = " + percent +
            " , " + ColumnModel.SIZE + " = " + size +
            " , " + ColumnModel.TOTAL_SIZE + " = " + totalSize +
            " where " + ColumnModel.URL + " = " + "\"" + url + "\""
    database.writableDatabase.execSQL(query)
    database.close()

例如获取数据

        var model: FileModel? = null
    val query =
        "select * from " + DownloaderDatabase.TABLE_NAME + " where " + ColumnModel.URL + " = \"" + url + "\""
    val cursor = database.readableDatabase.rawQuery(query, null)
    while (cursor.moveToNext()) {
        val itemId = cursor.getInt(cursor.getColumnIndex(ColumnModel.ID))
        val itemUrl = cursor.getString(cursor.getColumnIndex(ColumnModel.URL))
        val itemFileName = cursor.getString(cursor.getColumnIndex(ColumnModel.FILE_NAME))
        val itemStatus = cursor.getInt(cursor.getColumnIndex(ColumnModel.STATUS))
        val itemPercent = cursor.getInt(cursor.getColumnIndex(ColumnModel.PERCENT))
        val itemSize = cursor.getInt(cursor.getColumnIndex(ColumnModel.SIZE))
        val itemTotalSize = cursor.getInt(cursor.getColumnIndex(ColumnModel.TOTAL_SIZE))
        model = FileModel(itemId, itemUrl, itemFileName, itemStatus, itemPercent, itemSize, itemTotalSize)
    }
    cursor.close()
    database.close()
    return model

答案 2 :(得分:0)

如果要在表中插入行,请使用ContentValues(就像您的更新代码一样)和insert()方法:

SQLiteDatabase db = dbHandler.getWritableDatabase();
for (int i = 0; i < 3; i++) {
    ContentValues cValues = new ContentValues();
    cValues.put(DbHandler.COLUMN_ID, i + 1);
    cValues.put(DbHandler.COLUMN_NAME, "Test");
    cValues.put(DbHandler.COLUMN_SERIALNUMBER, String.valueOf(i + 1));
    cValues.put(DbHandler.COLUMN_COUNTALL, 10);
    cValues.put(DbHandler.COLUMN_COUNTHIGH, i + 1);
    cValues.put(DbHandler.COLUMN_COUNTLOW, i + 1);
    cValues.put(DbHandler.COLUMN_RAWDATA, i + 1);
    db.insert(DbHandler.TABLE_USERS, null, cValues);
}
db.close();

上面的代码将在表中插入3行:

1 Test 1 10 1 1 1
2 Test 2 10 2 2 2
3 Test 1 10 3 3 3

现在,假设您要用{将行的COLUMN_COUNTHIGH列更新为100,将列COLUMN_COUNTLOW更新为0 {1}},然后使用update()方法:

id = 2

因此SQLiteDatabase db = dbHandler.getWritableDatabase(); ContentValues cValues = new ContentValues(); cValues.put(DbHandler.COLUMN_COUNTHIGH, 100); cValues.put(DbHandler.COLUMN_COUNTLOW, 0); db.update(DbHandler.TABLE_USERS, cValues, DbHandler.COLUMN_ID + " = ?", new String[] {String.valueOf(2)}); db.close(); 方法的参数为:
表格名称
update()
ContentValues子句,其中包含列名和占位符WHERE,这些占位符将决定要更新的行
以及将在?
处设置的列的值 如果要删除,请用?说行,请使用delete()方法:

id = 1

自变量与db.delete(DbHandler.TABLE_USERS, DbHandler.COLUMN_ID + " = ?", new String[] {String.valueOf(1)}); 方法的自变量相似(没有ContentValues)。