0

我是 Android 的初学者,这个数据操纵器允许我将数据插入到第一个表(“人”)中,但是我无法使用第二种方法,第二个字符串将数据添加到第二个表(“约会”)中“插入 2”。但是数据被插入到第一个表而不是第二个表中。关于我做错了什么的任何想法?提前谢谢!

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 android.widget.Toast;
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";
static final String TABLE_NAME2 = "Citas";
private static Context context;
private static Context context2;
static SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + " (nombre,cedula,fechanacimiento,telefonocasa,telefonomovil,tipo) values (?,?,?,?,?,?)";
private static final String INSERT2 = "insert into " + TABLE_NAME2 + " (nombrepaciente,fechacita,horacita,nombredoctor) 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  void DataManipulator(Context context2) {
    DataManipulator.context2 = context2;
    OpenHelper openHelper = new OpenHelper(DataManipulator.context2);
    DataManipulator.db = openHelper.getWritableDatabase();
    this.insertStmt = DataManipulator.db.compileStatement(INSERT2);
}

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 long insert2(String nombrepaciente,String fechacita,String horacita,String nombredoctor) {
    this.insertStmt.bindString(1, nombrepaciente);
    this.insertStmt.bindString(2, fechacita);
    this.insertStmt.bindString(3, horacita);
    this.insertStmt.bindString(4, nombredoctor);
    //this.insertStmt.bindString(5, telefonomovil);
    //this.insertStmt.bindString(6, tipo);
    return this.insertStmt.executeInsert();
}

public void deleteAll() {
    db.delete(TABLE_NAME, null, null);
}

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)");
         //B) FINAL NO QUITAR
         db.execSQL("CREATE TABLE " + TABLE_NAME2 + " (id INTEGER PRIMARY KEY, nombrepaciente TEXT, fechacita TEXT, horacita TEXT, nombredoctor TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
    }
}
}
4

1 回答 1

1

您不能有两个采用相同参数的方法:

public DataManipulator(Context context) {
    DataManipulator.context = context;
    OpenHelper openHelper = new OpenHelper(DataManipulator.context);
    DataManipulator.db = openHelper.getWritableDatabase();
    this.insertStmt = DataManipulator.db.compileStatement(INSERT);
}

public  void DataManipulator(Context context2) {
    DataManipulator.context2 = context2;
    OpenHelper openHelper = new OpenHelper(DataManipulator.context2);
    DataManipulator.db = openHelper.getWritableDatabase();
    this.insertStmt = DataManipulator.db.compileStatement(INSERT2);
}

尝试传递一个标志:

public DataManipulator(Context context, boolean first) {
    DataManipulator.context = context;
    OpenHelper openHelper = new OpenHelper(DataManipulator.context);
    DataManipulator.db = openHelper.getWritableDatabase();

    if(first)
        this.insertStmt = DataManipulator.db.compileStatement(INSERT);
    else
        this.insertStmt = DataManipulator.db.compileStatement(INSERT2);
}

或者,您应该考虑创建两个不同的处理程序类,每个表一个。

于 2012-09-24T20:48:55.210 回答