Merge pull request #5 from LinlyBoi/data

Data SQLite added
This commit is contained in:
Linly
2023-12-20 09:49:29 +02:00
committed by GitHub
6 changed files with 167 additions and 29 deletions

View File

@@ -0,0 +1,37 @@
package com.example.myapp_1.DB;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "WITL.db";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_USERS_TABLE =
"CREATE TABLE users (" +
"U_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"UserName TEXT NOT NULL," +
"phone TEXT CHECK(length(phone) = 11)," +
"U_email TEXT NOT NULL UNIQUE," +
"U_password TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create your tables here
db.execSQL(CREATE_USERS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Handle database upgrades if needed
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}

View File

@@ -0,0 +1,78 @@
package com.example.myapp_1.DB;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBOperations {
private DBHelper dbHelper;
public DBOperations(Context context) {
dbHelper = new DBHelper(context);
}
// Insert user data into the users table
public long insertUser(String UserName, String phone,String email, String password) {
try {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("UserName", UserName);
values.put("phone", phone);
values.put("U_email", email);
values.put("U_password", password);
long newRowId = db.insert("users", null, values);
return newRowId;
}
catch (Exception e){
return 0;
}
}
public boolean checkLoginCredentials(String UserName, String password) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] columns = {"U_ID"};
String selection = "UserName=? AND U_password=?";
String[] selectionArgs = {UserName, password};
Cursor cursor = db.query("users", columns, selection, selectionArgs, null, null, null);
boolean isCredentialsValid = cursor.moveToFirst();
cursor.close();
return isCredentialsValid;
}
// Add other methods as needed
}
// Retrieve all users from the users table
// public Cursor getAllUsers() {
// SQLiteDatabase db = dbHelper.getReadableDatabase();
//
// String[] columns = {"U_ID", "first_name", "last_name", "U_email", "phone", "U_password"};
// return db.query("users", columns, null, null, null, null, null);
// }
// Delete a user from the users table based on their email
// public int deleteUser(String email) {
// SQLiteDatabase db = dbHelper.getWritableDatabase();
//
// String selection = "U_email=?";
// String[] selectionArgs = {email};
//
// int deletedRows = db.delete("users", selection, selectionArgs);
// db.close();
//
// return deletedRows;
// }

View File

@@ -1,6 +1,5 @@
package com.example.myapp_1;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
@@ -11,6 +10,8 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapp_1.DB.DBOperations;
public class LoginActivity extends AppCompatActivity {
EditText username;
@@ -18,7 +19,7 @@ public class LoginActivity extends AppCompatActivity {
TextView forgetpassword;
Button login;
Button signup;
DBOperations dbOperations = new DBOperations(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -54,11 +55,20 @@ public class LoginActivity extends AppCompatActivity {
if(username_data.isEmpty() || password_data.isEmpty()){
Toast.makeText(LoginActivity.this,"Please fill your data!", Toast.LENGTH_LONG).show();
}else{
// Check for user input if in database
// if found go to home page
// else keep in same page bitch
Intent newScreen = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(newScreen);
if (dbOperations.checkLoginCredentials(username_data, password_data)) {
// Login successful
// Proceed to the next screen or perform other actions
Toast.makeText(LoginActivity.this,"Welcome", Toast.LENGTH_LONG).show();
Intent newScreen = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(newScreen);
} else {
// Login failed
Toast.makeText(LoginActivity.this,"Invalid Iformation", Toast.LENGTH_LONG).show();
}
}
}

View File

@@ -1,14 +1,16 @@
package com.example.myapp_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapp_1.DB.DBOperations;
public class SigninActivity extends AppCompatActivity {
EditText username;
@@ -18,6 +20,7 @@ public class SigninActivity extends AppCompatActivity {
Button register;
DBOperations dbOperations = new DBOperations(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -32,37 +35,46 @@ public class SigninActivity extends AppCompatActivity {
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username_data = username.getText().toString();
String phonenumber_data = username.getText().toString();
String email_data = username.getText().toString();
String password_data = username.getText().toString();
String phonenumber_data = phonenumber.getText().toString(); // Corrected here
String email_data = email.getText().toString(); // Corrected here
String password_data = password.getText().toString(); // Corrected here
boolean checked = false;
//Add the user to the database
//betengano base
String [] data = new String[4];
String[] data = new String[4];
data[0] = username_data;
data[1] = phonenumber_data;
data[2] = email_data;
data[3] = password_data;
for(int i = 0; i < data.length; i++){
if(data[i].isEmpty()){
for (int i = 0; i < data.length; i++) {
if (data[i].isEmpty()) {
Toast.makeText(SigninActivity.this, "Please fill your data!", Toast.LENGTH_LONG).show();
checked = false;
break;
}else{
} else {
checked = true;
}
}
if(checked == true){
if (checked) {
long newRowId = dbOperations.insertUser(data[0], data[1], data[2], data[3]);
if (newRowId != -1) {
Toast.makeText(SigninActivity.this, "SignUp Success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SigninActivity.this, "SignUp failed" + newRowId, Toast.LENGTH_LONG).show();
}
Intent newScreen = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(newScreen);
}
}
});
}
}

View File

@@ -4,7 +4,7 @@
-- last_name => User's Last Name
-- U_email => User's Registered Email
-- U_password => User's Password
CREATE TABLE users(U_ID SERIAL PRIMARY KEY,
CREATE TABLE users(u_id SERIAL PRIMARY KEY,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
U_email varchar(255) NOT NULL UNIQUE,
@@ -54,12 +54,13 @@ CREATE TABLE prefers(pref_UID SERIAL,
-- time_of_day => Time object, nanosecond accuracy, y'all handle it idk
-- week_day => varchar as {Mon, Tue, Wed}, Could probs just encode with 0 -> 6
-- tram_line => int ranging 1 -> 4
-- fill_status => Boolean, 1 full, 0 not as full
CREATE TABLE arrival(time_of_day TIME,
-- direction => Boolean, 1 full, 0 not as full
CREATE TABLE arrivals(
a_id SERIAL PRIMARY KEY,
time_of_day TIME,
week_day INTEGER,
tram_line INTEGER,
fill_status BOOLEAN
CONSTRAINT ck PRIMARY KEY (week_day,tram_line)
CONSTRAINT in_week CHECK (week_day >= 0 AND week_day <= 6)
direction BOOLEAN,
CONSTRAINT in_week CHECK (week_day >= 0 AND week_day <= 6),
CONSTRAINT valid_line CHECK (tram_line >= 1 AND tram_line <= 4)
);

View File

@@ -4,5 +4,5 @@
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Wed Dec 06 17:47:01 EET 2023
sdk.dir=C\:\\Users\\Beast\\AppData\\Local\\Android\\Sdk
#Sun Dec 10 14:55:12 EET 2023
sdk.dir=C\:\\Users\\ahmed\\AppData\\Local\\Android\\Sdk