首页 > 解决方案 > Android 应用程序中是否使用了数据库?

问题描述

了解如何在 Android 应用中使用数据库。

我想了解如何将 SQL 数据库连接到 Android 应用程序。它就像 YouTube 和更多使用数据库的应用程序。

请帮我谈谈在 Android 应用程序中使用数据库。

谢谢大家

标签: androiddatabasekotlin

解决方案


您可以使用android sqlite 数据库

以及更多信息SQLiteDatabase

使用此代码连接到数据库

public class DBHelper extends SQLiteOpenHelper
{

    private static String DB_NAME = "DataBase.sqlite";
    private static String DB_PATH = "";
    private static final int DB_VERSION = 63;

    private SQLiteDatabase mDataBase;
    private final Context mContext;
    private boolean mNeedUpdate = false;


    public DBHelper(Context context)
    {
        super(context, DB_NAME, null, DB_VERSION);
        this.mContext = context;

        DB_PATH = mContext.getApplicationInfo().dataDir + "/databases/";

        boolean dbexist = checkDataBase();
        if (dbexist)
        {
            openDataBase();
        }
        else
        {
            createDataBase();
        }
    }

    private void createDataBase()
    {
        boolean dbexist = checkDataBase();
        if (!dbexist)
        {
            this.getReadableDatabase();
            this.close();
            try
            {
                copyDataBase();
            }
            catch (IOException e)
            {
                e.printStackTrace();
              
            }
        }
    }

    private boolean checkDataBase()
    {
        boolean checkdb = false;
        try
        {
            String myPath = DB_PATH + DB_NAME;
            File dbfile = new File(myPath);
            checkdb = dbfile.exists();
        }
        catch (SQLiteException e)
        {
            e.printStackTrace();
         
        }
        return checkdb;
    }

    private void copyDataBase() throws IOException
    {
        InputStream myinput = mContext.getAssets().open("G2DcfxJZdu/" + DB_NAME);
        OutputStream myoutput = new FileOutputStream(DB_PATH + DB_NAME);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myinput.read(buffer)) > 0)
        {
            myoutput.write(buffer, 0, length);
        }
        myoutput.flush();
        myoutput.close();
        myinput.close();
    }

    private void openDataBase() throws SQLException
    {
        String mypath = DB_PATH + DB_NAME;
        mDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public synchronized void close()
    {
        if (mDataBase != null)
        {
            mDataBase.close();
        }
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        if (newVersion > oldVersion)
        {
            mNeedUpdate = true;
            try
            {
                updateDataBase();
            }
            catch (Exception exception)
            {
                exception.printStackTrace();
            }
        }
    }


    private void updateDataBase() throws IOException
    {
        if (mNeedUpdate)
        {
            File dbFile = new File(DB_PATH + DB_NAME);
            if (dbFile.exists())
            {
                dbFile.delete();
            }

            copyDataBase();

            mNeedUpdate = false;
        }
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        db.disableWriteAheadLogging();
    }

推荐阅读