2011-11-16 11 views
71

मैंने एक डेटाबेस बनाया है। मैं लेनदेन करना चाहता हूँ। SaveCustomer() उस समय Customer, CustomerControl, Profile, Payment तालिका में रिकॉर्ड डालने के लिए एक से अधिक कथन शामिल हैं।एंड्रॉइड डाटाबेस लेनदेन

जब कोई उपयोगकर्ता SaveCustomer() विधि पर कॉल करता है तो वह डेटा इन 4 टेबल पर जाएगा। तो मैं लेनदेन कैसे कर सकता हूं? यदि एक टेबल आवेषण विफल हो गया है तो उसे सब कुछ वापस रोल करने की आवश्यकता है। उदाहरण के लिए, जब तीसरी तालिका रिकॉर्ड दर्ज करती है तो मुझे एक त्रुटि मिलती है, फिर पिछले दो टेबल के सम्मिलित रिकॉर्ड्स को वापस रोल करने की आवश्यकता होती है।

public void saveCustomer(){ 
    DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(RetailerOrderKeyActivity.this); 
    dbAdapter.openDataBase(); 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put("CustomerName",customer.getName()); 
    initialValues.put("Address",customer.getAddress()); 
    initialValues.put("CustomerPID",strPID); 
    initialValues.put("Date",strDateOnly); 
    long n = dbAdapter.insertRecordsInDB("Customer", null, initialValues); 

} 

इसी तरह अन्य भी वहाँ बयान:

मेरी कोड देखें।

DBAdpter कोड है:

public class DBAdapter extends SQLiteOpenHelper { 

    private static String DB_PATH = "/data/data/com.my.controller/databases/"; 
    private static final String DB_NAME = "customer"; 
    private SQLiteDatabase myDataBase; 
    private final Context myContext; 
    private static DBAdapter mDBConnection; 


    private DBAdapter(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
     DB_PATH = "/data/data/" 
       + context.getApplicationContext().getPackageName() 
       + "/databases/"; 
     // The Android's default system path of your application database is 
     // "/data/data/mypackagename/databases/" 
    } 


    public static synchronized DBAdapter getDBAdapterInstance(Context context) { 
     if (mDBConnection == null) { 
      mDBConnection = new DBAdapter(context); 
     } 
     return mDBConnection; 
    } 


    public void createDataBase() throws IOException { 
     boolean dbExist = checkDataBase(); 
     if (dbExist) { 
      // do nothing - database already exist 
     } else { 
      // By calling following method 
      // 1) an empty database will be created into the default system path of your application 
      // 2) than we overwrite that database with our database. 
      this.getReadableDatabase(); 
      try { 
       copyDataBase(); 
      } catch (IOException e) { 
       throw new Error("Error copying database"); 
      } 
     } 
    } 


