列出存储到数据库,并检索它们放在一起:安卓放在一起、数据库

2023-09-12 10:29:11 作者:何必咎由自取

我想存储的字符串列表,然后检索它们。我将如何做到这一点使用数据库?

I want to store a list of strings, and then retrieve them. How would I do this using a database?

推荐答案

在理想情况下,从一个结构化的方式处理这一来有你的主要业务/服务的方式与活动直接与数据库,而不是通信的实用工具类交互/服务它的自我。

Ideally the way to approach this from a structured way to have your main activity/service interact with a Utility class that directly communicates with the database instead of the activity/service it self.

这样做,你模块化使得它的一个更容易处理比做的一切都是在一个类中的方法。具体的流动将遵循这个

By doing this you modularize the process making its a lot easier to handle than doing it all in one class. Specifically the flow would follow this

活动/服务 - >数据库工具类 - >数据库

Activity/Service -> Database Utility Class -> Database

该活动通过实用工具类获取数据库的结果。你可以有像加,减,记录编辑在实用程序类中的所有数据库的功能。

The activity gets database results through the utility class. You could have all your database functionality like adding, subtracting, editing of records in the utility class.

具体来说,你需要寻找到SQLiteOpenHelper

Specifically you need to look into SQLiteOpenHelper

public class myDatabaseOpenHelper extends SQLiteOpenHelper {

    public myDatabaseOpenHelper(Context context) {
        super(context, (Database Name), null, (Database Version));

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        // create event database & category database
        db.execSQL([TableCreationStatementHere]);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

}

有一点要提的是先在纸上创建表,并建立了关系,你想,然后生成一份声明中这样的列名

public static String CREATE_EVENTDATABASE = "CREATE TABLE " + TABLE_EVENTS
            + " ( " + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COL_NAME + " TEXT, " 
                        + COL_DATE + " TEXT NOT NULL, " 
                        + COL_TIME + " TEXT NOT NULL, " 
                        + COL_DATETIME + " TEXT, " 
                        + COL_IMAGE + " TEXT, " 
                        + COL_NOTE + " TEXT, " 
                        + COL_REPEAT + " INTEGER, "
                + COL_REPEAT_INTERVAL + " INTEGER, " 
                        + COL_REMINDER + " INTEGER, "
                + COL_CATEGORY + " TEXT, " 
                        + COL_FLAG + " INTEGER )";

然后创建了表之后,然后就抓住一个手柄,你的数据库,像这样:

Then after you have created your table, then just grab a handle to your database like so:

myDatabaseOpenHelper helper = new myDatabaseOpenHelper(getApplicationContext());
SQLiteDatabase database = helper.getWritableDatabase()

这行会回报你一个链接到你的数据库,在这里你可以查询,删除,根据需要更新!

This line will return you a link to your database, where you can query, delete, update as necessary!