1

我正在开发一个应用程序,它有几个模块。我将不得不多次访问许多模块中的同一个数据库。

我的问题是哪个地方是为其创建数据库和表的最佳方式和最佳位置?

像...

    1. creating the required databases and tables at once while loading app or

    2. Creating db and tables in respective modules when required??
     resulting in extra lines of codes to check if db and table already 
     exists or not..

哪一个是好的选择??还是有其他更好的方法?

任何建议将不胜感激。

提前致谢 :)

4

4 回答 4

3

首先,您应该创建一个类来创建 DB Like :

package com.DB;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.webkit.WebChromeClient.CustomViewCallback;

public class DatabaseHandler_Dashboard extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "dashboard";

    // Contacts table name
    private static final String TABLE_NAME = "tbl_dashboard";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String RANK = "RANK";
    private static final String MHBHUBCD = "MHBHUBCD";
    private static final String WK1Dkts = "WK1Dkts";
    private static final String WK1Per = "WK1Per";
    private static final String WK1OnTime = "WK1OnTime";

    private static final String WK2Dkts = "WK2Dkts";    
    private static final String Wk2Per = "Wk2Per";
    private static final String WK2OnTime = "WK2OnTime";

    private static final String QTDDkts = "QTDDkts";
    private static final String QTDPer = "QTDPer";
    private static final String QTDOnTime = "QTDOnTime";

    private static final String YTDDkts = "YTDDkts";
    private static final String YTDPer = "YTDPer";
    private static final String YTDOnTime = "YTDOnTime";



    public DatabaseHandler_Dashboard(Context context) {
        // TODO Auto-generated constructor stub
        super(context, DATABASE_NAME, null, DATABASE_VERSION);  
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        String CREATE_DEVICE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
        + RANK + " TEXT,"
        + MHBHUBCD + " TEXT,"
        + WK1Dkts + " TEXT,"
        + WK1Per + " TEXT,"
        + WK1OnTime + " TEXT,"

        + WK2Dkts + " TEXT,"
        + Wk2Per + " TEXT,"
        + WK2OnTime + " TEXT,"

        + QTDDkts + " TEXT,"
        + QTDPer + " TEXT,"
        + QTDOnTime + " TEXT,"

        + YTDDkts + " TEXT,"
        + YTDPer + " TEXT,"
        + YTDOnTime + " TEXT"
        + ")";
        db.execSQL(CREATE_DEVICE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new Device
    public void addDevice(Contact_Dashboard device) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(RANK , device.get_RANK());

        values.put(MHBHUBCD  , device.get_MHBHUBCD ()); 

        values.put(WK1Dkts  , device.get_WK1Dkts()); 
        values.put(WK1Per  , device.get_WK1Per()); 
        values.put(WK1OnTime  , device.get_WK1OnTime()); 

        values.put(WK2Dkts  , device.get_WK2Dkts()); 
        values.put(Wk2Per   , device.get_Wk2Per()); 
        values.put(WK2OnTime  , device.get_WK2OnTime()); 

        values.put(QTDDkts  , device.get_QTDDkts()); 
        values.put(QTDPer  , device.get_QTDPer()); 
        values.put(QTDOnTime  , device.get_QTDOnTime()); 

        values.put(YTDDkts   , device.get_YTDDkts()); 
        values.put(YTDPer   , device.get_YTDPer()); 
        values.put(YTDOnTime   , device.get_YTDOnTime()); 


        // Inserting Row
        db.insert(TABLE_NAME, null, values);

        db.close(); // Closing database connection
    }





    // Getting All Devices
    public List<Contact_Dashboard> getAllDevice() {
        List<Contact_Dashboard> deviceList = new ArrayList<Contact_Dashboard>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_NAME;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor !=null)
        {
            if (cursor.moveToFirst()) {
                do {
                    Contact_Dashboard device = new Contact_Dashboard();

                    device.set_RANK(cursor.getString(0));
                    device.set_MHBHUBCD(cursor.getString(1));

                    device.set_WK1Dkts(cursor.getString(2));
                    device.set_WK1Per (cursor.getString(3));
                    device.set_WK1OnTime (cursor.getString(4));


                    device.set_WK2Dkts (cursor.getString(5));
                    device.set_Wk2Per  (cursor.getString(6));
                    device.set_WK2OnTime (cursor.getString(7));

                    device.set_QTDDkts (cursor.getString(8));
                    device.set_QTDPer (cursor.getString(9));
                    device.set_QTDOnTime (cursor.getString(10));


                    device.set_YTDDkts (cursor.getString(11));
                    device.set_YTDPer (cursor.getString(12));
                    device.set_YTDOnTime (cursor.getString(13));


                    // Adding Device to list
                    deviceList.add(device);

                } while (cursor.moveToNext());

            }

    }
        cursor.close();
        return deviceList;


}





    // Getting contacts Count
    public int get_all_data() {
        String countQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }



    public int del_all_data()
    {
        SQLiteDatabase db = this.getWritableDatabase();
         return db.delete(TABLE_NAME, null, null);

    }



    // Getting All Devices
    public List<Contact_Dashboard> getAllDevice_sort_by(String vall,String sort_type)
    {
        List<Contact_Dashboard> deviceList = new ArrayList<Contact_Dashboard>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_NAME  + " order by CAST(" + vall + " as integer)" + sort_type;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor !=null)
        {
            if (cursor.moveToFirst()) {
                do {
                    Contact_Dashboard device = new Contact_Dashboard();

                    device.set_RANK(cursor.getString(0));
                    device.set_MHBHUBCD(cursor.getString(1));

                    device.set_WK1Dkts(cursor.getString(2));
                    device.set_WK1Per (cursor.getString(3));
                    device.set_WK1OnTime (cursor.getString(4));


                    device.set_WK2Dkts (cursor.getString(5));
                    device.set_Wk2Per  (cursor.getString(6));
                    device.set_WK2OnTime (cursor.getString(7));

                    device.set_QTDDkts (cursor.getString(8));
                    device.set_QTDPer (cursor.getString(9));
                    device.set_QTDOnTime (cursor.getString(10));


                    device.set_YTDDkts (cursor.getString(11));
                    device.set_YTDPer (cursor.getString(12));
                    device.set_YTDOnTime (cursor.getString(13));


                    // Adding Device to list
                    deviceList.add(device);

                } while (cursor.moveToNext());

            }

    }
        cursor.close();
        return deviceList;


}


}

