Android SQLite 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12015731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:29:20  来源:igfitidea点击:

Android SQLite Example

androiddatabasesqlitesqliteopenhelper

提问by Rushabh Chheda

I am new to Android and I have to create an application where I need to use a SQLite database. I am not so well equipped with the SQLite.

我是 Android 新手,我必须创建一个需要使用 SQLite 数据库的应用程序。我对 SQLite 的装备不是很好。

Could you tell me what the purpose and use of the SQLiteOpenHelperclass, and provide a simple database creation and insertion example?

你能告诉我这个类的目的和用途是什么SQLiteOpenHelper,并提供一个简单的数据库创建和插入示例吗?

回答by Vinay

Sqlite helper class helps us to manage database creation and version management. SQLiteOpenHelper takes care of all database management activities. To use it,
1.Override onCreate(), onUpgrade()methods of SQLiteOpenHelper. Optionally override onOpen() method.
2.Use this subclass to create either a readable or writable database and use the SQLiteDatabase's four API methods insert(), execSQL(), update(), delete()to create, read, update and delete rows of your table.

Sqlite helper 类帮助我们管理数据库创建和版本管理。SQLiteOpenHelper 负责所有数据库管理活动。要使用它,
1.Override 的onCreate(), onUpgrade()方法SQLiteOpenHelper。可选择覆盖 onOpen() 方法。
2.使用该子类创建可读或可写的数据库,并使用SQLiteDatabase 的四个API 方法insert(), execSQL(), update(), delete()来创建、读取、更新和删除表的行。

Example to create a MyEmployees table and to select and insert records:

创建 MyEmployees 表并选择和插入记录的示例:

public class MyDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "DBName";

    private static final int DATABASE_VERSION = 2;

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table MyEmployees
                                 ( _id integer primary key,name text not null);";

    public MyDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Method is called during creation of the database
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    // Method is called during an upgrade of the database,
    @Override
    public void onUpgrade(SQLiteDatabase database,int oldVersion,int newVersion){
        Log.w(MyDatabaseHelper.class.getName(),
                         "Upgrading database from version " + oldVersion + " to "
                         + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS MyEmployees");
        onCreate(database);
    }
}

Now you can use this class as below,

现在你可以使用这个类,如下所示,

public class MyDB{  

    private MyDatabaseHelper dbHelper;  

    private SQLiteDatabase database;  

    public final static String EMP_TABLE="MyEmployees"; // name of table 

    public final static String EMP_ID="_id"; // id value for employee
    public final static String EMP_NAME="name";  // name of employee

    /** 
     * 
     * @param context 
     */  
    public MyDB(Context context){  
        dbHelper = new MyDatabaseHelper(context);  
        database = dbHelper.getWritableDatabase();  
    }


    public long createRecords(String id, String name){  
        ContentValues values = new ContentValues();  
        values.put(EMP_ID, id);  
        values.put(EMP_NAME, name);  
        return database.insert(EMP_TABLE, null, values);  
    }    

    public Cursor selectRecords() {
        String[] cols = new String[] {EMP_ID, EMP_NAME};  
        Cursor mCursor = database.query(true, EMP_TABLE,cols,null  
            , null, null, null, null, null);  
        if (mCursor != null) {  
            mCursor.moveToFirst();  
        }  
        return mCursor; // iterate to get each value.
    }
}

Now you can use MyDB class in you activity to have all the database operations. The create records will help you to insert the values similarly you can have your own functions for update and delete.

现在您可以在您的活动中使用 MyDB 类来进行所有数据库操作。创建记录将帮助您插入值,类似地,您可以拥有自己的更新和删除功能。

回答by Aerrow

The following Links my help you

下面的链接我帮你

1. Android Sqlite Database

1.安卓Sqlite数据库

2. Tutorial 1

2. 教程 1

Database Helper Class:

数据库助手类:

A helper class to manage database creation and version management.

用于管理数据库创建和版本管理的助手类。

You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int)and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.

您创建一个实现和可选的子类onCreate(SQLiteDatabase),该类负责打开数据库(如果存在),如果不存在则创建它,并根据需要对其进行升级。事务用于确保数据库始终处于合理状态。onUpgrade(SQLiteDatabase, int, int)onOpen(SQLiteDatabase)

This class makes it easy for ContentProviderimplementations to defer opening and upgrading the database until first use, to avoid blocking application startup with long-running database upgrades.

此类使实现可以轻松地ContentProvider将打开和升级数据库推迟到第一次使用,以避免因长时间运行的数据库升级而阻塞应用程序启动。

You need more refer this link Sqlite Helper

您需要更多参考此链接Sqlite Helper

回答by wosimosi

Using Helper class you can access SQLite Database and can perform the various operations on it by overriding the onCreate() and onUpgrade() methods.

使用 Helper 类,您可以访问 SQLite 数据库,并可以通过覆盖 onCreate() 和 onUpgrade() 方法对其执行各种操作。

http://technologyguid.com/android-sqlite-database-app-example/

http://technologyguid.com/android-sqlite-database-app-example/

回答by CBredlow

The DBHelper class is what handles the opening and closing of sqlite databases as well sa creation and updating, and a decent article on how it all works is here. When I started android it was very useful (however I've been objective-c lately, and forgotten most of it to be any use.

DBHelper 类用于处理 sqlite 数据库的打开和关闭以及 sa 创建和更新,关于它如何工作的一篇不错的文章在这里。当我开始使用 android 时,它非常有用(但是我最近一直在使用 Objective-c,并且忘记了它的大部分用途。