Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.pablo.infovalenciacf;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- /**
- * Created by pablo on 24/2/18.
- */
- public class UsersDBHelper extends SQLiteOpenHelper {
- public static final String TAG = UsersDBHelper.class.getSimpleName();
- public static final String DB_NAME = "valencia.db";
- public static final int DB_VERSION = 1;
- public static final String USER_TABLE = "usuarios";
- public static final String COLUMN_ID = "_id";
- public static final String COLUMN_USER = "username";
- public static final String COLUMN_EMAIL = "email";
- public static final String COLUMN_PASS = "password";
- public static final String sqlCreate = "CREATE TABLE " + USER_TABLE + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + COLUMN_USER + " TEXT," + COLUMN_EMAIL + " TEXT," + COLUMN_PASS + " TEXT)";
- public UsersDBHelper(Context context) {
- super(context, DB_NAME, null, DB_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(sqlCreate);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
- onCreate(db);
- }
- public void addUser(String username, String email, String pass) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_USER, username);
- values.put(COLUMN_EMAIL, email);
- values.put(COLUMN_PASS, pass);
- long id = db.insert(USER_TABLE, null, values);
- db.close();
- Log.d(TAG, "user inserted" + id);
- }
- public boolean getUser(String username, String pass) {
- String selectQuery = "select * from " + USER_TABLE + " where " + COLUMN_USER + " = " + "'" + username + "'" + " and " + COLUMN_PASS + " = " + "'" + pass + "'";
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- cursor.moveToFirst();
- if (cursor.getCount() > 0) {
- return true;
- }
- cursor.close();
- db.close();
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement