0

我会开始但说没有错误,我根本不知道如何编码。我有一个包含一个表的数据库,其中有 4 列和 30 行。

这是我从资产文件夹复制数据库(它是静态的)的代码:我的 DBHandler 类:

public class DBHandler extends SQLiteOpenHelper {
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat
                                                // window
// destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME = "CookbookDB";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;

public DBHandler(Context context) {
    super(context, DB_NAME, null, 1);// 1? its Database Version
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    this.mContext = context;
}

public void createDataBase() throws IOException {
    // If database doesn't exists copy it from the assets

    boolean mDataBaseExist = checkDataBase();
    try {
        if (!mDataBaseExist) {

            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        }
        this.getReadableDatabase();
        this.close();
    } catch (SQLiteException e) {
        throw new Error("ErrorCopyingDataBase");

    }

}

// Check that the database exists here: /data/data/your
// package/databases/DaName
private boolean checkDataBase() {
    File dbFile = new File(DB_PATH + DB_NAME);
    // Log.v("dbFile", dbFile + "   "+ dbFile.exists());
    return dbFile.exists();
}

// Copy the database from assets
private void copyDataBase() throws IOException {
    InputStream mInput = mContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer)) > 0) {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

// Open the database, so we can query it
public boolean openDataBase() throws SQLException {
    String mPath = DB_PATH + DB_NAME;
    // Log.v("mPath", mPath);
    mDataBase = SQLiteDatabase.openDatabase(mPath, null,
            SQLiteDatabase.CREATE_IF_NECESSARY);
    // mDataBase = SQLiteDatabase.openDatabase(mPath, null,
    // SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return mDataBase != null;
}

@Override
public synchronized void close() {
    if (mDataBase != null)
        mDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

}

我的数据适配器类:

import java.io.IOException;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DataAdapter {
protected static final String TAG = "DataAdapter";

private final Context mContext;
private SQLiteDatabase mDb;
private DBHandler mDbHelper;

public DataAdapter(Context context) {
    this.mContext = context;
    mDbHelper = new DBHandler(mContext);
}

public DataAdapter createDatabase() throws SQLException {
    try {
        mDbHelper.createDataBase();
    } catch (IOException mIOException) {
        Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
        throw new Error("UnableToCreateDatabase");
    }
    return this;
}

public DataAdapter open() throws SQLException {
    try {
        mDbHelper.openDataBase();
        mDbHelper.close();
        mDb = mDbHelper.getReadableDatabase();
    } catch (SQLException mSQLException) {
        Log.e(TAG, "open >>" + mSQLException.toString());
        throw mSQLException;
    }
    return this;
}

public void close() {
    mDbHelper.close();
}

public Cursor getTestData() {
    try {
        String sql = "SELECT * FROM CookbookTable";

        Cursor mCur = mDb.rawQuery(sql, null);
        if (mCur != null) {
            mCur.moveToNext();
        }
        return mCur;
    } catch (SQLException mSQLException) {
        Log.e(TAG, "getTestData >>" + mSQLException.toString());

        throw mSQLException;
    }
}
}

复制数据库的实现是:

DataAdapter mDbHelper = new DataAdapter(getBaseContext());
mDbHelper.createDatabase();

我不知道如何正确使用游标查询。我什至不知道我在这两个类上的 open 方法是否合法。如何从我的数据库访问信息?我设法复制了它,但仅此而已,从这里我被卡住了。如果我只有一个复制整个列的示例(可以说用作 ListView 的项目名称),我想我可以将其背后的逻辑实现到我的整个项目中。

任何帮助将不胜感激。非常感谢。

4

1 回答 1

0

引用文档

有关演示如何在 Android 中使用 SQLite 数据库的示例应用程序,请参阅Note PadSearchable Dictionary应用程序。

本教程涵盖基础知识:将 SQLite 数据库与 ListView 一起使用

于 2012-11-30T10:12:38.173 回答