在Android中使用SQLiteDataBase的rawQuery方法

时间:2012-10-17 13:05:15

标签: android database

我正在使用ContentProvider从DataBase进行转换。现在我想使用SQLiteDataBase的rawQuery()方法。我该如何使用这种方法。任何人都可以告诉我。提前致谢。我使用的代码:

public class ChatProvider extends ContentProvider {

@Override
public boolean onCreate() {
    mOpenHelper = new ChatDatabaseHelper(getContext());
    return true;
}
}


    private static class ChatDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "quytech.db";
    private static final int DATABASE_VERSION = 6;

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        if (LogConstants.LOG_DEBUG) {
            infoLog("creating new chat table");
        }

        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ChatConstants._ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + ChatConstants.DATE + " INTEGER,"
                + ChatConstants.DIRECTION + " INTEGER," + ChatConstants.JID
                + " TEXT," + ChatConstants.MESSAGE + " TEXT,"
                + ChatConstants.DELIVERY_STATUS + " INTEGER,"
                + ChatConstants.SPECIAL_SAVE + " INTEGER,"
                + ChatConstants.USERNAME + " TEXT,"
                + ChatConstants.PACKET_ID + " TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        infoLog("onUpgrade: from " + oldVersion + " to " + newVersion);
        switch (oldVersion) {
        case 3:
            db.execSQL("UPDATE " + TABLE_NAME + " SET READ=1");
        case 4:
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD "
                    + ChatConstants.PACKET_ID + " TEXT");
            break;
        // Sid (31/08/2012)
        // for saving individual messages
        // For fixing bug related to chat messages shown even after change
        // in user name.
        case 5:
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD "
                    + ChatConstants.SPECIAL_SAVE + " INTEGER,"
                    + ChatConstants.USERNAME + " TEXT");
            break;
        default:
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

例如:

private void openAndQueryDatabase() {
    try {

        DataBaseHelper newDB = new DataBaseHelper(this, "quytech.db");

        SQLiteDatabase sdb = newDB.getReadableDatabase();


        Cursor c = sdb.rawQuery("SELECT yourcolumntitle FROM yourtablename ORDER BY name ASC", null);



if (c != null ) {
    if  (c.moveToFirst()) {
        int i = 0;
        do {
            i++;
            String name = c.getString(c.getColumnIndex("yourcolumntitle"));

            results.add(yourcolumntitle);

        }while (c.moveToNext());
    } 

}       



    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    } finally {
        if (newDB != null) 

            newDB.close();
    }