每次重新启动应用程序时,SQLite数据库都会被删除

时间:2018-08-13 11:14:36

标签: android sqlite android-sqlite

我创建了一个简单的android应用程序,用于在SQLite数据库中注册用户并列出他们。录音进行得很好,并插入到数据库中。 我还可以列出注册用户的列表。但是每次我重新启动应用程序时。数据库为空。我不知道为什么有人可以向我解释吗?

我在Android Studio 3.1上。

谢谢

ChanteurActivity.java

package com.example.odoo.ivencol.feature.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.odoo.ivencol.feature.R;
import com.example.odoo.ivencol.feature.adapters.ChanteurRecyclerAdapter;
import com.example.odoo.ivencol.feature.model.Chanteur;
//import com.example.odoo.ivencol.feature.sql.ChanteurDAO;
import com.example.odoo.ivencol.feature.sql.DatabaseHandler;

import java.util.ArrayList;
import java.util.List;

public class ChanteurActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener{
    private final AppCompatActivity activity = ChanteurActivity.this;

    private FloatingActionButton fabAddChanteur;
    private RecyclerView recyclerViewChanteur;
    private List<Chanteur> listChanteur;
    private ChanteurRecyclerAdapter chanteurRecyclerAdapter;
    private DatabaseHandler chanteurDAO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chanteur);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_chanteur);
        setSupportActionBar(toolbar);

        initViews();
        initListeners();
        initObjects();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_chanteur);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_chanteur);
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void initViews() {
        fabAddChanteur = findViewById(R.id.fab_chanteur);
        recyclerViewChanteur = (RecyclerView) findViewById(R.id.recyclerViewChanteur);
    }

    private void initListeners() {
        fabAddChanteur.setOnClickListener(this);
    }

    private void initObjects() {
        listChanteur = new ArrayList<>();
        chanteurRecyclerAdapter = new ChanteurRecyclerAdapter(listChanteur);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerViewChanteur.setLayoutManager(mLayoutManager);
        recyclerViewChanteur.setItemAnimator(new DefaultItemAnimator());
        recyclerViewChanteur.setHasFixedSize(true);
        recyclerViewChanteur.setAdapter(chanteurRecyclerAdapter);
        chanteurDAO = new DatabaseHandler(activity);

        getDataSQLite();
    }

    @SuppressLint("StaticFieldLeak")
    private void getDataSQLite() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                listChanteur.clear();
                listChanteur.addAll(chanteurDAO.getAllChanteur());

                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                chanteurRecyclerAdapter.notifyDataSetChanged();
            }
        }.execute();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_chanteur);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.chanteur, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_artiste) {

        }
        else if (id == R.id.nav_chanson) {
            finish();
            Intent intent = new Intent(getApplicationContext(),AddChanteurActivity.class);
            startActivity(intent);

        } else if (id == R.id.nav_recherche) {

        } else if (id == R.id.nav_quitter) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_chanteur);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.fab_chanteur) {
            finish();
            Intent intentAddChanteur = new Intent(getApplicationContext(), AddChanteurActivity.class);
            startActivity(intentAddChanteur);

        }
    }
}

DatabaseHandler.java

package com.example.odoo.ivencol.feature.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.odoo.ivencol.feature.model.Chanteur;

