Back to project page LearnByHeart.
The source code is released under:
Apache License
If you think the Android project LearnByHeart listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.mps.learn.pb.db; /*from w w w . j av a2 s. co m*/ import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import com.mps.learn.pb.db.PhraseReaderContract.PhraseEntry; import com.mps.learn.pb.model.PhraseModel; public class DataSourcePhrase { private static final String TAG = DataSourcePhrase.class .getSimpleName(); private SQLiteDatabase database; private DatabaseHelper mDbHelper; private String[] allColumns = { PhraseEntry._ID, PhraseEntry.COLUMN_NAME_TITLE, PhraseEntry.COLUMN_NAME_USAGE_PB, PhraseEntry.COLUMN_NAME_USAGE_EN, PhraseEntry.COLUMN_NAME_RES_URL, PhraseEntry.COLUMN_NAME_UPDATED }; public DataSourcePhrase(Context context) { this.mDbHelper = new DatabaseHelper(context); } public void open() throws SQLException { database = mDbHelper.getWritableDatabase(); } public void close() { mDbHelper.close(); } public PhraseModel createPhrase(String title, String usagePb, String usageEn, String resUrl ) { ContentValues values = new ContentValues(); values.put(PhraseEntry.COLUMN_NAME_TITLE, title); values.put(PhraseEntry.COLUMN_NAME_USAGE_PB, usagePb); values.put(PhraseEntry.COLUMN_NAME_USAGE_EN, usageEn); values.put(PhraseEntry.COLUMN_NAME_RES_URL, resUrl); values.put(PhraseEntry.COLUMN_NAME_UPDATED, System.currentTimeMillis()); // Insert the new row, returning the primary key value of the new row long insertRowId = database.insert( PhraseEntry.TABLE_NAME, PhraseEntry.COLUMN_NAME_NULLABLE, values); if (insertRowId == -1 ? false : true) { Cursor cursor = database.query( PhraseEntry.TABLE_NAME, allColumns, PhraseEntry._ID + " = " + insertRowId, null, null, null, null); if (cursor != null) { cursor.moveToFirst(); PhraseModel PhraseModel = cursorToPhraseModel(cursor); cursor.close(); return PhraseModel; } } return null; } public List<PhraseModel> getAllSavedPhrases() { List<PhraseModel> PhraseModels = new ArrayList<PhraseModel>(); Cursor cursor = database.query(PhraseEntry.TABLE_NAME, allColumns, null, null, null, null, PhraseEntry.COLUMN_NAME_UPDATED + " ASC"); if(cursor == null)return null; cursor.moveToFirst(); while (!cursor.isAfterLast()) { PhraseModel PhraseModel = cursorToPhraseModel(cursor); PhraseModels.add(PhraseModel); cursor.moveToNext(); } cursor.close(); return PhraseModels; } private PhraseModel cursorToPhraseModel(Cursor cursor) { PhraseModel model = new PhraseModel(); model.setId(cursor.getLong(cursor .getColumnIndexOrThrow(PhraseEntry._ID))); model.setTitle(cursor.getString(cursor .getColumnIndexOrThrow(PhraseEntry.COLUMN_NAME_TITLE))); model.setUsagePunjabi(cursor.getString(cursor .getColumnIndexOrThrow(PhraseEntry.COLUMN_NAME_USAGE_PB))); model.setUsageEnglish(cursor.getString(cursor .getColumnIndexOrThrow(PhraseEntry.COLUMN_NAME_USAGE_EN))); model.setResourceUrl(cursor.getString(cursor .getColumnIndexOrThrow(PhraseEntry.COLUMN_NAME_RES_URL))); model.setUpdated(cursor.getLong(cursor .getColumnIndexOrThrow(PhraseEntry.COLUMN_NAME_UPDATED))); return model; } public void delete(long id){ String string =String.valueOf(id); database.execSQL("DELETE FROM " + PhraseEntry.TABLE_NAME + " WHERE " + PhraseEntry._ID + "= '" + string + "'"); } }