这个数据操纵器允许我: 1. 创建表角色(“人”),我可以毫无问题地添加病人和医生。
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);
}
}
}
但是当我使用第二个“datamanipulator2”(从第一个数据操纵器复制粘贴)时,它应该创建表 Citas(“约会”),所以我可以添加一个约会,但它给出了一个错误说:
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 (?,?,?,?)
我注意到如果我将数据库版本(在“datamanipulator2”中)更改为 2,我可以解决这个问题
DATABASE_VERSION = 2;
但我想知道:A)另一种避免此错误的方法,我的意思是,我想将两个数据操纵器都保留为“DATABASE_VERSION = 1;” 总的来说,我认为这对我来说听起来更好,除非完全有必要“datamanipulator”的值为 1 而“datamanipulator2”的值为 2。B)为什么会发生这种情况?这是正常行为吗?