· 9 years ago · Nov 14, 2016, 07:50 PM
1public class GameContract {
2
3 public static abstract class LevelEntry implements BaseColumns {
4
5 public static final String TABLE_NAME = "Level";
6 public static final String COLUMN_TYPE_ID = "INTEGER PRIMARY KEY";
7 public static final String COLUMN_NAME_LEVELNAME = "levelName";
8 public static final String COLUMN_TYPE_LEVELNAME = "TEXT";
9 }
10}
11
12public class MySQLiteHelper extends SQLiteOpenHelper{
13 public static final int DATABASE_VERSION = 1;
14 public static final String DATABASE_NAME = "MyGame.db";
15
16 private final String SQL_CREATE_ENTRIES = "CREATE TABLE IF NOT EXISTS " + LevelEntry.TABLE_NAME +
17 " (" + LevelEntry._ID + " " + LevelEntry.COLUMN_TYPE_ID + " autoincrement," +
18 LevelEntry.COLUMN_NAME_LEVELNAME + " " + LevelEntry.COLUMN_TYPE_LEVELNAME + ");";
19
20 private final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + LevelEntry.TABLE_NAME;
21
22 public MySQLiteHelper(Context context) {
23 // The application context to create and open the database.
24 // Used for creating cursor objects, or null for the default.
25 super(context, DATABASE_NAME, null, DATABASE_VERSION);
26 }
27
28 @Override
29 public void onCreate(SQLiteDatabase db) {
30 ....
31 }
32
33 @Override
34 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
35 ....
36 }
37
38 private void dropTables(SQLiteDatabase db) {
39 ....
40}
41
42public class MyGameProvider extends ContentProvider {
43
44 private MySQLiteHelper database;
45
46 // Used for UriMatcher
47 private static final int SCORE = 10;
48 private static final int SCORE_ID = 20;
49
50 private static final String AUTHORITY = "de.unide.sqliteexample.persistence";
51
52 private static final String BASE_PATH = GameContract.LevelEntry.TABLE_NAME;
53 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
54 + "/" + BASE_PATH);
55
56 private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
57 static {
58 sURIMatcher.addURI(AUTHORITY, BASE_PATH, SCORE); //Matches a content URI for all rows in table
59 sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", SCORE_ID);
60 }
61
62 @Override
63 public boolean onCreate() {
64 database = new MySQLiteHelper(getContext());
65 return false;
66 }
67
68 @Override
69 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
70 String sortOrder) {
71 // Uisng SQLiteQueryBuilder instead of query() method
72 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
73
74 // Set the table
75 queryBuilder.setTables(LevelEntry.TABLE_NAME);
76
77 int uriType = sURIMatcher.match(uri);
78 switch (uriType) {
79 case SCORE:
80 break;
81 case SCORE_ID:
82 // adding the ID to the original query
83 queryBuilder.appendWhere(LevelEntry._ID + "="
84 + uri.getLastPathSegment());
85 break;
86 default:
87 throw new IllegalArgumentException("Unknown URI: " + uri);
88 }
89
90 SQLiteDatabase db = database.getWritableDatabase();
91 Cursor cursor = queryBuilder.query(db, projection, selection,
92 selectionArgs, null, null, sortOrder);
93 // make sure that potential listeners are getting notified
94 cursor.setNotificationUri(getContext().getContentResolver(), uri);
95
96 return cursor;
97 }
98
99 @Override
100 public String getType(Uri uri) {
101 return null;
102 }
103
104 @Override
105 public Uri insert(Uri uri, ContentValues values) {
106 int uriType = sURIMatcher.match(uri);
107 SQLiteDatabase sqlDB = database.getWritableDatabase();
108 long id = 0;
109 switch (uriType) {
110 case SCORE:
111 id = sqlDB.insert(LevelEntry.TABLE_NAME, null, values);
112 break;
113 default:
114 throw new IllegalArgumentException("Unknown URI: " + uri);
115 }
116 getContext().getContentResolver().notifyChange(uri, null);
117 return Uri.parse(BASE_PATH + "/" + id);
118 }
119
120 @Override
121 public int delete(Uri uri, String selection, String[] selectionArgs) {
122 int uriType = sURIMatcher.match(uri);
123 SQLiteDatabase sqlDB = database.getWritableDatabase();
124 int rowsDeleted = 0;
125 switch (uriType) {
126 case SCORE:
127 rowsDeleted = sqlDB.delete(LevelEntry.TABLE_NAME, selection,
128 selectionArgs);
129 break;
130 case SCORE_ID:
131 String id = uri.getLastPathSegment();
132 if (TextUtils.isEmpty(selection)) {
133 rowsDeleted = sqlDB.delete(
134 LevelEntry.TABLE_NAME,
135 LevelEntry._ID + "=" + id,
136 null);
137 } else {
138 rowsDeleted = sqlDB.delete(
139 LevelEntry.TABLE_NAME,
140 LevelEntry._ID + "=" + id
141 + " and " + selection,
142 selectionArgs);
143 }
144 break;
145 default:
146 throw new IllegalArgumentException("Unknown URI: " + uri);
147 }
148 getContext().getContentResolver().notifyChange(uri, null);
149 return rowsDeleted;
150 }
151
152 @Override
153 public int update(Uri uri, ContentValues values, String selection,
154 String[] selectionArgs) {
155
156 int uriType = sURIMatcher.match(uri);
157 SQLiteDatabase sqlDB = database.getWritableDatabase();
158 int rowsUpdated = 0;
159 switch (uriType) {
160 case SCORE:
161 rowsUpdated = sqlDB.update(LevelEntry.TABLE_NAME,
162 values,
163 selection,
164 selectionArgs);
165 break;
166 case SCORE_ID:
167 String id = uri.getLastPathSegment();
168 if (TextUtils.isEmpty(selection)) {
169 rowsUpdated = sqlDB.update(LevelEntry.TABLE_NAME,
170 values,
171 LevelEntry._ID + "=" + id,
172 null);
173 } else {
174 rowsUpdated = sqlDB.update(LevelEntry.TABLE_NAME,
175 values,
176 LevelEntry._ID + "=" + id
177 + " and "
178 + selection,
179 selectionArgs);
180 }
181 break;
182 default:
183 throw new IllegalArgumentException("Unknown URI: " + uri);
184 }
185 getContext().getContentResolver().notifyChange(uri, null);
186 return rowsUpdated;
187 }
188
189 public Map GetMap(){
190 Map gameMap = new Map(10,10,5,5);
191
192 gameMap.SetWall(1,5);
193 gameMap.SetWall(2,5);
194 gameMap.SetWall(3,5);
195 gameMap.SetWall(4,5);
196
197 gameMap.SetExit(1,6);
198
199 gameMap.SetItem(1,4,new Item(Item.ItemType.KEY));
200
201 return gameMap;
202 }
203
204 public void LoadFromSQLFile(String path){
205
206 }
207
208 public void SaveToSQLFile(Map map){
209 }
210}