Guest User

Untitled

a guest
Aug 15th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.52 KB | None | 0 0
  1. public final class customerContract {
  2.  
  3. public static final String CONTENT_AUTHORITY = "com.example.dell.nammabakery";
  4.  
  5. public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
  6.  
  7. public static final String PATH_DETAILS = "details";
  8.  
  9. private customerContract() {
  10.  
  11. }
  12.  
  13. public static final class customerDataEntry implements BaseColumns {
  14.  
  15. public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_DETAILS);
  16.  
  17. public static final String CONTENT_LIST_TYPE =
  18. ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_DETAILS;
  19.  
  20. public static final String CONTENT_ITEM_TYPE =
  21. ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_DETAILS;
  22.  
  23. /**
  24. * Name of database table for customer registry
  25. */
  26. public final static String TABLE_NAME = "customerRegistry";
  27.  
  28. /**
  29. * Unique ID number for the item (only for use in the database table).
  30. * <p>
  31. * Type: INTEGER
  32. */
  33.  
  34. public final static String _ID = BaseColumns._ID;
  35.  
  36. public final static String COLUMN_CUSTOMER_NAME = "name";
  37.  
  38. public final static String COLUMN_CUSTOMER_USERNAME = "username";
  39.  
  40. public final static String COLUMN_CUSTOMER_PASSWORD = "password";
  41.  
  42. public final static String COLUMN_CUSTOMER_EMAIL = "email";
  43.  
  44. public final static String COLUMN_CUSTOMER_ADDRESS = "address";
  45.  
  46. public final static String COLUMN_PHONE_NUMBER = "phoneNumber";
  47.  
  48. }
  49. }
  50.  
  51. public class customerDbHelper extends SQLiteOpenHelper {
  52.  
  53. public static final String LOG_TAG = customerDbHelper.class.getSimpleName();
  54.  
  55. /**
  56. * Name of the database file
  57. */
  58. private static final String DATABASE_NAME = "customerRegistory.db";
  59.  
  60. /**
  61. * Database version. If you change the database schema, you must increment the database version.
  62. */
  63. private static final int DATABASE_VERSION = 1;
  64.  
  65. public customerDbHelper(Context context) {
  66. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  67. }
  68.  
  69. @Override
  70. public void onCreate(SQLiteDatabase db) {
  71. // Create a String that contains the SQL statement to create the pets table
  72. String SQL_CREATE_ITEMS_TABLE = "CREATE TABLE " + customerDataEntry.TABLE_NAME + " ("
  73. + customerDataEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  74. + customerDataEntry.COLUMN_CUSTOMER_NAME + " TEXT NOT NULL, "
  75. + customerDataEntry.COLUMN_CUSTOMER_USERNAME + "TEXT NOT NULL,"
  76. + customerDataEntry.COLUMN_CUSTOMER_PASSWORD + "TEXT NOT NULL,"
  77. + customerDataEntry.COLUMN_CUSTOMER_EMAIL + " TEXT NOT NULL, "
  78. + customerDataEntry.COLUMN_CUSTOMER_ADDRESS + " TEXT NOT NULL, "
  79. + customerDataEntry.COLUMN_PHONE_NUMBER + " TEXT NOT NULL);";
  80.  
  81. // Execute the SQL statement
  82. db.execSQL(SQL_CREATE_ITEMS_TABLE);
  83. }
  84.  
  85. /**
  86. * This is called when the database needs to be upgraded.
  87. */
  88. @Override
  89. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  90. // The database is still at version 1, so there's nothing to do be done here.
  91. }
  92. }
  93.  
  94. public class customerProvider extends ContentProvider {
  95.  
  96. public static final String LOG_TAG = customerProvider.class.getSimpleName();
  97.  
  98. /**
  99. * URI matcher code for the content URI for the customerRegistry table
  100. */
  101. private static final int CUSTOMERS = 100;
  102.  
  103. /**
  104. * URI matcher code for the content URI for a single customer in the registry table
  105. */
  106. private static final int CUSTOMER_ID = 101;
  107.  
  108. private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  109.  
  110. static {
  111.  
  112. sUriMatcher.addURI(customerContract.CONTENT_AUTHORITY, customerContract.PATH_DETAILS, CUSTOMERS);
  113.  
  114. sUriMatcher.addURI(customerContract.CONTENT_AUTHORITY, customerContract.PATH_DETAILS + "/#", CUSTOMER_ID);
  115. }
  116.  
  117. /**
  118. * Database helper object
  119. */
  120. private customerDbHelper mDbHelper;
  121.  
  122. @Override
  123. public boolean onCreate() {
  124. mDbHelper = new customerDbHelper(getContext());
  125. return true;
  126. }
  127.  
  128. @Override
  129. public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
  130. String sortOrder) {
  131. // Get readable database
  132. SQLiteDatabase database = mDbHelper.getReadableDatabase();
  133.  
  134. // This cursor will hold the result of the query
  135. Cursor cursor;
  136.  
  137. // Figure out if the URI matcher can match the URI to a specific code
  138. int match = sUriMatcher.match(uri);
  139. switch (match) {
  140. case CUSTOMERS:
  141.  
  142. cursor = database.query(customerDataEntry.TABLE_NAME, projection, selection, selectionArgs,
  143. null, null, sortOrder);
  144. break;
  145. case CUSTOMER_ID:
  146.  
  147. selection = customerDataEntry._ID + "=?";
  148. selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
  149.  
  150. cursor = database.query(customerDataEntry.TABLE_NAME, projection, selection, selectionArgs,
  151. null, null, sortOrder);
  152. break;
  153. default:
  154. throw new IllegalArgumentException("Cannot query unknown URI " + uri);
  155. }
  156.  
  157. cursor.setNotificationUri(getContext().getContentResolver(), uri);
  158. // Return the cursor
  159. return cursor;
  160. }
  161.  
  162. @Override
  163. public Uri insert(Uri uri, ContentValues contentValues) {
  164. final int match = sUriMatcher.match(uri);
  165. switch (match) {
  166.  
  167. case CUSTOMERS:
  168. return insertItem(uri, contentValues);
  169.  
  170. default:
  171. throw new IllegalArgumentException("Insertion is not supported for " + uri);
  172. }
  173. }
  174.  
  175. private Uri insertItem(Uri uri, ContentValues values) {
  176.  
  177. // Check that the name is not null
  178. String name = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_NAME);
  179. if (name == null) {
  180. throw new IllegalArgumentException("Customer requires a name");
  181. }
  182.  
  183. String username = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_USERNAME);
  184. if(username == null) {
  185. throw new IllegalArgumentException("Customer requires a username");
  186. }
  187.  
  188. String password = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_PASSWORD);
  189. if(password == null) {
  190. throw new IllegalArgumentException("password is required");
  191. }
  192.  
  193. String email = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_EMAIL);
  194. if (email == null) {
  195. throw new IllegalArgumentException("Customer requires an emailID");
  196. }
  197.  
  198. // Check that the gender is valid
  199. String address = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_ADDRESS);
  200. if (address == null) {
  201. throw new IllegalArgumentException("Customer requires valid address");
  202. }
  203.  
  204. // If the weight is provided, check that it's greater than or equal to 0 kg
  205. int phoneNumber = values.getAsInteger(customerDataEntry.COLUMN_PHONE_NUMBER);
  206. if (String.valueOf(phoneNumber).length() != 10) {
  207. throw new IllegalArgumentException("Customer requires a valid phone number");
  208. }
  209.  
  210. // Get writeable database
  211. SQLiteDatabase database = mDbHelper.getWritableDatabase();
  212.  
  213. // Insert the new pet with the given values
  214. long id = database.insert(customerDataEntry.TABLE_NAME, null, values);
  215. // If the ID is -1, then the insertion failed. Log an error and return null.
  216. if (id == -1) {
  217.  
  218. Log.e(LOG_TAG, "Failed to insert row for " + uri);
  219. return null;
  220. }
  221.  
  222. // Notify all listeners that the data has changed for the pet content URI
  223. getContext().getContentResolver().notifyChange(uri, null);
  224.  
  225. // Return the new URI with the ID (of the newly inserted row) appended at the end
  226. return ContentUris.withAppendedId(uri, id);
  227. }
  228.  
  229. @Override
  230. public int update(Uri uri, ContentValues contentValues, String selection,
  231. String[] selectionArgs) {
  232. final int match = sUriMatcher.match(uri);
  233. switch (match) {
  234. case CUSTOMERS:
  235. return updateItem(uri, contentValues, selection, selectionArgs);
  236. case CUSTOMER_ID:
  237. // For the PET_ID code, extract out the ID from the URI,
  238. // so we know which row to update. Selection will be "_id=?" and selection
  239. // arguments will be a String array containing the actual ID.
  240. selection = customerDataEntry._ID + "=?";
  241. selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
  242. return updateItem(uri, contentValues, selection, selectionArgs);
  243. default:
  244. throw new IllegalArgumentException("Update is not supported for " + uri);
  245. }
  246. }
  247. private int updateItem(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  248. // If the {@link ItemEntry#COLUMN_ITEM_NAME} key is present,
  249. // check that the name value is not null.
  250. if (values.containsKey(customerDataEntry.COLUMN_CUSTOMER_NAME)) {
  251. String name = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_NAME);
  252. if (name == null) {
  253. throw new IllegalArgumentException("Customer requires a name");
  254. }
  255. }
  256.  
  257. if(values.containsKey(customerDataEntry.COLUMN_CUSTOMER_USERNAME)) {
  258. String username = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_USERNAME);
  259. if (username == null) {
  260. throw new IllegalArgumentException("Customer requires a username");
  261. }
  262. }
  263.  
  264. if(values.containsKey(customerDataEntry.COLUMN_CUSTOMER_PASSWORD)) {
  265. String password = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_USERNAME);
  266. if(password == null) {
  267. throw new IllegalArgumentException("Password is required");
  268. }
  269. }
  270.  
  271. if (values.containsKey(customerDataEntry.COLUMN_CUSTOMER_EMAIL)) {
  272. String email = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_EMAIL);
  273. if (email == null) {
  274. throw new IllegalArgumentException("Customer requires a valid email");
  275. }
  276. }
  277.  
  278. // check that the gender value is valid.
  279. if (values.containsKey(customerDataEntry.COLUMN_CUSTOMER_ADDRESS)) {
  280. String address = values.getAsString(customerDataEntry.COLUMN_CUSTOMER_ADDRESS);
  281. if (address == null) {
  282. throw new IllegalArgumentException("Customer requires valid address");
  283. }
  284. }
  285.  
  286. // check that the weight value is valid.
  287. if (values.containsKey(customerDataEntry.COLUMN_PHONE_NUMBER)) {
  288. // Check that the weight is greater than or equal to 0 kg
  289. Integer phoneNumber = values.getAsInteger(customerDataEntry.COLUMN_PHONE_NUMBER);
  290. if (phoneNumber == null || String.valueOf(phoneNumber).length() < 10) {
  291. throw new IllegalArgumentException("Customer requires a valid phone number");
  292. }
  293. }
  294.  
  295.  
  296. // If there are no values to update, then don't try to update the database
  297. if (values.size() == 0) {
  298. return 0;
  299. }
  300.  
  301. // Otherwise, get writeable database to update the data
  302. SQLiteDatabase database = mDbHelper.getWritableDatabase();
  303.  
  304. // Perform the update on the database and get the number of rows affected
  305. int rowsUpdated = database.update(customerDataEntry.TABLE_NAME, values, selection, selectionArgs);
  306.  
  307. // If 1 or more rows were updated, then notify all listeners that the data at the
  308. // given URI has changed
  309.  
  310. if (rowsUpdated != 0) {
  311. getContext().getContentResolver().notifyChange(uri, null);
  312. }
  313.  
  314. // Return the number of rows updated
  315. return rowsUpdated;
  316. }
  317.  
  318. @Override
  319. public int delete(Uri uri, String selection, String[] selectionArgs) {
  320. // Get writeable database
  321. SQLiteDatabase database = mDbHelper.getWritableDatabase();
  322.  
  323. // Track the number of rows that were deleted
  324. int rowsDeleted;
  325.  
  326. final int match = sUriMatcher.match(uri);
  327. switch (match) {
  328. case CUSTOMERS:
  329. // Delete all rows that match the selection and selection args
  330. rowsDeleted = database.delete(customerDataEntry.TABLE_NAME, selection, selectionArgs);
  331. break;
  332. case CUSTOMER_ID:
  333. // Delete a single row given by the ID in the URI
  334. selection = customerDataEntry._ID + "=?";
  335. selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
  336. rowsDeleted = database.delete(customerDataEntry.TABLE_NAME, selection, selectionArgs);
  337. break;
  338. default:
  339. throw new IllegalArgumentException("Deletion is not supported for " + uri);
  340. }
  341.  
  342. // If 1 or more rows were deleted, then notify all listeners that the data at the
  343. // given URI has changed
  344. if (rowsDeleted != 0) {
  345. getContext().getContentResolver().notifyChange(uri, null);
  346. }
  347.  
  348. // Return the number of rows deleted
  349. return rowsDeleted;
  350. }
  351.  
  352. @Override
  353. public String getType(Uri uri) {
  354. final int match = sUriMatcher.match(uri);
  355. switch (match) {
  356. case CUSTOMERS:
  357. return customerDataEntry.CONTENT_LIST_TYPE;
  358. case CUSTOMER_ID:
  359. return customerDataEntry.CONTENT_ITEM_TYPE;
  360. default:
  361. throw new IllegalStateException("Unknown URI " + uri + " with match " + match);
  362. }
  363. }
  364. }
  365.  
  366. 08-15 17:30:46.875 31790-31790/com.example.dell.nammabakery E/SQLiteLog: (1) table customerRegistry has no column named password
  367. 08-15 17:30:46.892 31790-31790/com.example.dell.nammabakery E/SQLiteDatabase: Error inserting email=shashank address=gbbbnh name=cbngbn password=fghjmn phoneNumber=9986685826 username=ghnkk
  368. android.database.sqlite.SQLiteException: table customerRegistry has no column named password (code 1): , while compiling: INSERT INTO customerRegistry(email,address,name,password,phoneNumber,username) VALUES (?,?,?,?,?,?)
  369. at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  370. at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
  371. at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
  372. at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
  373. at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
  374. at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
  375. at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
  376. at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
  377. at com.example.dell.nammabakery.registrationData.customerProvider.insertItem(customerProvider.java:134)
  378. at com.example.dell.nammabakery.registrationData.customerProvider.insert(customerProvider.java:88)
  379. at android.content.ContentProvider$Transport.insert(ContentProvider.java:263)
  380. at android.content.ContentResolver.insert(ContentResolver.java:1229)
  381. at com.example.dell.nammabakery.RegistrationPage.saveItem(RegistrationPage.java:147)
  382. at com.example.dell.nammabakery.RegistrationPage.access$000(RegistrationPage.java:24)
  383. at com.example.dell.nammabakery.RegistrationPage$1.onClick(RegistrationPage.java:84)
Add Comment
Please, Sign In to add comment