0

我尝试从数据库中获取一些值,但我没有得到这样的表student错误。

我有这个 DBAdapter 类

public class DBAdapter {
    private static final String TAG = "DBAdapter";
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;
    private final Context context;

    public static final String KEY_ROWID = "_id";
    public static final int KEY_STUDENTSID = 0;
    public static final String KEY_NAME = "name";
    public static final String KEY_SURNAME = "surname";
    public static final int KEY_MIDTERM = 0;
    public static final int KEY_PROJECT = 0;
    public static final int KEY_FINAL = 0;


    private static final String DATABASE_NAME = "studentDB";
    private static final String DATABASE_TABLE = "student";
    private static final int DATABASE_VERSION = 2;

    // Constructor
    public DBAdapter(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    // To create and upgrade a database in an Android application
    // SQLiteOpenHelper subclass is usually created
    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Sends a Warn log message
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");

            // Method to execute an SQL statement directly
            db.execSQL("DROP TABLE IF EXISTS student");
            onCreate(db);
        }
    }

    // Opens the database
    public DBAdapter open() throws SQLException {
        // Create and/or open a database that will be used for reading only
        db = DBHelper.getReadableDatabase();
        return this;
    }

    // Closes the database
    public void close() {
        // Closes the database
        DBHelper.close();
    }

    public Cursor getNameSurname(String name) throws SQLException {
        if (name.toString().equals("*")) {
            return db.query(DATABASE_TABLE, new String[] { KEY_NAME,
                    KEY_SURNAME }, null, null, null, null, null);
        } else {

            return db.query(true, DATABASE_TABLE, new String[] { KEY_NAME,
                    KEY_SURNAME }, KEY_NAME + "='" + name + "'", null, null,
                    null, null, null);
        }

    }

}

我试图用这个类来获得价值。我将我的 studentDB 数据库放入资产中。该代码有什么问题?我错过了什么?我错过了什么?怎么了?怎么了?

我打开 DDMS 并找到数据库文件夹。有我的 db studentDB 并将其导出到我的桌面并打开它。没有我的桌子student为什么?

4

3 回答 3

0

您是否创建了一个名为 的表student?你必须做。

于 2013-05-24T20:37:30.530 回答
0

使用以下内容填写您的 onCreate() 方法:

@Override
public void onCreate(SQLiteDatabase db) {
    DATABASE_CREATE = ... your table creation command ...
    try {
        db.execSQL(DATABASE_CREATE);
    } catch (SQLException e) {
        // if table already exists, do nothing
    }
}
于 2013-05-24T20:42:29.700 回答
0

您在onCreate方法中缺少代码

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

缺少代码

     String CREATE_StudentTable = "CREATE TABLE student  ( "
            + " ID   INTEGER PRIMARY KEY,  Student Name TEXT)";   
     db.execSQL(CREATE_StudentTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Sends a Warn log message
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");

        // Method to execute an SQL statement directly
        db.execSQL("DROP TABLE IF EXISTS student");
        onCreate(db);
    }
}
于 2013-05-24T20:44:03.503 回答