Guest User

Untitled

a guest
Oct 26th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. private static final String DATABASE_NAME = "test.db";
  2. private static final int DATABASE_VERSION = 1;
  3. private final Context context;
  4. SQLiteDatabase db;
  5.  
  6. private static final String DATABASE_PATH = "/data/data/com.app.army.tab/databases/";
  7. private final String USER_TABLE = "user";
  8.  
  9.  
  10. public DatabaseHelperTwinIGE(Context context) {
  11. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  12. this.context = context;
  13. createDb();
  14. }
  15.  
  16. @Override
  17. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  18.  
  19. }
  20.  
  21. @Override
  22. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  23. }
  24.  
  25. public void createDb() {
  26. boolean dbExist = checkDbExist();
  27.  
  28. if (!dbExist) {
  29. this.getReadableDatabase();
  30. copyDatabase();
  31. }
  32. }
  33.  
  34. private boolean checkDbExist() {
  35. SQLiteDatabase sqLiteDatabase = null;
  36.  
  37. try {
  38. String path = DATABASE_PATH + DATABASE_NAME;
  39. sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
  40. } catch (Exception ex) {
  41. }
  42.  
  43.  
  44. if (sqLiteDatabase != null) {
  45. sqLiteDatabase.close();
  46. return true;
  47. }
  48. return false;
  49.  
  50. }
  51.  
  52. private void copyDatabase() {
  53. try {
  54. InputStream inputStream = context.getAssets().open(DATABASE_NAME);
  55.  
  56. String ourFileName = DATABASE_PATH + DATABASE_NAME;
  57.  
  58. OutputStream outputStream = new FileOutputStream(ourFileName);
  59.  
  60. byte[] b = new byte[1024];
  61. int length;
  62.  
  63.  
  64. while ((length = inputStream.read(b)) > 0) {
  65. outputStream.write(b, 0, length);
  66. }
  67.  
  68. outputStream.flush();
  69. outputStream.close();
  70. inputStream.close();
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75.  
  76. private SQLiteDatabase openDatabase() {
  77. String path = DATABASE_PATH + DATABASE_NAME;
  78. db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
  79. return db;
  80. }
  81.  
  82. public void close() {
  83. if (db != null) {
  84. db.close();
  85. }
  86.  
  87. }
  88.  
  89.  
  90. public String getUserWeigth(String username, String password) {
  91. String[] column = {"weight"};
  92. db = openDatabase();
  93.  
  94. String selection = "username=? and password = ?";
  95. String[] selectionArgs = {username, password};
  96.  
  97. String weight = null;
  98.  
  99. Cursor cursor = db.query(USER_TABLE, null , selection, selectionArgs, null, null, null);
  100. if (cursor.moveToFirst()) {
  101. weight = cursor.getString(2);
  102. }
  103. cursor.close();
  104. close();
  105.  
  106.  
  107. return weight;
  108. }
  109.  
  110. Button btnLogin;
  111. EditText editUsername;
  112. EditText editPassword;
  113. TextView result;
  114. DatabaseHelperTwinIGE databaseHelperTwinIGE;
  115.  
  116.  
  117. @Override
  118. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  119. Bundle savedInstanceState) {
  120. View view = inflater.inflate(R.layout.tab5twinoge, container, false);
  121.  
  122.  
  123. btnLogin = (Button)view.findViewById(R.id.btnAdd);
  124. editUsername = (EditText)view.findViewById(R.id.txtNumber1);
  125. editPassword = (EditText)view.findViewById(R.id.txtNumber2);
  126. result = (TextView)view .findViewById(R.id.txtResult);
  127.  
  128. databaseHelperTwinIGE = new DatabaseHelperTwinIGE(getActivity());
  129.  
  130. btnLogin.setOnClickListener(new View.OnClickListener() {
  131. @Override
  132. public void onClick(View v) {
  133. String weight = databaseHelperTwinIGE.getUserWeigth(editUsername.getText().toString(), editPassword.getText().toString());
  134.  
  135. if (weight != null){
  136. Toast.makeText(getActivity()," weight = " + weight,Toast.LENGTH_SHORT).show();
  137. result.setText(weight);
  138. } else {
  139. editPassword.setText(null);
  140. Toast.makeText(getActivity(),"Login failed. Invalid username or password.",Toast.LENGTH_SHORT).show();
  141. }
  142.  
  143.  
  144. }
  145. });
  146.  
  147.  
  148. return view;
  149.  
  150. }
Add Comment
Please, Sign In to add comment