0

我是 Android 开发的初学者——也很抱歉我的英语不好,这不是我的母语。(:好的,所以。我正在尝试实现一个在三个选项卡之间滑动的应用程序,其中一个选项卡中有一个数据库 - 它必须显示在 ListView 中。我的代码远非正确,我正在尝试找出每个错误,只要他们自己显示。成功,因为......现在。当我运行应用程序时,似乎......没有这样的表。接下来,我的代码:

SQLDBase.java:

public class SQLDBase {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_TIMER = "time";
public static final String KEY_DISTANCE = "kms";
public static final String KEY_HR = "heart_rate";
public static final String KEY_CAL = "kcals";

private static final String DATABASE_NAME = "SESSION";
private static final int DATABASE_VERSION = 42;
private static final String DATABASE_READABLE = "rdInfos";
private static final String DATABASE_WRITEABLE = "wrtInfos";
private DBHelper mHelper;
private final Context mContext;
private SQLiteDatabase mDatabase;

public static class DBHelper extends SQLiteOpenHelper {

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



@Override
//Set up database here
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + DATABASE_READABLE + " (" +
                    //Column name     Type of variable
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                  //  KEY_NAME + " TEXT NOT NULL, " +
                    KEY_TIMER + " TEXT NOT NULL, " +
                    KEY_DISTANCE + " TEXT NOT NULL, " +
                    KEY_HR + " TEXT NOT NULL);"
    );

    db.execSQL("CREATE TABLE " + DATABASE_WRITEABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
               //     KEY_NAME + " TEXT NOT NULL, " +
                    KEY_TIMER + " TEXT NOT NULL, " +
                    KEY_DISTANCE + " TEXT NOT NULL, " +
                    KEY_HR + " TEXT NOT NULL);"
    );

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_READABLE);
    db.execSQL("DROP IF TABLE EXISTS " + DATABASE_WRITEABLE);
    onCreate(db);
}
}
public SQLDBase(Context c) {
    mContext = c;
}

public SQLDBase open() throws SQLException {
    //Set up the helper with the context
    mHelper = new DBHelper(mContext);
    //Open the database with our helper
    mDatabase = mHelper.getWritableDatabase();
    return this;
}
public long createEntry(String timer, String distance, String hr, String kcals) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_TIMER, timer);
    cv.put(KEY_DISTANCE, distance);
    cv.put(KEY_HR, hr);
    cv.put(KEY_CAL, kcals);
    return mDatabase.insert(DATABASE_WRITEABLE, null, cv);
    }
public int deleteAll(){
    return mDatabase.delete(DATABASE_NAME, null, null);
}
public Cursor queueAll(){
    String[] columns = new String[]{KEY_ROWID, KEY_TIMER, KEY_DISTANCE, KEY_HR, KEY_ROWID};
    Cursor cursor = mDatabase.query(DATABASE_NAME, columns,
            null, null, null, null, null);

    return cursor;
}
}

日记.java

public class Diary extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
private SQLDBase db;
private LoaderManager.LoaderCallbacks<Cursor> mCallbacks;
private SimpleCursorAdapter mAdapter;
SearchView mSearchView;
String mCurFilter;
private Context mContext;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab_2, container, false);
    ButterKnife.inject(this, view);
    return view;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
    setEmptyText("No entries.");
    db = new SQLDBase(getActivity());
    db.open();
    Log.i("", "Database in diary opened.");
    Cursor cursor = db.queueAll();
    mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,null,
            new String[] {SQLDBase.KEY_TIMER, SQLDBase.KEY_DISTANCE,SQLDBase.KEY_HR, SQLDBase.KEY_CAL,},
            new int[] {android.R.id.text1, android.R.id.text2}, 0);
    setListAdapter(mAdapter);

    setListShown(false);
    getLoaderManager().initLoader(0, null, this);

}
public static class MySearchView extends SearchView {
    public MySearchView(Context context) {
        super(context);
    }

    public void onActionViewCollapsed() {
        setQuery("", false);
        super.onActionViewCollapsed();
    }
}
public boolean onClose() {
    if (!TextUtils.isEmpty(mSearchView.getQuery())) {
        mSearchView.setQuery(null, true);
    }
    return true;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Insert desired behavior here.
    Log.i("FragmentComplexList", "Item clicked: " + id);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created.  This
    // sample only has one Loader, so we don't care about the ID.
    // First, pick the base URI to use depending on whether we are
    // currently filtering.
    Uri baseUri;
    if (mCurFilter != null) {
        baseUri = Uri.withAppendedPath(Uri.parse(SQLDBase.KEY_ROWID),
                Uri.encode(mCurFilter));
    } else {
        baseUri = Uri.parse(SQLDBase.KEY_TIMER + " " + SQLDBase.KEY_DISTANCE + " "     + SQLDBase.KEY_HR + " "  + SQLDBase.KEY_CAL + " ");
    }

    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    String select = "((" + SQLDBase.KEY_TIMER + " NOTNULL) AND ("
            + SQLDBase.KEY_DISTANCE + " NOTNULL) AND ("
            + SQLDBase.KEY_HR + "NOTNULL) AND ("
            + SQLDBase.KEY_CAL + " NOTNULL ))";
    return new CursorLoader(getActivity(), baseUri,
            new String [] {SQLDBase.KEY_TIMER + " " + SQLDBase.KEY_DISTANCE + " "    + SQLDBase.KEY_HR + " "  + SQLDBase.KEY_CAL + " "}, select,
            new String [] {SQLDBase.KEY_NAME}, null);
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)
    mAdapter.swapCursor(data);

    // The list should now be shown.
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

public void onLoaderReset(Loader<Cursor> loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed.  We need to make sure we are no
    // longer using it.
    mAdapter.swapCursor(null);
}
}

我的日志猫

09-06 16:52:41.960  31324-31324/apheniti.prova E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: no such table: SESSION (code 1): , while compiling: SELECT _id, time, kms, heart_rate, _id FROM SESSION
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
        at apheniti.prova.SQLDBase.queueAll(SQLDBase.java:95)
        at apheniti.prova.Diary.onViewCreated(Diary.java:46)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
        at android.view.View.measure(View.java:15848)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
        at android.view.View.measure(View.java:15848)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)

SQLDBase 的第 95 行:

        Cursor cursor = mDatabase.query(DATABASE_NAME, columns,
            null, null, null, null, null);

日记第 46 行:

        Cursor cursor = db.queueAll();

谢谢你的帮助!

4

1 回答 1

0
db.execSQL("CREATE TABLE " + DATABASE_READABLE + "
db.execSQL("CREATE TABLE " + DATABASE_WRITEABLE

您创建的数据库不是使用数据库名称 SESSION 而是使用 readable/writable[rdInfos/wrtinfos] 并搜索会话表名称。

于 2015-09-06T15:33:10.220 回答