将列表存储到数据库并一起检索它们:Android

发布于 2024-10-28 19:44:59 字数 42 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2024-11-04 19:44:59

理想情况下,通过结构化方式来解决此问题,让您的主要活动/服务与实用程序类进行交互,该实用程序类直接与数据库而不是活动/服务本身进行通信。

通过这样做,您可以模块化流程,使其比在一个类中完成所有操作更容易处理。具体来说,流程将遵循此

“活动/服务”->“活动/服务”。数据库实用程序类->数据库

该活动通过实用程序类获取数据库结果。您可以在实用程序类中拥有所有数据库功能,例如添加、减去、编辑记录。

具体来说,您需要研究 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 )";

然后在创建了您的表之后表,然后只需获取数据库的句柄,如下所示:

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

此行将返回数据库的链接,您可以在其中根据需要查询、删除、更新!

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.

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) {


    }

}

One thing to mention is first create your tables on paper, and setup the relations and column names that you want and then build a statement like this:

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文