· 7 years ago · Dec 24, 2018, 06:04 AM
1package com.example.cherdantsev.fifthlabfirst;
2
3import android.content.ContentProvider;
4import android.content.ContentUris;
5import android.content.ContentValues;
6import android.content.Context;
7import android.content.UriMatcher;
8import android.database.Cursor;
9import android.database.sqlite.SQLiteDatabase;
10import android.database.sqlite.SQLiteOpenHelper;
11import android.net.Uri;
12import android.support.annotation.NonNull;
13import android.support.annotation.Nullable;
14
15public class CustomProvider extends ContentProvider {
16 protected static final String DATABASE_TABLE = "mainTable";
17 protected static final String DATABASE_CREATE = "create table if not exists "
18 + DATABASE_TABLE + " ( _id integer primary key autoincrement,"
19 + "column_deed text not null);";
20 static final String DB_NAME = "mydb";
21 static final int DB_VERSION = 1;
22 static final String AUTHORITY = "urfu.edu.provider.todolist";
23 static final String ACTIVITY_PATH = "activitylist";
24 public static final Uri ACTIVITY_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + ACTIVITY_PATH);
25 static final int URI_ACTIVITIES = 1;
26 static final int URI_ACTIVITIES_ID = 2;
27 SQLiteOpenHelper dbHelper;
28 SQLiteDatabase db;
29
30 private static final UriMatcher uriMatcher;
31 static {
32 uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
33 uriMatcher.addURI(AUTHORITY, ACTIVITY_PATH, URI_ACTIVITIES );
34 uriMatcher.addURI(AUTHORITY,ACTIVITY_PATH,URI_ACTIVITIES_ID);
35 }
36
37 @Override
38 public boolean onCreate() {
39 dbHelper = new SQLiteOpenHelper(getContext());
40 return true;
41 }
42
43 @Override
44 public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
45 db = dbHelper.getWritableDatabase();
46 Cursor userCursor = db.rawQuery("select * from "+ DATABASE_TABLE, null);
47 userCursor.setNotificationUri(getContext().getContentResolver(), ACTIVITY_CONTENT_URI);
48 return userCursor;
49 }
50
51 @Nullable
52 @Override
53 public String getType(@NonNull Uri uri) {
54 return null;
55 }
56
57 @Nullable
58 @Override
59 public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
60 return null;
61 }
62
63 @Override
64 public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
65 if (uriMatcher.match(uri) == URI_ACTIVITIES)
66 throw new IllegalArgumentException("Wrong URI: " + uri);
67 db = dbHelper.getWritableDatabase();
68 long rID = db.insert(DATABASE_TABLE, null,contentValues);
69 Uri resultUri = ContentUris.withAppendedId(ACTIVITY_CONTENT_URI,rID);
70 getContext().getContentResolver().notifyChange(resultUri, null);
71 return resultUri;
72 }
73
74 @Override
75 public int delete(@NonNull Uri uri, String s, @Nullable String[] strings) {
76 if (uriMatcher.match(uri) == URI_ACTIVITIES_ID)
77 throw new IllegalArgumentException("Wrong URI: " + uri);
78 String id = uri.getLastPathSegment();
79 String selection = ("_id = "+ id);
80 db = dbHelper.getWritableDatabase();
81 int cnt = db.delete(DATABASE_TABLE, selection, null);
82 getContext().getContentResolver().notifyChange(uri, null);
83 return cnt;
84 }
85
86 @Override
87 public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
88 return 0;
89 }
90
91 public void Executer(){
92 dbHelper.close();
93 db.close();
94 }
95}
96
97public class FeedReaderDbHelper extends SQLiteOpenHelper {
98 // If you change the database schema, you must increment the database version.
99 public static final int DATABASE_VERSION = 1;
100 public static final String DATABASE_NAME = "FeedReader.db";
101 private static final String SQL_CREATE_ENTRIES =
102 "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
103 FeedEntry._ID + " INTEGER PRIMARY KEY," +
104 FeedEntry.COLUMN_NAME_TITLE + " TEXT," +
105 FeedEntry.COLUMN_NAME_SUBTITLE + " TEXT)";
106
107 private static final String SQL_DELETE_ENTRIES =
108 "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
109
110 public FeedReaderDbHelper(Context context) {
111 super(context, DATABASE_NAME, null, DATABASE_VERSION);
112 }
113 public void onCreate(SQLiteDatabase db) {
114 db.execSQL(SQL_CREATE_ENTRIES);
115 }
116 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
117 // This database is only a cache for online data, so its upgrade policy is
118 // to simply to discard the data and start over
119 db.execSQL(SQL_DELETE_ENTRIES);
120 onCreate(db);
121 }
122 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
123 onUpgrade(db, oldVersion, newVersion);
124 }
125}