2012-11-19 153 views
1

IM試圖從表中的數據,然後將其添加到另一個表。我需要KEY_CATEGORYID從表1自動增量,然後我需要將其添加到表2和4.我不能 找出如何做到這一點KEY_CATEGORYID將只有一個值後createBudget方法已完成,發生在addId後完成這個我完全難住什麼做任何幫助,非常感謝。獲取和將數據添加到現有的SQLite數據庫

public SQLiteDatabase createBudget(String bname, String bpay, String bamount, 
       String bmemo) { 
      // TODO Auto-generated method stub 
      ContentValues t1 = new ContentValues(); 
      ContentValues t2 = new ContentValues(); 
      ContentValues t4 = new ContentValues(); 
      t1.put(KEY_NAME, bname); 
      t1.put(KEY_PAYDAY,bpay); 
      t2.put(KEY_AMOUNT, bamount); 
      t2.put(KEY_DATE, date); 
      t4.put(KEY_MEMO, bmemo); 
      t4.put(KEY_DATE,date); 

      ourDatabase.insert(DATABASE_TABLE1, null, t1); 
      ourDatabase.insert(DATABASE_TABLE2, null, t2); 
      ourDatabase.insert(DATABASE_TABLE4, null, t4); 
      addID(bname); 
     return ourDatabase; 
     } 
    private void addID(String bname) throws SQLException { 
       // TODO Auto-generated method stub 
       ContentValues num = new ContentValues(); 
       List<String> nums = new ArrayList<String>(); 
       int number = 0; 
       String[] columns = new String[]{KEY_CATEGORYID};   
       Cursor c = ourDatabase.query(DATABASE_TABLE1, columns,KEY_CATEGORYID + "=?", new String[] {String.valueOf(bname)}, null, null, null, null); 
       if(c!= null) 
       { 
        if (c.moveToFirst()) { 
         nums.add(c.getString(0)); 
         number = nums.get(0); 
         } 
        num.put(KEY_CATEGORYID, number); 
       } 
       ourDatabase.insert(DATABASE_TABLE2, null, num); 
       ourDatabase.insert(DATABASE_TABLE4, null, num); 
      } 
+0

http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an- Android的應用程序/ 9109728#9109728 –

+0

@YaqubAhmad感謝你的幫助 – newProgrammer

回答

0

insert方法返回自動生成的密鑰值:

long key = ourDB.insert(TABLE1, null, t1); 
t2.put(KEY_CATEGORYID, key); 
ourDB.insert(TABLE2, null, t2);