0

此代码将在正确的 TextView 中显示一个空白字符串,但是当我尝试显示我的数据库的内容时,我收到一个错误,任何人都可以看到我注释掉的代码有问题吗?

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;
}

下面是处理 TextView 的类

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);

    }
}

如果需要,我可以提供更多代码完整的处理程序类

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;
    }

}

我的日志猫

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
4

1 回答 1

0

在您的方法 getData() 中,我看到您设置了 String data = ""; 并返回相同的空字符串。你确定返回数据没有返回空字符串吗?

于 2013-03-26T11:30:58.713 回答