Search code examples
androidsqlitekotlinkotlin-android-extensionsanko

Type interference failed. Expected type mismatch: required String found pair<String,String> in Kotlin and Anko


I have created a database using kotlin and anko library. I am following this article https://antonioleiva.com/databases-anko-kotlin/ I am trying to insert data inside database block using below but I am getting an error

Type interference failed. Expected type mismatch: required String found pair

enter image description here

fun insertPerson() {
    database.use {
        insert(PersonTable.Name,
                PersonTable.PersonName to "XX",
                PersonTable.Domain to "Technology",
                PersonTable.MobileNumber to "XXXXX")
    }
}

object PersonTable {
    val Name = "Person"
    val ID = "id"
    val PersonName = "person_name"
    val Domain = "domain"
    val MobileNumber = "mobile_number"
}

build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.williams.fourthdemo"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile "org.jetbrains.anko:anko-common:0.10.1"
    compile "org.jetbrains.anko:anko-sqlite:0.10.1"
}

Solution

  • Insert method that accepts Pair is extension function which can be found in this file. Android Studio often can't find such method. So import it:

    import org.jetbrains.anko.db.Database.insert
    

    or

    import org.jetbrains.anko.db.*