Sqlite android没有这样的表存在

时间:2010-11-15 10:03:22

标签: android sqlite

我在Android中使用sqlite数据库。我没有创建任何数据库,我正在使用已创建的数据库并且已成功打开但我正在使用

Cursor cursor= db.query(tablename,  null, null, null, null, null, null);

然后它给出了这个表不存在。请告诉我在Android中访问sqlite数据库的完整程序。

1 个答案:

答案 0 :(得分:1)

创建一个类DatabaseHelper ...

public class DBHelper extends SQLiteOpenHelper {

private static String dbPath = "ur database path";
private static String dbName = "ur database name";
private static SQLiteDatabase db;
private static DBHelper databaseHelper = null;
public final Context context;

public DBHelper(Context context) {
    super(context, dbName, null, 1);
    this.context = context;
}
public static DBHelper getInstance(Context context) {
    if (databaseHelper == null) {
        databaseHelper = new DBHelper(context);
        databaseHelper.openDataBase();

        if (db == null) {
            try {
                db = databaseHelper.getWritableDatabase();
                databaseHelper.copyDataBase();
            } 
            catch (Exception e) {
                Log.i(LOGTAG, "Error in database creation");
            }

            databaseHelper.openDataBase();
        }
    }
    return databaseHelper;
}

/** 
 * To return the database. 
 */
public SQLiteDatabase getDatabase() {
    return db;
}

/** 
 * To open the database. 
 */
private boolean openDataBase() throws SQLException {

    String path = dbPath + dbName;
    try {

        db = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READWRITE);
    } 
    catch (SQLiteException e) {
        Log.i(LOGTAG, "Error in openDataBase method");
        // TODO: handle exception
    }
    return db != null ? true : false;
}
private void copyDataBase() throws IOException {

    String outFileName = dbPath + dbName;
    OutputStream output = null;

    try {
        output = new FileOutputStream(outFileName);
    } 
    catch (IOException e) {
        Log.i(LOGTAG, "Error in copyDataBase method");
    }

    output.flush();
    output.close();

    SchemaCreator schemaCreator = new SchemaCreator(db);
    try {

        schemaCreator.createTables();

    }
    catch (SQLiteException e) {
        Log.i(LOGTAG, "Error in createTables method");
    }
}
@Override
public synchronized void close() {
    if (db != null)
        db.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

之后创建一个用于从数据库中获取详细信息的类...

public class DBFetch {

private SQLiteDatabase db = null;
private Cursor cursor = null;
DBHelper myDbHelper;

public DBFetch(Context context) {
    myDbHelper = DBHelper.getInstance(context);
    db = myDbHelper.getDatabase();
}   

private void fetchDetails() {
    String sql = "SELECT * FROM employees";
    cursor = db.rawQuery(sql, null);
        if (cursor.getCount() > 0) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                  //************do your operations*********

            }
       }
}