I'm trying to code two tables.
One which saves the information on some shoes and another one that saves information on the brands.
Although the shoe one works perfectly the other one doesn't.
public class AdminSQLiteOpenHelper extends SQLiteOpenHelper {
public AdminSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql1 = "create table zaps(matricula text primary key,color text,talla text)";
String sql2 = "create table alumnos(matricula text primary key,nombre text,tel text)";
Log.d("Data", "onCreate: " + sql1);
Log.d("Data", "onCreate: " + sql2);
db.execSQL(sql1);
db.execSQL(sql2);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
And this is the activity for the brands in which I'm trying to modify the table.
public class ProvActivity extends AppCompatActivity {
EditText etMatricula, etNombre, etTel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prov);
etMatricula = (EditText) findViewById(R.id.etMatricula);
etNombre = (EditText) findViewById(R.id.etNombre);
etTel = (EditText) findViewById(R.id.etTel);
}
public void guardar(View v) {
try {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"administracion1", null, 1);
SQLiteDatabase bd1 = admin.getWritableDatabase();
ContentValues reg = new ContentValues();
reg.put("matricula", etMatricula.getText().toString());
reg.put("nombre", etNombre.getText().toString());
reg.put("tel", etTel.getText().toString());
bd1.insert("alumnos", null, reg);
bd1.close();
etMatricula.setText("");
etNombre.setText("");
etTel.setText("");
etMatricula.requestFocus();
Toast.makeText(this, "Se ha guardado el registro con éxito.",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
}
}
public void consultar_matricula(View v) {
try {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"administracion1", null, 1);
SQLiteDatabase bd1 = admin.getWritableDatabase();
String cod = etMatricula.getText().toString();
Cursor fila = bd1.rawQuery(
"select nombre,tel from alumnos where matricula=" + cod, null);
if (fila.moveToFirst()) {
etNombre.setText(fila.getString(0));
etTel.setText(fila.getString(1));
} else
Toast.makeText(this, "No existe un registro con dicha matrícula.",
Toast.LENGTH_SHORT).show();
bd1.close();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);
}
}
}
The code you have shown works as far as inserting data into the database.
As such you likely have one of the following common issues.
You've added the other table to the code in the onCreate method of the AdminSQLiteOpenHelper class since running the App.
To change the schema (e.g. add a table) you either have to apply the changes elsewhere (suchs as via the onUpgrade method, along with increasing the database version) or, as is often the case when developing an App, Delete the database. You can easily delete the database by either :-
Wrongly determining the absence of data
The following code was used for testing that the code does in fact work with regards to adding data to the database.
AdminSQLiteOpenHelper.java
public class AdminSQLiteOpenHelper extends SQLiteOpenHelper {
public AdminSQLiteOpenHelper(@Nullable Context context) {
super(context, "administracion1", null, 1);
}
/*
See above for easier to use constructor (just pass the Context)
*/
public AdminSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql1 = "create table zaps(matricula text primary key,color text,talla text)";
String sql2 = "create table alumnos(matricula text primary key,nombre text,tel text)";
Log.d("Data", "onCreate: " + sql1);
Log.d("Data", "onCreate: " + sql2);
db.execSQL(sql1);
db.execSQL(sql2);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
/*
Added to simplify testing
*/
public long insertAlumnos(String matricula, String nombre, String tel) {
ContentValues cv = new ContentValues();
cv.put("matricula",matricula);
cv.put("nombre",nombre);
cv.put("tel",tel);
SQLiteDatabase db = this.getWritableDatabase();
return db.insert("alumnos",null,cv);
}
public long insertZaps(String matricula, String color, String talla) {
ContentValues cv = new ContentValues();
cv.put("matricula",matricula);
cv.put("color",color);
cv.put("talla",talla);
SQLiteDatabase db = this.getWritableDatabase();
return db.insert("zaps",null,cv);
}
public void logAllZaps() {
logAllRows("zaps");
}
public void logAllAlumnos() {
logAllRows("alumnos");
}
private void logAllRows(String table) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor csr = db.query(table,null,null,null,null,null,null);
DatabaseUtils.dumpCursor(csr);
csr.close();
}
}
MainActivity.java
THis is just a simplified activity which uses raw data rather than getting the data through the UI :-
public class MainActivity extends AppCompatActivity {
AdminSQLiteOpenHelper adminHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adminHelper = new AdminSQLiteOpenHelper(this);
adminHelper.insertZaps("test","red","blah");
adminHelper.insertAlumnos("test","199","0000000000");
adminHelper.logAllZaps();
adminHelper.logAllAlumnos();
//guardar();
//other();
}
/* Not USED although tested */
public void guardar() {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"administracion1", null, 1);
SQLiteDatabase bd1 = admin.getWritableDatabase();
ContentValues reg = new ContentValues();
reg.put("matricula", "test");
reg.put("nombre", "100");
reg.put("tel", "0000000000");
bd1.insert("alumnos", null, reg);
//bd1.close();
}
}
Result of initial run :-
2019-11-28 13:11:29.348 4788-4788/a.so59080227 D/Data: onCreate: create table zaps(matricula text primary key,color text,talla text)
2019-11-28 13:11:29.348 4788-4788/a.so59080227 D/Data: onCreate: create table alumnos(matricula text primary key,nombre text,tel text)
2019-11-28 13:11:29.368 4788-4788/a.so59080227 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5df21a1
2019-11-28 13:11:29.371 4788-4788/a.so59080227 I/System.out: 0 {
2019-11-28 13:11:29.372 4788-4788/a.so59080227 I/System.out: matricula=test
2019-11-28 13:11:29.372 4788-4788/a.so59080227 I/System.out: color=red
2019-11-28 13:11:29.372 4788-4788/a.so59080227 I/System.out: talla=blah
2019-11-28 13:11:29.372 4788-4788/a.so59080227 I/System.out: }
2019-11-28 13:11:29.372 4788-4788/a.so59080227 I/System.out: <<<<<
2019-11-28 13:11:29.372 4788-4788/a.so59080227 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@fc68fc6
2019-11-28 13:11:29.373 4788-4788/a.so59080227 I/System.out: 0 {
2019-11-28 13:11:29.373 4788-4788/a.so59080227 I/System.out: matricula=test
2019-11-28 13:11:29.373 4788-4788/a.so59080227 I/System.out: nombre=199
2019-11-28 13:11:29.373 4788-4788/a.so59080227 I/System.out: tel=0000000000
2019-11-28 13:11:29.373 4788-4788/a.so59080227 I/System.out: }
2019-11-28 13:11:29.373 4788-4788/a.so59080227 I/System.out: <<<<<