import java.util.ArrayList;
import java.util.List;

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int VERSION = 1;
    private static final String NOM_BD = "Ivencol.db";

    //----------------chanteurs table
    private static final String TABLE_NAME_CHANTEUR = "chanteur";
    private static final String ID_CHANTEUR = "id";
    private static final String NOM_CHANTEUR = "nom";
    private static final String SEXE = "sexe";
    private static final String DESCRIPTION = "description";

    private String TABLE_CREATE_CHANTEUR =
            "CREATE TABLE " + TABLE_NAME_CHANTEUR + " (" +
                    ID_CHANTEUR + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    NOM_CHANTEUR + " TEXT, " +
                    SEXE + " TEXT, " +
                    DESCRIPTION + " TEXT);";

    private String TABLE_DROP_CHANTEUR =  "DROP TABLE IF EXISTS " + TABLE_NAME_CHANTEUR + ";";

    //----------------chansons table
    public static final String TABLE_NAME_CHANSON = "chanson";
    public static final String ID_CHANSON = "id";
    public static final String CHANTEUR_ID = "chanteur_id";
    public static final String TITRE = "titre";
    public static final String CONTENU = "contenu";

    public static final String TABLE_CREATE_CHANSON =
            "CREATE TABLE " + TABLE_NAME_CHANSON + " (" +
                    ID_CHANSON + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
                    CHANTEUR_ID + " INTEGER, " +
                    TITRE + " TEXT, " +
                    CONTENU + " TEXT);";

    public static final String TABLE_DROP_CHANSON =  "DROP TABLE IF EXISTS " + TABLE_NAME_CHANSON + ";";

    public DatabaseHandler(Context context) {
        super(context, NOM_BD, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(TABLE_CREATE_CHANTEUR);
        //sqLiteDatabase.execSQL(TABLE_CREATE_CHANSON);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL(TABLE_DROP_CHANTEUR);
        //sqLiteDatabase.execSQL(TABLE_DROP_CHANSON);
        onCreate(sqLiteDatabase);
    }

    /**
     * @param m à ajouter à la base
     */
    public void addChanteur(Chanteur m) {
        // CODE
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(NOM_CHANTEUR, m.getNom());
        values.put(SEXE, m.getSexe());
        values.put(DESCRIPTION, m.getDescription());

        // Inserting Row
        db.insert(TABLE_NAME_CHANTEUR, null, values);
        db.close();
    }

    /**
     * @param id l'identifiant à supprimer
     */
    public void deleteChanteur(int id) {
        // CODE
    }

    /**
     * @param m  modifié
     */
    public void editChanteur(Chanteur m) {
        // CODE
    }

    /**
     *
     */
    public List<Chanteur> getAllChanteur() {
        // array column
        String[] columns = {
                ID_CHANTEUR,
                NOM_CHANTEUR,
                SEXE,
                DESCRIPTION
        };
        // sortes orders
        String sortOrder =
                NOM_CHANTEUR + " ASC";
        List<Chanteur> chantList = new ArrayList<>();

        SQLiteDatabase db = this.getWritableDatabase();

        // query uti table
        /**
         * SELECT uti_id,uti_nom,uti_mdp FROM utilisateur ORDER BY uti_nom;
         */
        Cursor cursor = db.query(TABLE_NAME_CHANTEUR, //Table query
                columns,    //columns à returner
                null,        //columns WHERE
                null,        //valeurs WHERE
                null,       //group lignes
                null,       //filtre par group lignes
                sortOrder); //sort order

        if (cursor.moveToFirst()) {
            do {
                Chanteur chant = new Chanteur();
                chant.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ID_CHANTEUR))));
                chant.setNom(cursor.getString(cursor.getColumnIndex(NOM_CHANTEUR)));
                chant.setSexe(cursor.getString(cursor.getColumnIndex(SEXE)));
                chant.setDescription(cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
                // Ajout uti record dans list
                chantList.add(chant);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();


        // return user list
        return chantList;
    }

//    /**
//     * @param id l'identifiant à récupérer
//     */
//    public Chanteur getChanteur(int id) {
//        // CODE
//
//        return ;
//    }
}

activity_chanteur.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout_chanteur"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_chanteur"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_chanteur"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_chanteur"
        app:menu="@menu/activity_chanteur_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_chanteur.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.ChanteurActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_chanteur"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_chanteur" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_chanteur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_add_black_24dp" />  //@android:drawable/ic_input_add

</android.support.design.widget.CoordinatorLayout>

content_chanteur.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activities.ChanteurActivity"
    tools:showIn="@layout/app_bar_chanteur">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewChanteur"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <android.support.v7.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

AndroidManifest.xml //基础

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

    <application
        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">
        <meta-data
            android:name="aia-compat-api-min-version"
            android:value="1" />
    </application>

</manifest>

AndroidManifest.xml //我的模块功能

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

    <application>
        <activity android:name=".activities.MainActivity">
            <intent-filter android:order="1">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />

                <data
                    android:host="example.com"
                    android:pathPattern="/feature"
                    android:scheme="https" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ChanteurActivity"
            android:label="@string/title_activity_chanteur"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter android:order="1">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />

                <data
                    android:host="example.com"
                    android:pathPattern="/feature"
                    android:scheme="https" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.AddChanteurActivity"
            android:label="@string/title_activity_add_chanteur">
            <intent-filter android:order="1">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />

                <data
                    android:host="example.com"
                    android:pathPattern="/feature"
                    android:scheme="https" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

