This datamanipulator allows me to:
1. Create table Personas (“people”) and i can add pacients and doctors without any issue.
package com.example.citas.medicas;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import java.util.ArrayList;
import java.util.List;
public class DataManipulator
{
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
static final String TABLE_NAME = "Personas";
private static Context context;
static SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + " (nombre,cedula,fechanacimiento,telefonocasa,telefonomovil,tipo) values (?,?,?,?,?,?)";
public DataManipulator(Context context) {
DataManipulator.context = context;
OpenHelper openHelper = new OpenHelper(DataManipulator.context);
DataManipulator.db = openHelper.getWritableDatabase();
this.insertStmt = DataManipulator.db.compileStatement(INSERT);
}
public long insert(String nombre,String cedula,String fechanacimiento,String telefonocasa,String telefonomovil,String tipo) {
this.insertStmt.bindString(1, nombre);
this.insertStmt.bindString(2, cedula);
this.insertStmt.bindString(3, fechanacimiento);
this.insertStmt.bindString(4, telefonocasa);
this.insertStmt.bindString(5, telefonomovil);
this.insertStmt.bindString(6, tipo);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
db.delete(TABLE_NAME, null, null);
}
public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME, new String[] { "id","nombre","cedula","fechanacimiento","telefonocasa","telefonomovil","tipo" }, null, null, null, null, "name asc");
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),
cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
public void delete(int rowId) {
db.delete(TABLE_NAME, null, null);
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, nombre TEXT, cedula TEXT, fechanacimiento TEXT, telefonocasa TEXT, telefonomovil TEXT, tipo TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
BUT when i use this 2nd “datamanipulator2” (copy-paste from the 1st data manipulator), its supposed to create the table Citas (“appointments”) so i can add an appointment but it gaves an error saying:
09-23 22:40:57.202: E/AndroidRuntime(889): android.database.sqlite.SQLiteException: no such table: Citas: , while compiling: insert into Citas (nombrepaciente,fechacita,horacita,nombredoctor) values (?,?,?,?)
I noticed i can fix this if i change (in the “datamanipulator2”) the db version to 2
DATABASE_VERSION = 2;
but i would like to know: A) another way to avoid this error, i mean, i would like to keep both datamanipulators with “DATABASE_VERSION = 1;” which i think it sounds better to me in general, unless is completely necessary that “datamanipulator” has a value of 1 and the “datamanipulator2” has a value of 2. B) why this is happening? is this a normal behavior?
If you look at the class SQLiteOpenHelper, onCreate is only called when the database is created the first time. onUpgrade is called if the version of the database is changed. The changing of the version serves exactly that purpose – gives a hook to create additional tables, etc.
So, there are 2 choices:
1. Remove the database completely before re-installing your application so that both tables are created at version 1.
2. Keep incrementing the version as you add more tables.
In the development cycle, I would recommend using option 1.
Hope this helps…