SQLite不会显示结果

时间:2014-04-06 22:21:01

标签: android sqlite

我正在使用一个非常简单的DatabaseHandler-Class,因为我想用raqQueries做大部分事情。数据库已正确创建。这就是我使用的:

public class DatabaseHandler extends SQLiteOpenHelper {

private static int DATABASE_VERSION = 2;
private static String DATABASE_CREATE_TABLE = null;
private static String DATABASE_TABLE = null;

public DatabaseHandler(Context context, String dbName, String dbCreateTable, String dbTable) {

    super(context, dbName, null, DATABASE_VERSION);

    DATABASE_CREATE_TABLE = dbCreateTable;
    DATABASE_TABLE = dbTable;

}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL(DATABASE_CREATE_TABLE);
        Log.i("DB CREATED WITH: ", DATABASE_CREATE_TABLE);
    }
    catch (SQLException e) {
        Log.e("onCreate", e.getMessage());
    }
}

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

    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

    // Create tables again
    onCreate(db);

}

public SQLiteDatabase returnDB() {
    return this.getWritableDatabase();
}

}

在我的主要帖子中我做了:

// DB
        DatabaseHandler dbHandler = new DatabaseHandler(
                v.getContext(), 
                v.getContext().getResources().getString(R.string.DATABASE_NAME), 
                v.getContext().getResources().getString(R.string.DATABASE_CREATE_DEFAULT_TABLE), 
                v.getContext().getResources().getString(R.string.DATABASE_DEFAULT_TABLE));
        // DB END

       SQLiteDatabase db = dbHandler.returnDB();
       Cursor cursor = db.rawQuery("SELECT * FROM contacts;", null);
       Log.e("Cursor: ", cursor.toString());
       Log.e("Cursor count: ", String.valueOf(cursor.getCount()));

       try {
           db.rawQuery("INSERT INTO contacts (contactname, phonenumber) VALUES ('test', '123');", null); 
       }
       catch (SecurityException e) {
           Log.e("ERROR: ", e.getMessage());
       }
       Cursor cursor2 = db.rawQuery("SELECT * FROM contacts;", null);
       Log.e("Cursor: ", cursor2.toString());
       Log.e("Cursor count: ", String.valueOf(cursor2.getCount()));

    }

但我明白了:

04-07 00:19:35.707: E/Cursor:(6526): android.database.sqlite.SQLiteCursor@42ea27f8
04-07 00:19:35.707: E/Cursor count:(6526): 0
04-07 00:19:35.707: E/Cursor:(6526): android.database.sqlite.SQLiteCursor@42ea33c0
04-07 00:19:35.707: E/Cursor count:(6526): 0

作为回报,有谁知道为什么?

1 个答案:

答案 0 :(得分:1)

试试这个..

  Cursor cursor2 = db.rawQuery("SELECT * FROM contacts;", null);
      cursor2.moveToFirst();
   Log.e("Cursor: ", cursor2.toString());
   Log.e("Cursor count: ", String.valueOf(cursor2.getCount()));