2013-03-26 10 views
0

이 코드는 올바른 텍스트 뷰에 빈 문자열을 표시하지만 내 데이터베이스의 내용을 표시하려고하면 오류가 발생합니다. 내 주석 처리 된 코드에 문제가있는 사람을 볼 수 있습니까? 다음은 데이터를 텍스트 뷰로 보낼 때 도움이 필요합니다

public String getData() { 
    String data = ""; 
    String[] columns = new String[] {KEY_ROWID, KEY_EXERCISE, KEY_WEIGHT, KEY_SETS}; 


    /*Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
      int iRow = c.getColumnIndex(KEY_ROWID); 
      int iExercise = c.getColumnIndex(KEY_EXERCISE); 
      int iWeight = c.getColumnIndex(KEY_WEIGHT); 
      int iSets = c.getColumnIndex(KEY_SETS); 
      for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
       data = data + c.getString(iRow) + " " + c.getString(iExercise) + " " + c.getString(iWeight) 
    + " " + c.getString(iSets) + "\n"; 
     }*/ 
    return data; 
} 

는 필요하다면 내가 내 로그 캣 완전한 핸들러 클래스

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class TheHandler { 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_EXERCISE = "theexercise"; 
    public static final String KEY_WEIGHT = "theweight"; 
    public static final String KEY_SETS = "thesets"; 
    private static final String DATABASE_NAME = "TheHandledb"; 
    private static final String DATABASE_TABLE = "TheHandleTable"; 
    private static final int DATABASE_VERSION = 2; 
    private DbHelper ourHelper; 
    private final Context ourContext; 
    private SQLiteDatabase ourDatabase; 

    private static class DbHelper extends SQLiteOpenHelper{ 

     public DbHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 

     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_TABLE + "(" 
        + KEY_ROWID + " INTEGER PRIMARY KEY," + KEY_EXERCISE + " TEXT," 
        + KEY_WEIGHT + " TEXT" + KEY_SETS + " TEXT" + ")"; 
      db.execSQL(CREATE_CONTACTS_TABLE); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 
      db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 

       // Create tables again 
       onCreate(db); 
     } 

    } 


    public TheHandler(Context c) { 
     ourContext = c; 
    } 
    public void open() { 
     ourHelper = new DbHelper(ourContext); 
     ourDatabase = ourHelper.getWritableDatabase(); 
     // TODO Auto-generated method stub 

    } 

    public long createEntry(String theExercise, String theWeight, String theSets) { 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_EXERCISE, theExercise); 
     cv.put(KEY_WEIGHT, theWeight); 
     cv.put(KEY_SETS, theSets); 
     return ourDatabase.insert(DATABASE_TABLE, null,cv); 
    } 

    public void close() { 
     ourHelper.close(); 

    } 
    public String getData() { 
     String data = ""; 
     String[] columns = new String[] {KEY_ROWID, KEY_EXERCISE, KEY_WEIGHT, KEY_SETS}; 


     /*Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
     int iRow = c.getColumnIndex(KEY_ROWID); 
     int iExercise = c.getColumnIndex(KEY_EXERCISE); 
     int iWeight = c.getColumnIndex(KEY_WEIGHT); 
     int iSets = c.getColumnIndex(KEY_SETS); 
     for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
      data = data + c.getString(iRow) + " " + c.getString(iExercise) + " " + c.getString(iWeight) 
+ " " + c.getString(iSets) + "\n"; 
    }*/ 
     return data; 
    } 

} 

더 많은 코드를 제공 할 수

public class TheView extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.theview); 
     TheHandler i = new TheHandler(this); 
     i.open(); 
     String data = i.getData(); 
     TextView tv = (TextView) findViewById(R.id.tvResults); 
     tv.setText(data); 

    } 
} 

텍스트 뷰를 다루는 클래스입니다

03-26 12:19:43.988: I/Database(471): sqlite returned: error code = 1, msg = no such column: thesets 
03-26 12:19:43.998: D/AndroidRuntime(471): Shutting down VM 
03-26 12:19:43.998: W/dalvikvm(471): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
03-26 12:19:44.078: E/AndroidRuntime(471): FATAL EXCEPTION: main 
03-26 12:19:44.078: E/AndroidRuntime(471): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.anothersql/com.example.anothersql.TheView}: android.database.sqlite.SQLiteException: no such column: thesets: , while compiling: SELECT _id, theexercise, theweight, thesets FROM TheHandleTable 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.os.Looper.loop(Looper.java:123) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-26 12:19:44.078: E/AndroidRuntime(471): at java.lang.reflect.Method.invokeNative(Native Method) 
03-26 12:19:44.078: E/AndroidRuntime(471): at java.lang.reflect.Method.invoke(Method.java:521) 
03-26 12:19:44.078: E/AndroidRuntime(471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-26 12:19:44.078: E/AndroidRuntime(471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-26 12:19:44.078: E/AndroidRuntime(471): at dalvik.system.NativeStart.main(Native Method) 
03-26 12:19:44.078: E/AndroidRuntime(471): Caused by: android.database.sqlite.SQLiteException: no such column: thesets: , while compiling: SELECT _id, theexercise, theweight, thesets FROM TheHandleTable 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264) 
03-26 12:19:44.078: E/AndroidRuntime(471): at com.example.anothersql.TheHandler.getData(TheHandler.java:77) 
03-26 12:19:44.078: E/AndroidRuntime(471): at com.example.anothersql.TheView.onCreate(TheView.java:16) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-26 12:19:44.078: E/AndroidRuntime(471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
03-26 12:19:44.078: E/AndroidRuntime(471): ... 11 more 
03-26 12:19:47.530: I/Process(471): Sending signal. PID: 471 SIG: 9 
+2

어떤 오류가 발생합니까? – Blackbelt

+0

logcat – Triode

+0

코드를 TheHandler 클래스에 붙여 넣습니다. – kumar

답변

0

getData() 메소드에서 String data = ""; 이 같은 빈 문자열을 반환합니다. 반환 데이터가 빈 문자열을 반환하지 않는 것이 확실합니까?

관련 문제