本质上,SQLite明智的代码没有错。

正在运行以下代码,这是数据库代码的子集,可以产生预期的结果。

ChanteurActivity.java (已删除或最小化了非数据库代码):-

public class ChanteurActivity extends AppCompatActivity {
    private DatabaseHandler chanteurDAO;
    private List<Chanteur> clist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
        initListeners();
        initObjects();
    }

    private void initViews() {
    }

    private void initListeners() {
    }

    private void initObjects() {
        chanteurDAO = new DatabaseHandler(this);
        chanteurDAO.addChanteur(new Chanteur("Fred","male","Fred is a male."));
        chanteurDAO.addChanteur(new Chanteur("Mary","female","Mary is a female."));
        chanteurDAO.editChanteur(new Chanteur());
        clist = chanteurDAO.getAllChanteur();

        for (Chanteur c: clist) {
            StringBuilder sb = new StringBuilder("Name is ").append(c.getNom());
            sb.append(" Sex is ").append(c.getSexe());
            sb.append(" Description is ").append(c.getDescription());
            sb.append(" ID is ").append(c.getId());
            Log.d("CHANTEURLIST","Found Chanteur :- " + sb.toString());
        }
    }
}

DatabaseHelper.java (已修改为包括基本调试):-

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int VERSION = 1;
    private static final String NOM_BD = "Ivencol.db";

    //----------------chanteurs table
    private static final String TABLE_NAME_CHANTEUR = "chanteur";
    private static final String ID_CHANTEUR = "id";
    private static final String NOM_CHANTEUR = "nom";
    private static final String SEXE = "sexe";
    private static final String DESCRIPTION = "description";

    private String TABLE_CREATE_CHANTEUR =
            "CREATE TABLE " + TABLE_NAME_CHANTEUR + " (" +
                    ID_CHANTEUR + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    NOM_CHANTEUR + " TEXT, " +
                    SEXE + " TEXT, " +
                    DESCRIPTION + " TEXT);";

    private String TABLE_DROP_CHANTEUR =  "DROP TABLE IF EXISTS " + TABLE_NAME_CHANTEUR + ";";

    //----------------chansons table
    public static final String TABLE_NAME_CHANSON = "chanson";
    public static final String ID_CHANSON = "id";
    public static final String CHANTEUR_ID = "chanteur_id";
    public static final String TITRE = "titre";
    public static final String CONTENU = "contenu";

    public static final String TABLE_CREATE_CHANSON =
            "CREATE TABLE " + TABLE_NAME_CHANSON + " (" +
                    ID_CHANSON + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
                    CHANTEUR_ID + " INTEGER, " +
                    TITRE + " TEXT, " +
                    CONTENU + " TEXT);";

    public static final String TABLE_DROP_CHANSON =  "DROP TABLE IF EXISTS " + TABLE_NAME_CHANSON + ";";

    public DatabaseHandler(Context context) {
        super(context, NOM_BD, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        Log.d("DBHELPER_ONCREATE","OnCreate initiated.");
        sqLiteDatabase.execSQL(TABLE_CREATE_CHANTEUR);
        //sqLiteDatabase.execSQL(TABLE_CREATE_CHANSON);
        Log.d("DBHELPER_ONCREATE","OnCreate has run.");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL(TABLE_DROP_CHANTEUR);
        //sqLiteDatabase.execSQL(TABLE_DROP_CHANSON);
        onCreate(sqLiteDatabase);
    }

    /**
     * @param m à ajouter à la base
     */
    public void addChanteur(Chanteur m) {
        Log.d("DBHELPER_AddChanteur","Initiated");
        // CODE
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(NOM_CHANTEUR, m.getNom());
        values.put(SEXE, m.getSexe());
        values.put(DESCRIPTION, m.getDescription());

        // Inserting Row
        if (db.insert(TABLE_NAME_CHANTEUR, null, values) > -1) {
            Log.d("DBHELPER_AddChanteur","Chanteur Added");
        } else {
            Log.d("DBHELPER","Chanteur not Added");
        }
        db.close();
    }

    /**
     * @param id l'identifiant à supprimer
     */
    public void deleteChanteur(int id) {
        // CODE
    }

    /**
     * @param m  modifié
     */
    public void editChanteur(Chanteur m) {
        // CODE
    }

    /**
     *
     */
    public List<Chanteur> getAllChanteur() {
        // array column
        String[] columns = {
                ID_CHANTEUR,
                NOM_CHANTEUR,
                SEXE,
                DESCRIPTION
        };
        // sortes orders
        String sortOrder =
                NOM_CHANTEUR + " ASC";
        List<Chanteur> chantList = new ArrayList<>();

        SQLiteDatabase db = this.getWritableDatabase();

        // query uti table
        /**
         * SELECT uti_id,uti_nom,uti_mdp FROM utilisateur ORDER BY uti_nom;
         */
        Cursor cursor = db.query(TABLE_NAME_CHANTEUR, //Table query
                columns,    //columns à returner
                null,        //columns WHERE
                null,        //valeurs WHERE
                null,       //group lignes
                null,       //filtre par group lignes
                sortOrder); //sort order
        Log.d("DBHELPER_getAll","Number of rows extracted is " + cursor.getCount());
        if (cursor.moveToFirst()) {
            do {
                Chanteur chant = new Chanteur();
                chant.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ID_CHANTEUR))));
                chant.setNom(cursor.getString(cursor.getColumnIndex(NOM_CHANTEUR)));
                chant.setSexe(cursor.getString(cursor.getColumnIndex(SEXE)));
                chant.setDescription(cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
                // Ajout uti record dans list
                chantList.add(chant);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();

        // return user list
        return chantList;
    }

