Guest User

Untitled

a guest
Dec 7th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. public class DbHelper extends SQLiteOpenHelper {
  2. public static final String TAG = DbHelper.class.getSimpleName();
  3. public static final String DB_NAME = "myapp.db";
  4. public static final int DB_VERSION = 1;
  5.  
  6. public static final String USER_TABLE = "users";
  7. public static final String COLUMN_ID = "_id";
  8. public static final String COLUMN_usurarios = "usurarios";
  9. public static final String COLUMN_PASS = "password";
  10. public static final String COLUMN_ESTADO = "estado";
  11.  
  12. /*
  13. create table users(
  14. id integer primary key autoincrement,
  15. usurarios text,
  16. password text);
  17. */
  18. public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "("
  19. + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
  20. + COLUMN_usurarios + " TEXT,"
  21. + COLUMN_PASS + " TEXT"
  22. + COLUMN_ESTADO + " TEXT)";
  23.  
  24. public DbHelper(Context applicationContext, Object o, SQLiteDatabase.CursorFactory factory, int i) {
  25. super(applicationContext, DB_NAME,factory , DB_VERSION);
  26. }
  27.  
  28. @Override
  29. public void onCreate(SQLiteDatabase db) {
  30. db.execSQL(CREATE_TABLE_USERS);
  31. }
  32.  
  33.  
  34.  
  35.  
  36. @Override
  37. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  38. db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
  39.  
  40. onCreate(db);
  41. }
  42.  
  43. /**
  44. * Storing user details in database
  45. * */
  46. public void addUser(String usurarios, String password) {
  47. SQLiteDatabase db = this.getWritableDatabase();
  48.  
  49. ContentValues values = new ContentValues();
  50. values.put(COLUMN_usurarios, usurarios);
  51. values.put(COLUMN_PASS, password);
  52.  
  53. long id = db.insert(USER_TABLE, null, values);
  54. db.close();
  55.  
  56. Log.d(TAG, "Usuario insertado" + id);
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. public boolean getUser(String usurarios, String pass){
  64. //HashMap<String, String> user = new HashMap<String, String>();
  65. String selectQuery = "select * from " + USER_TABLE + " where " +
  66. COLUMN_usurarios + " = " + "'"+usurarios+"'" + " and " + COLUMN_PASS + " = " + "'"+pass+"'";
  67.  
  68. SQLiteDatabase db = this.getReadableDatabase();
  69. Cursor cursor = db.rawQuery(selectQuery, null);
  70. // Move to first row
  71. cursor.moveToFirst();
  72. if (cursor.getCount() > 0) {
  73.  
  74. return true;
  75. }
  76. cursor.close();
  77. db.close();
  78.  
  79. return false;
  80. }
  81.  
  82. public String eliminar(String usuarios){
  83. String mensaje="";
  84.  
  85. SQLiteDatabase db= this.getWritableDatabase();
  86.  
  87. int cantidad = db.delete(USER_TABLE ,COLUMN_usurarios+"=?",new String[]{usuarios});
  88.  
  89. if(cantidad!=0){
  90. mensaje="eliminado correctamente";
  91. }else{
  92. mensaje="No existe";
  93. }
  94. db.close();
  95. return mensaje;
  96. }
  97.  
  98.  
  99. public List<Map<String, String>> llenar_lv(){
  100. ArrayList<String> lista = new ArrayList<>();
  101. SQLiteDatabase db = this.getWritableDatabase();
  102. String q = "SELECT * FROM users";
  103. Cursor registros = db.rawQuery(q,null);
  104. registros.moveToFirst();
  105. List<Map<String, String>> data = new ArrayList<Map<String, String>>();
  106. while(registros.moveToNext()){
  107. Map<String, String> datum = new HashMap<String, String>(2);
  108. datum.put("datos1", registros.getString(1));
  109. datum.put("datos2", registros.getString(3));
  110. data.add(datum);
  111.  
  112.  
  113. }
  114. return data;
  115. }
  116. public String actualizar(String nombre,String contraseña){
  117. String Mensaje ="";
  118. SQLiteDatabase db = this.getWritableDatabase();
  119. ContentValues contenedor = new ContentValues();
  120. contenedor.put("password",contraseña);
  121. int cantidad = db.update(USER_TABLE, contenedor, COLUMN_usurarios+"='"+nombre+"'",null);
  122. if(cantidad!=0){
  123. Mensaje="Actualizado Correctamente";
  124. }else{
  125. Mensaje="No Actualizado";
  126. }
  127. db.close();
  128. return Mensaje;
  129. }
  130.  
  131. public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
  132. private Button reg,eliminar,button3;
  133. private TextView tvLogin;
  134. private EditText etuser, etPass;
  135.  
  136. private DbHelper db;
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. @Override
  145. protected void onCreate(Bundle savedInstanceState) {
  146. super.onCreate(savedInstanceState);
  147. setContentView(R.layout.activity_register);
  148.  
  149. db = new DbHelper(getApplicationContext(),null, null, 1);
  150. reg = (Button)findViewById(R.id.btnReg);
  151. eliminar=(Button)findViewById(R.id.button);
  152. tvLogin = (TextView)findViewById(R.id.tvLogin);
  153.  
  154. etuser = (EditText)findViewById(R.id.etuser);
  155. etPass = (EditText)findViewById(R.id.etPass);
  156. button3=(Button)findViewById(R.id.button3);
  157.  
  158. reg.setOnClickListener(this);
  159. tvLogin.setOnClickListener(this);
  160.  
  161.  
  162. eliminar.setOnClickListener(new View.OnClickListener(){
  163.  
  164.  
  165. @Override
  166. public void onClick(View v){
  167. DbHelper db = new DbHelper(getApplicationContext(),null,null,1);
  168. String usuarios = etuser.getText().toString();
  169. String mensaje = db.eliminar(usuarios);
  170. Toast.makeText(getApplicationContext(),mensaje,Toast.LENGTH_SHORT).show();
  171.  
  172.  
  173. }
  174. });
  175. button3.setOnClickListener(new View.OnClickListener() {
  176. @Override
  177. public void onClick(View v) {
  178. DbHelper db = new DbHelper(getApplicationContext(),null,null,1);
  179. String nombre = etuser.getText().toString();
  180. String contraseña = etPass.getText().toString();
  181. String Mensaje =db.actualizar(nombre,contraseña);
  182. Toast.makeText(getApplicationContext(),Mensaje,Toast.LENGTH_SHORT).show();
  183. }
  184. });
  185.  
  186. }
  187.  
  188.  
  189.  
  190. @Override
  191. public void onClick(View v) {
  192. switch(v.getId()){
  193. case R.id.btnReg:
  194. register();
  195. break;
  196. case R.id.tvLogin:
  197. startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
  198. finish();
  199. break;
  200. default:
  201.  
  202. }
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. private void register(){
  211. String user = etuser.getText().toString();
  212. String pass = etPass.getText().toString();
  213. if(user.isEmpty() && pass.isEmpty()){
  214. displayToast("Usuario o contraseña vacio");
  215. }
  216. else if (pass.isEmpty()){
  217. db.addUser(user,"1234");
  218. displayToast("Usuario registrado con exito!n" +
  219. "contraseña por defecto:1234");
  220. finish();
  221. }
  222. else{
  223. db.addUser(user,pass);
  224. displayToast("Usuario registrado con exito!");
  225. finish();
  226. }
  227. }
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. private void displayToast(String message){
  236. Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
  237. }
Add Comment
Please, Sign In to add comment