SQLite和数据库初始化 [英] SQLite and database initialization

查看:491
本文介绍了SQLite和数据库初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要第一次在Android上使用数据库,但有一件事我不知道。

I'm about to use a database on Android for the first time, but there is a thing I'm not sure to understand.

需要在应用程序中创建数据库;这意味着我需要给这个数据库的创建/初始化一些时间。这个时间可以很长,取决于我需要存储的数据量,我想知道,我们真的需要在正确的应用程序中创建数据库吗?

A database need to be created in an app ; which means I need to give some "time" to the creation/initialization of this database. This time can be quite long depending on the amount of datas I need to store, and I'm wondering, do we really need to create the database in the right app?

我的意思是,是不是更好只是创建一个虚拟应用程序,将创建和初始化我的数据库,恢复它,并把它放在我的真正的应用程序在我的数据库只是用来阅读的情况下?

I mean, isn't it better to just create a dummy app which will create and init my database, recover it and drop it in my real app in the case my database is just use to read?

另一件我不明白的是,因为创建属于一个应用程序,这意味着每次我将启动应用程序,我将检查是否数据库不存在已经,对吧?

The other thing I don't understand is that because the creation belong to an app, it means that everytime I will launch the app I will have to check if the database doesnt exist already, right?

如何提供数据库?您是否将所有数据存储在外部文件中,然后将其转换并在数据库中使用它? (JSON?)

How do you feed your database? Do you store all datas in an external file then convert it and use it in the database? (JSON?)

如果是这样,在我只需要读取信息的情况下使用数据库有什么意义,我可以用基本的文本文件,例如使用XML中的值ressource或.json in raw?

If so, what's the point of using a database in the case I just need to read informations, I could do the same with a basic text file, for example with a XML in values ressource or .json in raw?

推荐答案

使用SQLite数据库相对简单,不需要担心。

Working with SQLite database is relatively simple and you don't need to worry much about it.

一般关于简单应用程式的SQLite:

Generally about the SQLite for simple apps:


  1. 一个负责管理数据库的单独类。这个类扩展了SqliteOpenHelper,这意味着您将需要重写一些负责创建表的默认方法。但是这个方法是由系统调用的,不是你,所以没有必要担心他们。

  1. There is a separate class that is responsible for managing your database. This class extends SqliteOpenHelper which means that you will need to override some default methods which are responsible for creation of tables. But this methods are called by system, not you, so no need to worry about them. You configure them once, so that system know what kind of columns you need in your table and thats all.

您需要向您的表中添加自己的方法将从现有数据库添加/删除/更新/检索数据的同一类。

You will need to add your own methods to the same class that will add/delete/update/retrieve data from existing db. These are the main methods that will be used by you.

根据上面的说法,您可以看到不需要检查数据库是否存在。系统为您处理它。您只能使用更改数据集的方法。当然,你可以添加任何你想要的方法来检查你的数据库中的很多东西,并使你的工作复杂化。

Based on what was told above you can see that you don't need to check if the database is existing or not. System handles it for you. You only work with methods that change the dataset. Of course, you can add as many methods as you want that will check many things in your Database and complicate your work.

答案1:

我在我的应用程序中实现了SQLite,运行速度非常快。当然,如果数据太大,你可能需要更长的时间来处理数据。在这种情况下,您需要在后台运行与SQLite数据库相关的所有进程,因此您的用户界面不会冻结。

I have implemented SQLite right in my apps and it runs pretty fast. Of course you may get longer time for processing your data if it is too big. In this cases you need to run all processes related to SQLite db in a background, so your user interface will not get frozen.

关于创建一个虚拟应用程序,只保留你的SQLite db,在我看来是不好的主意。没有足够的经验来告诉是否可能,但如果你处理,那么你需要等待,直到该应用程序启动,然后处理您的数据。

About creating a dummy app for just hold your SQLite db, in my opinion is not good idea. Not enough experience to tell if it is possible, but if you handle to do it, you will need to wait until that app starts up and then processes your data.

回答2:

您首先需要决定要存储的数据类型。 SQLite非常适合存储文本和数字。如果你想存储,说,图像你显然需要使用不同的方法。

You first need to decide what type of data you want to store. SQLite is excellent for storing texts and numbers. If you want to store, say, images you obviously need to use different approach.

我想你首先需要运行一些关于SQLite的教程,并熟悉它。然后你将有清楚的形象,你可以做什么与SQ​​Lite数据库和如何使用它。开始的一个好地方是: https://thenewboston.com/videos。 php?cat = 6& video = 16832

I think you first need to run through some tutorials about SQLite and get familiar with it. Then you will have clear image of what you can do with SQLite databases and how to use it. One good place to start with is here: https://thenewboston.com/videos.php?cat=6&video=16832

这篇关于SQLite和数据库初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