Search code examples
androiddatabasesqliteandroid-studiogreendao

greenDAO Android no create tables in file database


I took the guide of https://blog.mindorks.com/powerful-android-orm-greendao-3-tutorial to integrate greenDAO in my android project, I follow everything (or so I think XD) and when I try on my mobile it is not created the tables, create the database file but when I open it with a viewer it doesn't show me anything.

I do not have how to test in the emulator to see if it is a problem of the mobile or of some permission that I need.

I will leave the project to see if you can help me, thank you very much.

https://anonfile.com/R6j6J2d5o1/Test_zip

file manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cafl.test">

    <application
        android:name="com.cafl.test.AppDbController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="com.cafl.test.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

file gradle project

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

file gradle module

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.cafl.test"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}
greendao{
    schemaVersion 1
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'org.greenrobot:greendao:3.2.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

the DaoMaster and DaoSession its creates to build project

and and as I am testing I am using the original DaoMaster function

mDaoSession = new DaoMaster(
                new DaoMaster.DevOpenHelper(this, "greendao_demo.db").getWritableDb()).newSession();

database file in my smathphone

sqlite viewer online empty

no matches found


Solution

  • Just check your project, it's working well. In MainActivity you can query and set to textView, remember set your textview id is nameTv:

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.nameTv);
    
        // Put this in a different thread or use AsyncSession in greenDAO.
        // For Demo purpose, this query is made on main thread but it should in a different thread.
        M_User user = ((AppDbController)getApplication()).getDaoSession().getM_UserDao().load(1L);
    
        if(user != null){
            textView.setText(user.getFullName());
        }
    }
    

    }