现在在您的其他活动页面中创建此类的对象,例如:

DatabaseHandler_Dashboard ECDS_DB = new DatabaseHandler_Dashboard(this);

因此,当该活动运行时,它会自动生成您的数据库并检查数据库是否存在......

现在您可以为变量创建一个 get set 方法并在每个活动中使用 DB 类对象,例如:

ECDS_DB.del_all_data();

    ECDS_DB.addDevice(new Contact_Dashboard(
                        ARR_RANK[i] ,
                        ARR_MHBHUBCD[i] , 

                        ARR_WK1Dkts[i] ,
                        ARR_WK1OnTime[i] ,
                        ARR_WK1Per[i] ,

                        ARR_WK2Dkts[i] ,
                        ARR_WK2OnTime[i] ,
                        ARR_Wk2Per[i] , 

                        ARR_QTDDkts[i] , 
                        ARR_QTDOnTime[i] ,
                        ARR_QTDPer[i] ,

                        ARR_YTDDkts[i] ,
                        ARR_YTDOnTime[i] ,
                        ARR_YTDPer[i] ));

试试这个,别忘了给我评论......

于 2012-10-12T06:13:53.417 回答
2

如果您仅在您的应用程序中使用数据库,那么我认为SQLiteOpenHelper是最佳选择。它将在您第一次使用时创建表。(建议您将自己的助手设为单例类以避免并发访问问题)。

如果您在两个或多个应用程序之间共享数据库,那么您应该将数据库包装在Content Provider后面。

于 2012-10-12T06:10:25.633 回答
1

使用帮助器类进行数据库访问的最佳方法是按照以下步骤定义数据库帮助器:

  1. 创建一个扩展 SQLiteOpenHelper 的类。
  2. 覆盖 onCreate()、onUpgrade() 方法和构造函数。
  3. 在 onCreate() 中创建所有表一次,因此您以后无需担心表存在。
  4. 在 onUpgrade() 方法中,删除所有表,然后执行 onCreate() 方法。
  5. 在您的活动和服务中,无论您需要在哪里访问数据库,只需创建一个 DatabaseHelper 实例,如果它是第一次调用数据库,它将创建所有表。
于 2012-10-12T06:11:49.377 回答
1

@Jeet 的答案是完美的,但我仍然添加它以进行性能调整。如果您的应用程序没有更多。表然后您应该在任何 sqlDev 工具中创建您的数据库并将其导出到 sql 文件中。并将其放入资产文件夹。

现在,

创建一个扩展 SQLiteOpenHelper 的新类。
在 onCreate() 方法中检查设备上您的数据库是否存在。如果您的数据库不可用,则将您的数据库结构文件复制到此路径。

/data/data/YOUR_PACKAGE/databases/

通过这样做,您复制整个数据库及其大约比通过逐个查询创建所有表快 10 倍。

于 2012-10-12T07:53:24.030 回答