2013-04-13 69 views
1

這是我的問題:我有一個SQLite數據庫設置爲存儲一些值。在這種情況下,它非常簡單。兩列(_id,名稱)。我知道數據正在存儲在數據庫中,我知道我用來傳遞該信息的光標也正在填充。但是,當我嘗試向ArrayList添加遊標值時,出現錯誤。這是我的問題方法的代碼:將光標值傳遞給ArrayList

public void sqltest(LDatabase LConnector){ 
    cursor = LConnector.getAllRecords(); //gets the cursor from my db 
    try{ 
    cursor.moveToFirst();} 
    catch (Exception e){ 
      log.i("Failed", "Couldn't moveToFirst"); //I get a successful moveToFirst 
    } 
    while(cursor.moveToNext()){ 
     try{ 
     getWork.add(cursor.getString(cursor.getColumnIndex("name"))); 
     } catch (Exception h){ 
     Log.i("FAILED", cursor.getString(cursor.getColumnIndex("name"))); 
    } 
    } 

我設置了最後一個日誌告訴我在那個位置的光標的值。爲數據庫輸入的值總是打印出來,所以我知道光標正在填充。任何人都知道我在這裏做錯了嗎?

編輯: logcat的顯示在此之後這兩行的活動開始時,被稱爲:

04-12 23:26:26.606: I/MOVED(9478): MOVED TO FIRST 
04-12 23:26:26.606: I/FAILED(9478): test1 

有沒有更好形容它更詳細的錯誤,這就是爲什麼我很困惑。它根本不會被存儲到AssetList中。

這是getAllRecords)的代碼(mehod:

public Cursor getAllLifts() throws SQLiteException 
{ 
    open(); 
    return database.rawQuery("SELECT * FROM contacts"+";", null); 
} 

另外,這裏是創建代碼+代碼用於插入:

創建:

String createQuery = "CREATE TABLE contacts" + 
    "(_id auto_increment integer primary key," + 
    "name text);"; 

    db.execSQL(createQuery); // execute the query 

插入: open(); //打開數據庫 嘗試{// 添加更多的後來獲得的數據,從USR插入,只是爲了測試 database.execSQL( 「INSERT INTO觸點(名稱)VALUES( 'TEST1')」); database.execSQL( 「INSERT INTO觸點(名稱)VALUES( '測試2')」);}

catch (Exception insert){ 
    Log.i("INSERT", "INSERT FAILED"); 
} 
close(); // close the database 

編輯:活動W的休息/進口

package com.jydev.llogger2; 

import java.util.ArrayList; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.content.Context; 
import android.database.Cursor; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class Create extends Activity implements OnClickListener { 

Button buttonNext; //continue button 
Button buttonBack; //back button 
EditText nameEditText; //editText w/ workout name 
EditText commentEditText; //editText w/ comments 
Intent info; //intent used to pass extras 
String wName = null; 
String wComments = null; 
ArrayList<String> getWork; 
Cursor cursor; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.create); 

    commentEditText = (EditText)findViewById(R.id.commentEditText); 
    buttonBack = (Button)findViewById(R.id.create_back); 
    buttonNext = (Button)findViewById(R.id.create_continue); 
    nameEditText = (EditText)findViewById(R.id.nameEditText); 

    LtDatabase LConnector = new LDatabase(this); 
    LConnector.insert(); 
    sqltest(LConnector); 

} 
+3

如果你不告訴我們錯誤是什麼,我們不知道你會得到什麼錯誤。根據請求將您的logcat –

+0

添加到原始q –

+0

發佈您的代碼getAllRecords()和整個logcat。如果「名稱」無效,您只能獲得例外。 –

回答

2

有一些錯誤的事情。 first moveToFirst()返回一個布爾值,所以你永遠不會得到拋出的異常。其次,while語句會在您調用moveToNext()之後跳過一行,因此跳過第一行。最後,你會得到一個異常,因爲你沒有初始化網絡。

public void sqltest(LDatabase LConnector) 
{ 
    cursor = LConnector.getAllRecords(); //gets the cursor from my db 

    if (cursor.moveToFirst()) 
    { 
     do 
     { 
      try{ 
      getWork.add(cursor.getString(cursor.getColumnIndex("name"))); 
      } catch (Exception h){ 
      Log.i("FAILED", cursor.getString(cursor.getColumnIndex("name"))); 
      } 
      while (cursor.moveToNext()); 
      } 
    } 
} 

在你getWork

聲明
ArrayList<String> getWork = new ArrayList<String>(); 
+0

工作得很漂亮!來自android新手感謝。 –

1

我查詢我使用以下

public ArrayList<T> findAllBy(String selection) { 
    final SQLiteDatabase db = HELPER.getReadableDatabase();  
    final ArrayList<T> result = new ArrayList<T>(); 
    final Cursor cursor = db.query(TABLE_NAME, SELECTED_COLS, WHERE, null, null, null, null); 

    try { 
     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      T model = CREATEOBJECT(cursor); 
      result.add(model); 
      cursor.moveToNext(); 
     } 

     return result; 
    } finally { 
     cursor.close(); 
     db.close(); 
    } 
} 

其中:

  • HELPERSQLiteOpenHelper一個實例extendes
  • TABLE_NAME與表的名稱的字符串
  • SELECTED_COLS是一個String []與列的名字,我想陣列得到
  • WHERE就像是"COL_NAME = 'value'"null
  • CREATEOBJECT一個字符串是創建一個方法T類型的對象想在我的ArrayList<T>

CREATEOBJECT

public T CREATEOBJECT(Cursor cursor) { 
    new T(
     cursor.getInt(0), cursor.getString(1) 
    ); 
} 

public class T { 
    private long id; 
    private String name;  

    T(int id, String name) { 
     this.id = id; 
     this.name = name; 
    } 
    //... 
}