Android:Sqlite database operation Demo
import java.util.List;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;public class ActivityCacheHelper extends SQLiteOpenHelper implements ActivityCacheInterface { public static final String DB_NAME="Activity_Info"; public static final String TABLE_NAME_STRING="ActivityInfo"; public static final String OBJECT_STRING="ObjectString"; public static final String OBJECT_ID="ObjectId"; public static final String OBJECR_RANK="ObjectRank"; public ActivityCacheHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists " +TABLE_NAME_STRING+"(" +OBJECT_ID+" INTEGER UNIQUE," +OBJECT_STRING+" TEXT," +OBJECR_RANK+" INTEGER PRIMARY KEY AUTOINCREMENT)" ); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { } @Override public List getActivityInCache() { List dataFeedItems=new ArrayList(); SQLiteDatabase db =getWritableDatabase(); //Log.d("ActivityCacheHelper", str); Cursor cursor=db.query (TABLE_NAME_STRING, null, null, null, null, null, OBJECR_RANK, "50"); //Cursor cursor=db.rawQuery(str, null); while (cursor!=null&&cursor.moveToNext()) { String objectString=cursor.getString(1); //Log.d("ActivityCacheHelper","ObjectString:"+objectString); try { LiveFeedItem liveFeedItem=Utils.createFromJson(objectString, LiveFeedItem.class); dataFeedItems.add(liveFeedItem); } catch (Exception e) { e.printStackTrace(); } } Log.d("ActivityCacheHelper", "LocalActivitySize:"+dataFeedItems.size()); cursor.close(); db.close(); return dataFeedItems; } @Override public void saveActivityitem(LiveFeedItem liveFeedItem) { SQLiteDatabase db =getWritableDatabase(); ContentValues values=new ContentValues(); String objectString=Utils.toJsonString(liveFeedItem); values.put(OBJECT_ID,objectString.hashCode()); values.put(OBJECT_STRING, objectString); db.insertWithOnConflict(TABLE_NAME_STRING, null, values, SQLiteDatabase.CONFLICT_REPLACE); db.close(); } @Override public void ShowAllDataInDatabase() { SQLiteDatabase db =getWritableDatabase(); String str="Select * from "+TABLE_NAME_STRING; Cursor cursor=db.rawQuery(str, null); Log.d("DatabaseInActivity","============="); while (cursor!=null&&cursor.moveToNext()) { Log.d("DatabaseInActivity", cursor.getInt(0)+"|"+cursor.getString(1).substring(0, 10)+"|"+cursor.getInt(2)); } Log.d("DatabaseInActivity","============="); cursor.close(); db.close(); }}
You must Sign up as a member of Effecthub to view the content.
ChOw
2013-11-07
A PHP Error was encountered
Severity: Notice
Message: Undefined index: HTTP_ACCEPT_LANGUAGE
Filename: helpers/time_helper.php
Line Number: 22
Latest Posts
- Android: CookieManager removeAllCookie() Crash
- XlistView: A listview which can pull to refresh and load more (1)
- Millions of Android Phones Could Be Affected by the Heartbleed Bug. Check to See if Yours Is One of Them
- TOP 10 ANDROID GAMES FOR MARCH 2014
- Android:Button with different textcolor and background in different states
1869 views 0 comments
You must Sign up as a member of Effecthub to join the conversation.