2015-09-28 34 views
1

我写了一个rssFeedReader应用程序,并直接使用sqliteSyntax来修改数据库。 这是我的代码:如何使用ContentValues和SQLiteOpenHelper在Android中修改sqlite数据库?

database.execSQL database= openOrCreateDatabase("MyDB", MODE_PRIVATE, null); 

database.execSQL("Create TABLE IF NOT EXISTS Press (press_name 
VARCHAR, press_url VARCHAR"); 

database.execSQL("INSERT INTO Press VALUES ('Tasnim', 
'http://www.tasnimnews.com/english/rss/feed/?d=2&c=1&m=6&alt=Recent%20News');"); 

database.execSQL("UPDATE Press press_name ='Tasnim', 
press_url ='http://www.tasnimnews.com/english/rss/feed/? 
d=2&c=1&m=6&alt=Recent%20News');"); 

Cursor cursor = database.rawQuery("SELECT * FROM MyTable", null); 

database.close(); 

,现在我的问题是我怎么能迁移到SQLiteOpenHelperConentValues方法来修改数据库。

回答

0
// Gets the data repository in write mode 
SQLiteDatabase db = mDbHelper.getWritableDatabase(); 

// Create a new map of values, where column names are the keys 
ContentValues values = new ContentValues(); 
values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id); 
values.put(FeedEntry.COLUMN_NAME_TITLE, title); 
values.put(FeedEntry.COLUMN_NAME_CONTENT, content); 

// Insert the new row, returning the primary key value of the new row 
long newRowId; 
newRowId = db.insert(
     FeedEntry.TABLE_NAME, 
     FeedEntry.COLUMN_NAME_NULLABLE, 
     values); 

你可以在这里找到它http://developer.android.com/training/basics/data-storage/databases.html

+0

谢谢,我用它和它的工作的罚款。 – MajidAbdullahi

相关问题