I was working with one table and all my program is ok, but now I have to add another table to my database, and by logic this is the code :
package net.ShamanOperativo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class UsuariosSQLiteHelper extends SQLiteOpenHelper {
//Sentencia SQL para crear la tabla de Incidentes
String sqlCreate = "CREATE TABLE Incidentes ( idInterno INTEGER PRIMARY KEY , codReg TEXT, codGr TEXT, entidad TEXT, codInc TEXT, loc TEXT, dom TEXT, sint TEXT, movil TEXT, sexo TEXT, edad INTEGER, estado TEXT, latitud DOUBLE, longitud DOUBLE,colorLoc TEXT, colorGr TEXT )";
String sqlCreate2 = "CREATE TABLE Moviles (idInterno INTEGER PRIMARY KEY, numMovil TEXT, colMovil TEXT, estado TEXT, localidad TEXT, cantServ INTEGER)";
String sqlCreateIndex = "CREATE UNIQUE INDEX idxIdInterno ON Incidentes(idInterno)";
public UsuariosSQLiteHelper(Context contexto, String nombre,
CursorFactory factory, int version) {
super(contexto, nombre, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//Se ejecuta la sentencia SQL de creación de la tabla
db.execSQL(sqlCreate);
db.execSQL(sqlCreateIndex);
db.execSQL(sqlCreate2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
//Se elimina la versión anterior de la tabla
db.execSQL("DROP TABLE IF EXISTS Incidentes");
db.execSQL("DROP TABLE IF EXISTS Moviles");
//Se crea la nueva versión de la tabla
db.execSQL(sqlCreate);
db.execSQL(sqlCreate2);
}
}
My table Incidentes is ok, it is which I was using always.. but I can't use the table Moviles, I use my table here:
UsuariosSQLiteHelper usdbh =
new UsuariosSQLiteHelper(this, "DBIncidentes", null, 1);
SQLiteDatabase db = usdbh.getWritableDatabase(); //Abro base de datos para escritura
if(db != null)
{
//db.execSQL("DELETE from Moviles");
for (int i=0; i<= (vecTotal.length) -1; i++) {
String reg = vecTotal[i].toString();
String [] inc = TextUtils.split(reg, "\\^");
String numMovil = inc[0];
String colMovil = inc[1];
String estado = inc[2];
String localidad = inc[3];
String strCantServ = inc[4];
Integer cantServ = Integer.parseInt(strCantServ);
try{
//Insertamos los datos en la tabla Incidentes
db.execSQL("INSERT INTO Moviles ( idInterno, numMovil, colMovil, estado, localidad, cantServ) " +
"VALUES ("+(i+1)+", '" + numMovil +"', '" + colMovil +"' , '" + estado +"', '" + localidad +"', " +cantServ +" )");
}
catch (Exception e)
{
e.getMessage();
}
Is there another thing I have to do to handle two tables or more? It isn't the same adapter and just refering which table of the database I want to consult or add info?
When the program want to add info into Moviles, logcat says it can't because Moviles doesn't exist.
No, there isn't anything extra you need to do for handling more than 1 table.
I think your table is not created because neither onCreate()
(because your database already exists) noronUpdate()
(you never increased the database version you pass to the UsuariosSQLiteHelper
, right?) is called.