Algo and Math

Android Studio - SQLite (come approcciarsi)

mi reco sulla cartella:

com.example.sqllite_1

tasto destro e creo un nuovo file Java

package com.example.sqllite_1;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

package com.example.sqllite_1;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

Se pensassimo di creare un nuovo Database e quindi una Tabella.

ID NAME SURNAME MARKS

1 Mark Taylor 78

2 Tom Smith 89

Ho solo inserito una Tabella con due record, sia chiaro

public static final String DATABASE_NAME = "student.db";

// Database Creazione
public static final String DATABASE_NAME = "student.db";
// Creazioen Tabella
public static final String TABLE_NAME = "student_data";

// Database Creazione
public static final String DATABASE_NAME = "student.db";
// Creazioen Tabella
public static final String TABLE_NAME = "student_data";

// Creamo delle Colonne per la nostra Tabella
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "MARKS";

@Override
public void onCreate(SQLiteDatabase db) {
// Creare Tabella
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// sono QUERY SQL. Interrogazioni DROP TABLE IF EXISTS Nome_Tabella
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}

// ovviamente avendo lo stesso nome della Classe che abbiamo creato
// E' il Costruttore. Uno speciale metodo
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}

// Creare Tabella
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// sono QUERY SQL. Interrogazioni DROP TABLE IF EXISTS Nome_Tabella
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
}

qui avremo:

@Override
public void onCreate(SQLiteDatabase db) {
// Creare Tabella
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// sono QUERY SQL. Interrogazioni DROP TABLE IF EXISTS Nome_Tabella
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
}

@Override
public void onCreate(SQLiteDatabase db) {
// Creare Tabella
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// sono QUERY SQL. Interrogazioni DROP TABLE IF EXISTS Nome_Tabella
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}

// crea Database e Tabella
SQLiteDatabase db = this.getWritableDatabase();

// creo il riferimento alla Classe
DatabaseHelper myDb;

// creo il riferimento alla Classe
DatabaseHelper myDb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myDb = new DatabaseHelper(this);
}

dove andiamo a reperire la Tabella, appena creata?

Con riferimento di sqllite_1 (Nome del Nostro Progetto)

Per visionare il Database e la Tabella creata

Exit mobile version