2

私のAndroidアプリには、SQLiteデータベースに製品テーブルがあり、ユーザーがインストールすると事前に入力されます。このテーブルは、AzureWebサービスから更新できます。

Webサービスから更新されたレコードのみを返し、SQLiteテーブルに更新されたレコードを入力する代わりに、2900レコードをアプリに返すだけです。これは、ほとんどの製品が変更されるためです。アプリを開くと、SQLクエリを使用して製品テーブルが削除され、データベースに挿入された製品オブジェクトにksoap2応答が送信されます。

この製品テーブルの更新が行われている間、ユーザーが中断することなくアプリを使用できるようにしたいと思います。Productsテーブルが削除されていると、アプリを正しく操作できません。

私のオプションは何ですか?一時的なProductテーブルにデータを入力し、サービスがデータの入力を終了したら、それを「ライブ」のProductテーブルにコピーできますか。または、Productテーブルを完全に削除して、一時テーブルの名前を「Product」に変更できますか?

それとも私はこれについて完全に間違っているのでしょうか。任意のアドバイスをいただければ幸いです。以下のコード抽出:

 try {

          productDatasource.open();
          productDatasource.deleteProductTable();
          productDatasource.close();
          ArrayList<Product> arrProduct = new ArrayList<Product>();
          SoapObject request = new SoapObject(NAMESPACE, PRODUCT_METHOD_NAME); 

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet = true;
         envelope.setOutputSoapObject(request);
         System.out.println("startit");
         HttpTransportSE ht = new HttpTransportSE(URL);
         ht.debug = true;
         ht.call(SOAP_ACTION_PRODUCT, envelope);

         SoapObject response = (SoapObject) envelope.getResponse();

         productDatasource.open();
         productDatasource.deleteProductTable();
         Product[] products = new Product[response.getPropertyCount()];

         for (int i = 0; i < products.length; i++) {

             SoapObject prodObj = (SoapObject)response.getProperty(i);
             Product product = new Product();

             product.setProductID(Integer.parseInt(prodObj.getProperty(10).toString()));
             product.setProductName(prodObj.getProperty(11).toString());
             product.setFKCategoryID(Integer.parseInt(prodObj.getProperty(5).toString()));
             product.setFKSubCategoryID(Integer.parseInt(prodObj.getProperty(13).toString()));
             product.setFKBrandID(Integer.parseInt(prodObj.getProperty(2).toString()));      

             productDatasource.createProduct(product);
         }
4

2 に答える 2

3

DBHelperクラスは、Respsitoryパターンに従います。基礎となるデータへのアクセスが「リポジトリ」クラスに含まれている場合。これにより、メインロジックが解放され、ビジネスロジックに集中できるようになり、すべてのデータ処理の責任がリポジトリにプッシュされます。アプリケーションの複雑さに応じて、リポジトリは「ファサード」として機能する場合があります。このファサードでは、呼び出しがドメインオブジェクトを担当するリポジトリに単純にプロキシされます。

以下のコードは、DBHelperの非常に単純なテンプレートを作成します

public class DBHelper 
{

    Context context;
    private SQLiteDatabase db;
    private final String DB_NAME = "MYDataBase";
    private final int DB_VERSION = 1;
    private final String TABLE_NAME = "Animal";
    private final String TABLE_ROW_ID = "id";
    private final String TABLE_ROW_ONE = "animal_name";
    private final String TABLE_ROW_TWO = "animal_bio";


    public DBHelper(Context context)
    {
        this.context = context;
        CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
        this.db = helper.getWritableDatabase();
    }

    public static byte[] getBitmapAsByteArray(Bitmap bitmap)
     {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, outputStream);       
        return outputStream.toByteArray();
    }   

    public void addRow(String name, String bio)
    {

        ContentValues values = new ContentValues();
        values.put(TABLE_ROW_ONE, name);
        values.put(TABLE_ROW_TWO, bio);


        try
        {
            db.insert(TABLE_NAME, null, values);
            Log.w("database message","Insert successfully");
        }
        catch(Exception e)
        {
            Log.e("DB ERROR", e.toString());
            //e.printStackTrace();
        }
    }
    public void deleteRow(long rowID)
    {
        try 
        {
            db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);
                Log.w("database message","delete successfully");
        }
        catch (Exception e)
        {
            Log.e("DB ERROR", e.toString());
            e.printStackTrace();
        }
    }

    public void updateRow(long rowID,String name,String bio)
    {


        ContentValues values = new ContentValues();
        values.put(TABLE_ROW_ONE, name);
        values.put(TABLE_ROW_TWO, bio);

        try 
        {
            db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);
            Log.w("database message","Update successfully");
        }
        catch (Exception e)
        {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
    }
    public int count_row()
    {
        int row=0;
            Cursor cursor;

        try
        {
                    cursor = db.query(
                    TABLE_NAME,
                    new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
                    null, null, null, null, null
            );

            cursor.moveToFirst();

            if (!cursor.isAfterLast())
            {
                do
                {
                    row++;
                }

                while (cursor.moveToNext());
            }
        }
        catch (SQLException e)
        {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }
    Log.w("row count..",""+row);
        return row;
    }



    public void getRow(long rowID)
    {
        Cursor cursor;

        try
        {
            cursor = db.query
            (
                    TABLE_NAME,
                    new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
                    TABLE_ROW_ID + "=" + rowID,
                    null, null, null, null, null
            );

            cursor.moveToFirst();

            if (!cursor.isAfterLast())
            {
                do
                {                   
                    Log.w("row ",""+cursor.getLong(0));
                    Log.w("row ",""+cursor.getString(1));
                    Log.w("row ",""+cursor.getFloat(2));

                }

                while (cursor.moveToNext());
            }
            cursor.close();
        }
        catch (SQLException e) 
        {
            Log.e("DB ERROR", e.toString());
            e.printStackTrace();
        }
    }
    public void getAllRows()
    {


        int i=0;
            Cursor cursor;

        try
        {
                    cursor = db.query(
                    TABLE_NAME,
                    new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
                    null, null, null, null, null
            );

            cursor.moveToFirst();

            if (!cursor.isAfterLast())
            {
                do
                {
                    Log.w("row "+i,""+cursor.getLong(0));
                    Log.w("row "+i,""+cursor.getString(1));
                    Log.w("row "+i,""+cursor.getFloat(2));

                    i++;
                }

                while (cursor.moveToNext());
            }
        }
        catch (SQLException e)
        {
            Log.e("DB Error", e.toString());
            e.printStackTrace();
        }

    }
    private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
    {
        public CustomSQLiteOpenHelper(Context context)
        {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            String newTableQueryString = "create table " +
                                        TABLE_NAME +
                                        " (" +
                                        TABLE_ROW_ID + " integer primary key autoincrement not null," +
                                        TABLE_ROW_ONE + " text," +
                                        TABLE_ROW_TWO + " text" +
                                        ");";
                db.execSQL(newTableQueryString);

        }


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

        }
    }
}

public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DBHelper database = new DBHelper(this);
    database.addRow("name1","bio1"); 
    database.addRow("name2","bio2");        
    database.addRow("name3","bio3"); 
    database.addRow("name4","bio4"); 
    database.addRow("name5","bio5");

    database.getAllRows();

    database.updateRow(2,"name20","bio20");

    database.getAllRows();

    database.deleteRow(2);

    database.getAllRows();
}

詳細 http://mel-tools.mit.edu/code/SimpleContentProvider/doc/edu/mit/mobile/android/content/class-use/DBHelper.html

アプリケーションのDBHelperクラスをコピーしてください。お役に立てば幸いです。

于 2012-10-11T12:20:27.827 に答える
1

ContentProviderをCursorLoaderと一緒に使用して、コンテンツの更新中にUIが流動的になるようにします。BulkInsertを使用して、行をより速く追加します。レコードを置き換えるのではなく、単に更新できる場合は、レコードを更新が必要であるとマークできる列を追加します。AndroidでSQLデータベースを使い始める方法については、http ://developer.android.com/guide/topics/data/data-storage.html#dbおよびhttp://developer.android.com/training/basics/を参照してください。 data-storage/databases.html

于 2012-10-11T12:27:16.913 に答える