    private boolean checkDataBase() { 
     SQLiteDatabase checkDB = null; 

     try { 
      String myPath = DB_PATH + DB_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY); 

     } catch (SQLiteException e) { 
      // database does't exist yet. 
     } 
     if (checkDB != null) { 
      checkDB.close(); 
     } 
     return checkDB != null ? true : false; 
    } 


    private void copyDataBase() throws IOException { 
     InputStream myInput = myContext.getAssets().open(DB_NAME); 
     String outFileName = DB_PATH + DB_NAME; 
     OutputStream myOutput = new FileOutputStream(outFileName); 
    byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer)) > 0) { 
      myOutput.write(buffer, 0, length); 
     } 
      // Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } 

    /** 
    * Open the database 
    * @throws SQLException 
    */ 
    public void openDataBase() throws SQLException { 
     String myPath = DB_PATH + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);  
    } 


    @Override 
    public synchronized void close() { 
     if (myDataBase != null) 
      myDataBase.close(); 
     super.close(); 
    } 

    /** 
    * Call on creating data base for example for creating tables at run time 
    */ 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
    } 


    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("ALTER TABLE WMPalmUploadControl ADD Testing int"); 

    } 

    public void upgradeDb(){ 
     onUpgrade(myDataBase, 1, 2); 
    } 

    public Cursor selectRecordsFromDB(String tableName, String[] tableColumns, 
      String whereClase, String whereArgs[], String groupBy, 
      String having, String orderBy) { 
     return myDataBase.query(tableName, tableColumns, whereClase, whereArgs, 
       groupBy, having, orderBy); 
    } 


    public ArrayList<ArrayList<String>> selectRecordsFromDBList(String tableName, String[] tableColumns, 
      String whereClase, String whereArgs[], String groupBy, 
      String having, String orderBy) {   

     ArrayList<ArrayList<String>> retList = new ArrayList<ArrayList<String>>(); 
      ArrayList<String> list = new ArrayList<String>(); 
      Cursor cursor = myDataBase.query(tableName, tableColumns, whereClase, whereArgs, 
        groupBy, having, orderBy);   
      if (cursor.moveToFirst()) { 
      do { 
       list = new ArrayList<String>(); 
       for(int i=0; i<cursor.getColumnCount(); i++){     
        list.add(cursor.getString(i)); 
       } 
       retList.add(list); 
      } while (cursor.moveToNext()); 
      } 
      if (cursor != null && !cursor.isClosed()) { 
      cursor.close(); 
      } 
      return retList; 

    } 


    public long insertRecordsInDB(String tableName, String nullColumnHack,ContentValues initialValues) { 
     long n =-1; 
     try { 
      myDataBase.beginTransaction(); 
      n = myDataBase.insert(tableName, nullColumnHack, initialValues); 

      myDataBase.endTransaction(); 
      myDataBase.setTransactionSuccessful(); 
     } catch (Exception e) { 
      // how to do the rollback 
      e.printStackTrace(); 
     } 

     return n; 
    } 


    public boolean updateRecordInDB(String tableName, 
      ContentValues initialValues, String whereClause, String whereArgs[]) { 
     return myDataBase.update(tableName, initialValues, whereClause, 
       whereArgs) > 0;    
    } 

    public int updateRecordsInDB(String tableName, 
      ContentValues initialValues, String whereClause, String whereArgs[]) { 
     return myDataBase.update(tableName, initialValues, whereClause, whereArgs);  
    } 


    public int deleteRecordInDB(String tableName, String whereClause, 
      String[] whereArgs) { 
     return myDataBase.delete(tableName, whereClause, whereArgs); 
    } 


    public Cursor selectRecordsFromDB(String query, String[] selectionArgs) { 
     return myDataBase.rawQuery(query, selectionArgs);  
    } 


    public ArrayList<ArrayList<String>> selectRecordsFromDBList(String query, String[] selectionArgs) {  
      ArrayList<ArrayList<String>> retList = new ArrayList<ArrayList<String>>(); 
      ArrayList<String> list = new ArrayList<String>(); 
      Cursor cursor = myDataBase.rawQuery(query, selectionArgs);    
      if (cursor.moveToFirst()) { 
      do { 
       list = new ArrayList<String>(); 
       for(int i=0; i<cursor.getColumnCount(); i++){     
        list.add(cursor.getString(i)); 
       } 
       retList.add(list); 
      } while (cursor.moveToNext()); 
      } 
      if (cursor != null && !cursor.isClosed()) { 
      cursor.close(); 
      } 
      return retList; 
     } 

} 

database lock issue in HTC Desire:

public long insertRecordsInDB(String tableName, String nullColumnHack,ContentValues initialValues) { 
    long n =-1; 
    try { 
     myDataBase.beginTransaction(); 
     n = myDataBase.insert(tableName, nullColumnHack, initialValues); 

     myDataBase.endTransaction(); 
     myDataBase.setTransactionSuccessful(); 
    } catch (Exception e) { 
     // how to do the rollback 
     e.printStackTrace(); 
    } 

    return n; 
} 

यह पूरा कोड है।

यदि तालिका डेटा डालने पर कोई समस्या आई है तो मैं वापस रोल करना चाहता हूं।

कृपया मेरी मदद करें

धन्यवाद। अपने finally में

उत्तर

24

आप जोड़ना चाहिए endTransaction, आपकी कोशिश ब्लॉक में नहीं

finally { 
    myDataBase.endTransaction(); 
    } 

परिवर्तन वापस तैयार की जाएगी यदि कोई लेन-देन बिना समाप्त हो गया है:

मैं इस एक ही संबंधित सवाल देखा साफ के रूप में चिह्नित किया जा रहा है (setTransaction असफल होने के द्वारा)। अन्यथा वे प्रतिबद्ध होंगे।

+1

