Java Code Examples for androidx.sqlite.db.SupportSQLiteDatabase#insert()

The following examples show how to use androidx.sqlite.db.SupportSQLiteDatabase#insert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void migrateGroup(final SupportSQLiteDatabase database) {
    database.execSQL("CREATE TABLE `groups_temp` " +
            "(`id` INTEGER PRIMARY KEY NOT NULL," +
            "`mesh_uuid` TEXT, " +
            "`name` TEXT, " +
            "`group_address` INTEGER NOT NULL DEFAULT 49152, " +
            "`parent_address` INTEGER NOT NULL DEFAULT 49152, " +
            "FOREIGN KEY(`mesh_uuid`) REFERENCES `mesh_network`(`mesh_uuid`) ON UPDATE CASCADE ON DELETE CASCADE )");

    final Cursor cursor = database.query("SELECT * FROM groups");
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final String uuid = cursor.getString(cursor.getColumnIndex("mesh_uuid"));
            final String name = cursor.getString(cursor.getColumnIndex("name"));
            final byte[] grpAddress = cursor.getBlob(cursor.getColumnIndex("group_address"));
            final byte[] pAddress = cursor.getBlob(cursor.getColumnIndex("parent_address"));
            final int groupAddress = MeshParserUtils.unsignedBytesToInt(grpAddress[1], grpAddress[0]);
            final ContentValues values = new ContentValues();
            values.put("mesh_uuid", uuid);
            values.put("name", name);
            values.put("group_address", groupAddress);
            if (pAddress != null) {
                final int parentAddress = MeshParserUtils.unsignedBytesToInt(pAddress[1], pAddress[0]);
                values.put("parent_address", parentAddress);
            }
            database.insert("groups_temp", SQLiteDatabase.CONFLICT_REPLACE, values);
        } while (cursor.moveToNext());
        cursor.close();
    }

    database.execSQL("DROP TABLE groups");
    database.execSQL("ALTER TABLE groups_temp RENAME TO groups");
    database.execSQL("CREATE INDEX index_groups_mesh_uuid ON `groups` (mesh_uuid)");
}
 
Example 2
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void addProvisionerNodes(@NonNull final SupportSQLiteDatabase database,
                                        @NonNull List<Provisioner> provisioners) {
    if (!provisioners.isEmpty()) {
        final List<NetworkKey> netKeys = getNetKeys(database);
        final List<ApplicationKey> appKeys = getAppKeys(database);
        final List<ProvisionedMeshNode> nodes = new ArrayList<>();
        for (Provisioner provisioner : provisioners) {
            final ProvisionedMeshNode node = new ProvisionedMeshNode(provisioner, netKeys, appKeys);
            final ContentValues values = new ContentValues();
            values.put("timestamp", node.getTimeStamp());
            values.put("name", node.getNodeName());
            values.put("mesh_uuid", node.getMeshUuid());
            values.put("uuid", node.getUuid());
            values.put("ttl", node.getTtl());
            values.put("blacklisted", node.isBlackListed());
            values.put("security", node.getSecurity());
            values.put("unicast_address", node.getUnicastAddress());
            values.put("configured", node.isConfigured());
            values.put("device_key", node.getDeviceKey());
            values.put("seq_number", node.getSequenceNumber());
            values.put("mElements", MeshTypeConverters.elementsToJson(node.getElements()));
            final List<Integer> networkKeys = new ArrayList<>();
            for (NetworkKey networkKey : netKeys) {
                networkKeys.add(networkKey.getKeyIndex());
            }
            final List<Integer> applicationKeys = new ArrayList<>();
            for (ApplicationKey applicationKey : appKeys) {
                applicationKeys.add(applicationKey.getKeyIndex());
            }
            if (!netKeys.isEmpty()) {
                values.put("netKeys", MeshTypeConverters.integerToJson(networkKeys));
            }
            if (!appKeys.isEmpty()) {
                values.put("appKeys", MeshTypeConverters.integerToJson(applicationKeys));
            }
            database.insert("nodes", SQLiteDatabase.CONFLICT_REPLACE, values);
        }
    }
}
 
Example 3
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void migrateMeshNetwork7_8(@NonNull final SupportSQLiteDatabase database) {
    database.execSQL("CREATE TABLE `mesh_network_temp` " +
            "(`mesh_uuid` TEXT NOT NULL, " +
            "`mesh_name` TEXT, " +
            "`timestamp` INTEGER NOT NULL, " +
            "`iv_index` TEXT NOT NULL, " +
            "`sequence_numbers` TEXT NOT NULL, " +
            "`last_selected` INTEGER NOT NULL, " +
            "PRIMARY KEY(`mesh_uuid`))");

    final Cursor cursor = database.query("SELECT * FROM mesh_network");
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final String uuid = cursor.getString(cursor.getColumnIndex("mesh_uuid"));
            final String meshName = cursor.getString(cursor.getColumnIndex("mesh_name"));
            final long timestamp = cursor.getInt(cursor.getColumnIndex("timestamp"));
            final int ivIndex = cursor.getInt(cursor.getColumnIndex("iv_index"));
            final int ivUpdateState = cursor.getInt(cursor.getColumnIndex("iv_update_state"));
            final String sequenceNumbers = cursor.getString(cursor.getColumnIndex("sequence_numbers"));
            final int lastSelected = cursor.getInt(cursor.getColumnIndex("last_selected"));
            final ContentValues values = new ContentValues();
            values.put("mesh_uuid", uuid);
            values.put("mesh_name", meshName);
            values.put("timestamp", timestamp);
            values.put("iv_index", MeshTypeConverters.ivIndexToJson(new IvIndex(ivIndex, ivUpdateState == MeshNetwork.IV_UPDATE_ACTIVE, Calendar.getInstance())));
            values.put("sequence_numbers", sequenceNumbers);
            values.put("last_selected", lastSelected);
            database.insert("mesh_network_temp", SQLiteDatabase.CONFLICT_REPLACE, values);
        } while (cursor.moveToNext());
        cursor.close();
    }
    database.execSQL("DROP TABLE mesh_network");
    database.execSQL("ALTER TABLE mesh_network_temp RENAME TO mesh_network");
}