Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MainActivity.java
- package com.example.orici.dbtest;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView( R.layout.activity_main );
- FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper( this );
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, "Mi título");
- values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE, "Unas pruebas");
- long newRowId = db.insert(
- FeedReaderContract.FeedEntry.TABLE_NAME, null, values
- );
- }
- } //class
- // FeedReaderContract.java
- package com.example.orici.dbtest;
- import android.provider.BaseColumns;
- public final class FeedReaderContract {
- private static final String TEXT_TYPE = " TEXT";
- private static final String COMMA_SEP = ",";
- protected static final String SQL_CREATE_ENTRIES =
- "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
- FeedEntry._ID + " INTEGER PRIMARY KEY," +
- FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
- FeedEntry.COLUMN_NAME_SUBTITLE + TEXT_TYPE + " )";
- protected static final String SQL_DELETE_ENTRIES =
- "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
- private FeedReaderContract() {}
- // Inner class that defines the table contents
- public static class FeedEntry implements BaseColumns {
- public static final String TABLE_NAME = "entry";
- public static final String COLUMN_NAME_TITLE = "title";
- public static final String COLUMN_NAME_SUBTITLE = "subtitle";
- } //class
- } //class
- //FeedReaderDbHelper.java
- package com.example.orici.dbtest;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import static com.example.orici.dbtest.FeedReaderContract.SQL_CREATE_ENTRIES;
- public class FeedReaderDbHelper extends SQLiteOpenHelper {
- public static final int DATABASE_VERSION = 1;
- public static final String DATABASE_NAME = "FeedReader.db";
- public FeedReaderDbHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL( FeedReaderContract.SQL_CREATE_ENTRIES );
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL( FeedReaderContract.SQL_DELETE_ENTRIES );
- onCreate(db);
- }
- @Override
- public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- onUpgrade(db, oldVersion, newVersion);
- }
- } //class
- String dbname = "FeedReader.db";
- Path dbpath = ctx.getDatabasePath(dbname);
Add Comment
Please, Sign In to add comment