2.INTEGER:帶符號的整型,具體取決有存入數字的範圍大小。
3.REAL:浮點數字,存儲爲8-byte IEEE浮點數。
4.TEXT:字符串文本。
5.BLOB:二進制對象。
- String CREATE_NEWS = "create table weiboTb ("
- + "id integer primary key autoincrement, "
- + "title text, "
- + "content text, "
- + "publishdate integer,"
- + "commentcount integer)";
- db.execSQL(CREATE_NEWS);
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("title", "這是一條微博的標題");
- values.put("content", "這是一條微博的內容");
- values.put("publishdate", System.currentTimeMillis());
- long id = db.insert("weiboTb", null, values);
- public int delete(String table, String whereClause, String[] whereArgs)
- delete from weiboTb where commentcount=0;
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- db.delete("weiboTb", "commentcount = ?", new String[] {"0"});
- public int update(String table, ContentValues values, String whereClause, String[] whereArgs)
- 其作用相當於如下SQL語句:
-
- update weiboTb set title='heihiehiehieh' where id=2;
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("title", "xxxxxxx");
- db.update("weiboTb", values, "id = ?", new String[] {"2"});
- public Cursor rawQuery(String sql, String[] selectionArgs)
- public Cursor query(String table, String[] columns, String selection,
- String[] selectionArgs, String groupBy, String having, String orderBy)
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- Cursor cursor = db.query("weiboTb", null, null, null, null, null, null);
可以看到,將第一個表名參數指定成news,然後後面的六個參數我們都用不到,就全部指定成null。
那如果是我們想查詢news表中所有評論數大於零的新聞該怎麼寫呢?代碼如下所示:
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- Cursor cursor = db.query("weiboTb", null, "commentcount>?", new String[]{"0"}, null, null, null);
然後我們可以看到,query()方法的返回值是一個Cursor對象,所有查詢到的數據都是封裝在這個對象中的,所以我們還需要將數據逐一從Cursor對象中取出,然後設置到News實體類當中,如下所示:
- List<Weibo> WeiboList = new ArrayList<Weibo>();
- if (cursor != null && cursor.moveToFirst()) {
- do {
- int id = cursor.getInt(cursor.getColumnIndex("id"));
- String title = cursor.getString(cursor.getColumnIndex("title"));
- String content = cursor.getString(cursor.getColumnIndex("content"));
- Date publishDate = new Date(cursor.getLong(cursor.getColumnIndex("publishdate")));
- int commentCount = cursor.getInt(cursor.getColumnIndex("commentcount"));
- Weibo weibo = new Weibo();
- weibo.setId(id);
- weibo.setTitle(title);
- weibo.setContent(content);
- weibo.setPublishDate(publishDate);
- weibo.setCommentCount(commentCount);
- weiboList.add(weibo);
- } while (cursor.moveToNext());
- }
- select title,content from users where commentcount > 0 order by publishdate desc limit 10 offset 10;
- 其中limit表示本次查詢的數量 offset表示偏移量 表示從第幾條數據開始查詢
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- Cursor c = db.rawQuery("select count(1) from weiboTb", null);
- if (c != null && c.moveToFirst()) {
- int count = c.getInt(0);
- }
- c.close();
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- Cursor c = db.rawQuery("select sum(commentcount) from weiboTb", null);
- if (c != null && c.moveToFirst()) {
- int count = c.getInt(0);
- }
- c.close();
- db.execSQL("alter table comment add column publishdate integer");