Databases In Android Studio
Databases In Android Studio Using Java:
SQLite is a database engine written in the C programming language. It is an open-source SQL database that stores data in a text file on a device. Android comes in with built in SQLite database implementation.
SQLite supports all the relational database features. To access this database, you don't need to establish any kind of connections for it like JDBC, ODBC etc.
Database – Creation:
In order to create a database, you just need to call this method openOrCreateDatabase with your database name and mode as a parameter. It returns an instance of SQLite database which you have to receive in your own object. Its syntax is given below.
SQLiteDatabase mydatabase = openOrCreateDatabase("your database name",MODE_PRIVATE,null);
Database – Insertion:
We can create a table or insert data into table using execSQL method defined in SQLiteDatabase class.
This will insert some values into our table in our database. Its syntax is given below.
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS Table_Name(Username VARCHAR,Password VARCHAR);");
mydatabase.execSQL("INSERT INTO Table_Name VALUES('admin','admin');");
Database – Fetching:
We can retrieve anything from a database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data.
Cursor resultSet = mydatbase.rawQuery("Select * from Table_Name",null);
resultSet.moveToFirst();
String username = resultSet.getString(0);
String password = resultSet.getString(1);
MYSQL is used as a database at the webserver and PHP is used to fetch data from the database. Our application will communicate with the PHP page with necessary parameters and PHP will contact MYSQL database and will fetch the result and return the results to us.
While Android Studio is primarily used for developing native Android applications using Java or Kotlin, it's not the ideal environment for directly integrating PHP and MySQL. PHP is a server-side scripting language, and MySQL is a relational database management system, both of which are typically used in web development.
However, you can achieve communication between an Android app and a PHP/MySQL backend by using HTTP requests (such as HTTP GET or POST) to interact with PHP scripts on a server.
Firebase is a powerful platform provided by Google that offers various services for building and improving your app's backend infrastructure. It includes services for real-time databases, authentication, cloud storage, messaging, and more. To integrate Firebase into your Android Studio project using Java, follow these steps:
Set Up Firebase Project:
A. Go to the Firebase Console (https://console.firebase.google.com/).
B. Create a new project or select an existing one.
C. Follow the setup instructions to add your Android app to the project. You'll need to provide your app's package name and other details.
Recommended by LinkedIn
Add Firebase SDK to Your Project:
A. In your Android Studio project, open the build.gradle (Module: app) file.
B. Add the following dependency to the dependencies block:
implementation 'com.google.firebase:firebase-analytics:19.0.0' // Add more dependencies as needed
C. Also, add the Google services plugin at the bottom of the file:
apply plugin: 'com.google.gms.google-services'
Download Google Services JSON:
A. After adding the dependency and plugin, go back to the Firebase Console.
B. In the Project settings, download the google-services.json file.
C. Place this file in the app module of your Android Studio project.
Initialize Firebase:
A. In your app's main activity or application class, add the following code to initialize Firebase:
import com.google.firebase.FirebaseApp; // Import the necessary classe
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
}s
Using Firebase Services:
A. Once Firebase is initialized, you can use various Firebase services. For example, to use Firebase Realtime Database:
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a reference to the database
mDatabase = FirebaseDatabase.getInstance().getReference();
}
};
Firebase services are:
For More Information Please visit this link:
عاش استمري ❤️