//    /**
//     * @param id l'identifiant à récupérer
//     */
//    public Chanteur getChanteur(int id) {
//        // CODE
//
//        return ;
//    }
}

运行以上结果将在日志中包含以下内容:-

08-14 03:54:26.812 3675-3675/? D/DBHELPER_AddChanteur: Initiated
08-14 03:54:26.872 3675-3675/? D/DBHELPER_ONCREATE: OnCreate initiated.
    OnCreate has run.
08-14 03:54:26.880 3675-3675/? D/DBHELPER_AddChanteur: Chanteur Added
    Initiated
08-14 03:54:26.888 3675-3675/? D/DBHELPER_AddChanteur: Chanteur Added
08-14 03:54:26.888 3675-3675/? D/DBHELPER_getAll: Number of rows extracted is 2
08-14 03:54:26.888 3675-3675/? D/CHANTEURLIST: Found Chanteur :- Name is Fred Sex is male Description is Fred is a male. ID is 1
    Found Chanteur :- Name is Mary Sex is female Description is Mary is a female. ID is 2

第二次运行会导致:-

08-14 03:56:22.543 3722-3722/? D/DBHELPER_AddChanteur: Initiated
08-14 03:56:22.567 3722-3722/? D/DBHELPER_AddChanteur: Chanteur Added
    Initiated
08-14 03:56:22.575 3722-3722/? D/DBHELPER_AddChanteur: Chanteur Added
08-14 03:56:22.575 3722-3722/? D/DBHELPER_getAll: Number of rows extracted is 4
08-14 03:56:22.575 3722-3722/? D/CHANTEURLIST: Found Chanteur :- Name is Fred Sex is male Description is Fred is a male. ID is 1
    Found Chanteur :- Name is Fred Sex is male Description is Fred is a male. ID is 3
    Found Chanteur :- Name is Mary Sex is female Description is Mary is a female. ID is 2
    Found Chanteur :- Name is Mary Sex is female Description is Mary is a female. ID is 4
  • 请注意如何不调用onCreate,即数据库存在并且日志中没有08-14 03:54:26.872 3675-3675/? D/DBHELPER_ONCREATE: OnCreate initiated. OnCreate has run.
  • 提取并返回预期的行数 4

您永远不要做任何假设,而应该始终进行调试,并确定实际问题出在哪里。

您的问题与代码的其他方面有关。

答案 1 :(得分:0)

我解决了这个问题。

这对我来说是一个可怕的错误。实际上,我通过“ instantapp”而不是“ app”配置启动了该应用程序。 我不知道为什么,但是如果我使用“ instantapp”配置(每次都这样做)运行应用程序,则每次从虚拟设备重新运行时,该表都会变为空白。 (如果有人可以解释我?)

如果我使用“ app”配置启动应用程序,则该应用程序运行良好,并且每次重新运行虚拟设备时该表都不会再为空。 如果像我这样的初学者以后遇到此问题,我会发布此决议。

非常感谢所有为我提供解决方案的人,以及那些花时间调试我的应用程序的人。

enter image description here