जानकारी के लिए धन्यवाद। मेरे 'SaveCustomer()' में मैं 'long n = dbAdapter.insertRecordsInDB ("ग्राहक", शून्य, प्रारंभिक वैल्यू) को कॉल करने जा रहा हूं;' विभिन्न रिकॉर्ड के साथ 4 बार में। क्या यह सब कुछ रोलबैक करेगा, अगर मध्य तालिका में भी कोई इर्रू होता है। मेरे पास 4 टेबल हैं। अलग-अलग रिकॉर्ड के साथ 4 टेबल में रिकॉर्ड जोड़ने की ज़रूरत है। – Kartheepan

258

असल में आप गलत कर रहे हैं। यदि आपके पास डेटाबेस तालिका में से किसी एक में डेटा डालने में कोई समस्या है तो आपको डेटाबेस में डालने के लिए एकाधिक रिकॉर्ड होने पर आपको लेन-देन शुरू करना होगा यदि आपको अन्य तालिका से डेटा रोलबैक करना है।

उदाहरण

के लिए आपको दो तालिकाएं

  1. एक
  2. बी

अब आप इन दो टेबलों में डेटा सम्मिलित करना चाहते हैं, लेकिन आप अगर आप लेन-देन रोलबैक करना होगा टेबल में डेटा डालने के समय में कोई त्रुटि मिलेगी।

अब आपने तालिका ए में डेटा सफलतापूर्वक सम्मिलित किया है और अब आप तालिका बी में डेटा डालने का प्रयास कर रहे हैं। यदि आपको तालिका बी में डेटा डालने के समय त्रुटि मिलती है तो आपको तालिका से प्रासंगिक डेटा हटाना होगा इसका मतलब है कि आपको लेनदेन को रोलबैक करना होगा।

तुम कैसे आप लेन-देन के लिए एक विधि है शुरू करने के लिए चाहते हैं एंड्रॉयड

  1. में डेटाबेस लेनदेन का उपयोग कर सकते beginTransaction()
  2. आप लेन-देन के लिए प्रतिबद्ध चाहते हैं एक विधि setTransactionSuccessful() जो प्रतिबद्ध होगा है डेटाबेस
  3. यदि आप लेनदेन शुरू कर चुके हैं तो आपको लेनदेन को बंद करने की आवश्यकता है, इसलिए एक विधि endTransaction() है जो आपके डेटाबेस लेनदेन को समाप्त कर देगी

अब दो मुख्य बिंदु

  1. आप लेन-देन सफल आप setTransactionSuccessful() लिखने की ज़रूरत है और फिर endTransaction()beginTransaction()
  2. अपना लेन-देन रोलबैक करना चाहते हैं के बाद तो आप बिना endTransaction() करने की जरूरत है स्थापित करना चाहते हैं कर रहे हैं setTransactionSuccessful() द्वारा लेनदेन करना।

आप से here

SQLite डेटाबेस लेनदेन के बारे में विस्तृत जानकारी प्राप्त कर सकते हैं आपके मामले में

आप ट्राई एवं कैच ब्लॉक में अपने saveCustomer() समारोह कॉल कर सकते हैं

db.beginTransaction(); 
try { 
    saveCustomer(); 
    db.setTransactionSuccessful(); 
} catch { 
    //Error in between database transaction 
} finally { 
    db.endTransaction(); 
} 
+3

इस जानकारी के लिए धन्यवाद। यह मेरे लिए और अधिक सहायक है !!! धन्यवाद फिर से – Piraba

+11

आपको अपने प्रयास ब्लॉक में नहीं, अंत में 'endTransaction' जोड़ना चाहिए। – VansFannel

+6

@VansFannel आप अंततः डेटाबेस लेनदेन को बंद करने के लिए सबसे अच्छी जगह है। इसे बेहतर बनाने के लिए धन्यवाद। – Dharmendra

11

सम्मिलित लेनदेन का उपयोग कर रिकॉर्ड, यह बहुत तेज है

String sql = "INSERT INTO table (col1, col2) VALUES (?, ?)"; 
db.beginTransaction(); 

SQLiteStatement stmt = db.compileStatement(sql); 
for (int i = 0; i < values.size(); i++) { 
    stmt.bindString(1, values.get(i).col1); 
    stmt.bindString(2, values.get(i).col2); 
    stmt.execute(); 
    stmt.clearBindings(); 
} 

db.setTransactionSuccessful(); 
db.endTransaction(); 
संबंधित मुद्दे