Guest User

Untitled

a guest
Jan 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. // MainActivity.java
  2. package com.example.orici.dbtest;
  3. import android.app.Activity;
  4. import android.content.ContentValues;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.os.Bundle;
  7.  
  8. public class MainActivity extends Activity {
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView( R.layout.activity_main );
  14.  
  15. FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper( this );
  16. SQLiteDatabase db = mDbHelper.getWritableDatabase();
  17. ContentValues values = new ContentValues();
  18. values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, "Mi título");
  19. values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE, "Unas pruebas");
  20.  
  21. long newRowId = db.insert(
  22. FeedReaderContract.FeedEntry.TABLE_NAME, null, values
  23. );
  24. }
  25.  
  26. } //class
  27.  
  28.  
  29.  
  30. // FeedReaderContract.java
  31. package com.example.orici.dbtest;
  32. import android.provider.BaseColumns;
  33.  
  34. public final class FeedReaderContract {
  35.  
  36. private static final String TEXT_TYPE = " TEXT";
  37. private static final String COMMA_SEP = ",";
  38. protected static final String SQL_CREATE_ENTRIES =
  39. "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
  40. FeedEntry._ID + " INTEGER PRIMARY KEY," +
  41. FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
  42. FeedEntry.COLUMN_NAME_SUBTITLE + TEXT_TYPE + " )";
  43. protected static final String SQL_DELETE_ENTRIES =
  44. "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
  45.  
  46. private FeedReaderContract() {}
  47.  
  48. // Inner class that defines the table contents
  49. public static class FeedEntry implements BaseColumns {
  50. public static final String TABLE_NAME = "entry";
  51. public static final String COLUMN_NAME_TITLE = "title";
  52. public static final String COLUMN_NAME_SUBTITLE = "subtitle";
  53.  
  54. } //class
  55. } //class
  56.  
  57.  
  58.  
  59. //FeedReaderDbHelper.java
  60. package com.example.orici.dbtest;
  61. import android.content.Context;
  62. import android.database.sqlite.SQLiteDatabase;
  63. import android.database.sqlite.SQLiteOpenHelper;
  64. import static com.example.orici.dbtest.FeedReaderContract.SQL_CREATE_ENTRIES;
  65.  
  66. public class FeedReaderDbHelper extends SQLiteOpenHelper {
  67. public static final int DATABASE_VERSION = 1;
  68. public static final String DATABASE_NAME = "FeedReader.db";
  69.  
  70. public FeedReaderDbHelper(Context context) {
  71. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  72. }
  73. @Override
  74. public void onCreate(SQLiteDatabase db) {
  75. db.execSQL( FeedReaderContract.SQL_CREATE_ENTRIES );
  76. }
  77. @Override
  78. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  79. db.execSQL( FeedReaderContract.SQL_DELETE_ENTRIES );
  80. onCreate(db);
  81. }
  82. @Override
  83. public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  84. onUpgrade(db, oldVersion, newVersion);
  85. }
  86.  
  87. } //class
  88.  
  89. String dbname = "FeedReader.db";
  90. Path dbpath = ctx.getDatabasePath(dbname);
Add Comment
Please, Sign In to add comment