首页 > 学院 > 开发设计 > 正文

我的数据库

2019-11-09 14:22:49
字体:
来源:转载
供稿:网友
import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class DBHomeList { PRivate static final String TABLE_NAME = "homeList"; private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME+ "( _ID INTEGER PRIMARY KEY,CONTENT_ID TEXT ,IMAGE_URI TEXT,TITLE TEXT,SHORT_DESCRipTION TEXT,CONTENT TEXT);"; public static void init(SQLiteDatabase db) { db.execSQL(TABLE_CREATE); } public static void clear() { DBManager.delete(TABLE_NAME, null, null); } public static void saveArticle(String contentId,String imageUri,String title,String shortDescription,String content){ ContentValues contentValues = new ContentValues(); contentValues.put("CONTENT_ID", contentId); contentValues.put("IMAGE_URI", imageUri); contentValues.put("TITLE", title); contentValues.put("SHORT_DESCRIPTION", shortDescription); contentValues.put("CONTENT", content); DBManager.insert(TABLE_NAME, null, contentValues); } public static void getAllArticleList(){ String sql = "SELECT _ID,CONTENT_ID,IMAGE_URI, TITLE,SHORT_DESCRIPTION,CONTENT FROM "+TABLE_NAME; Cursor cursor = DBManager.rawQuery(sql, null); String imageUri = ""; String title = ""; String shortDescription = ""; String content = ""; try { if(cursor.moveToFirst()){ int idxImageUri = cursor.getColumnIndex("IMAGE_URI") ; int idxTitle = cursor.getColumnIndex("TITLE") ; int idxShortDescription = cursor.getColumnIndex("SHORT_DESCRIPTION") ; int idxContent = cursor.getColumnIndex("CONTENT") ; do { imageUri = cursor.getString(idxImageUri); title = cursor.getString(idxTitle); shortDescription = cursor.getString(idxShortDescription); content = cursor.getString(idxContent); } while (cursor.moveToNext()); } } catch (Exception e) { } finally { cursor.close(); } } public static void getArticleById(String contentId){ String sql = "SELECT _ID,CONTENT_ID,IMAGE_URI, TITLE,SHORT_DESCRIPTION,CONTENT FROM "+TABLE_NAME+" WHERE CONTENT_ID = ?"; Cursor cursor = DBManager.rawQuery(sql, new String[]{contentId}); String imageUri = ""; String title = ""; String shortDescription = ""; String content = ""; try { if(cursor.moveToFirst()){ int idxImageUri = cursor.getColumnIndex("IMAGE_URI") ; int idxTitle = cursor.getColumnIndex("TITLE") ; int idxShortDescription = cursor.getColumnIndex("SHORT_DESCRIPTION") ; int idxContent = cursor.getColumnIndex("CONTENT") ; imageUri = cursor.getString(idxImageUri); title = cursor.getString(idxTitle); shortDescription = cursor.getString(idxShortDescription); content = cursor.getString(idxContent); } } catch (Exception e) { } finally { cursor.close(); } } public static void deleteArticleById(String contentId){ DBManager.delete(TABLE_NAME, "CONTENT_ID = ?", new String[]{contentId}); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表