Android SQLite在启动时只读取一个表,而不修改第二个表

Android SQLite在启动时只读取一个表,而不修改第二个表,android,android-sqlite,multiple-tables,sqliteopenhelper,Android,Android Sqlite,Multiple Tables,Sqliteopenhelper,我正在尝试编写两个表的代码 一个用于保存某些鞋的信息,另一个用于保存品牌信息 虽然一只鞋很好用,但另一只却不行 public class AdminSQLiteOpenHelper extends SQLiteOpenHelper { public AdminSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, i

我正在尝试编写两个表的代码

一个用于保存某些鞋的信息,另一个用于保存品牌信息

虽然一只鞋很好用,但另一只却不行

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) {

    }
}
这是我试图修改表格的品牌活动

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);
        }

    }
}

您所显示的代码可以将数据插入数据库

因此,您可能存在以下常见问题之一

自运行应用程序以来,您已将另一个表添加到AdminSQLiteOpenHelper类的onCreate方法中的代码中。

  • 数据库助手(扩展SQLiteOpenHelper的类)的onCreate方法在数据库的生命周期内只运行一次
  • 要更改模式(例如,添加表),您必须将更改应用到其他地方(例如,通过onUpgrade方法,同时增加数据库版本),或者,如开发应用程序时经常发生的情况,删除数据库。您可以通过以下方式轻松删除数据库:-

    • 卸载应用程序,或
    • 正在删除应用程序的数据
错误地确定缺少数据

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();
    }
}
  • 这通常是由于试图通过GUI确定数据的存在,但没有刷新显示的数据
测试 以下代码用于测试该代码在向数据库添加数据方面是否确实有效

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

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();
    }
}
这只是一个简化的活动,它使用原始数据,而不是通过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();
    }
}
初次运行结果:-

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: <<<<<
2019-11-28 13:11:29.348 4788-4788/a.so59080227 D/Data:onCreate:create table zaps(矩阵文本主键、彩色文本、塔拉文本)
2019-11-28 13:11:29.348 4788-4788/a.so59080227 D/数据:onCreate:创建表alumnos(matricula text主键、nombre text、tel text)
2019-11-28 13:11:29.368 4788-4788/a.so59080227 I/System.out:>>>>>>>>转储游标android.database.sqlite。SQLiteCursor@5df21a1
2019-11-28 13:11:29.3714788-4788/a.so59080227 I/System.out:0{
2019-11-28 13:11:29.3724788-4788/a.so59080227 I/System.out:matricula=test
2019-11-28 13:11:29.3724788-4788/a.so59080227 I/System.out:color=red
2019-11-28 13:11:29.3724788-4788/a.so59080227 I/System.out:talla=blah
2019-11-28 13:11:29.3724788-4788/a.so59080227 I/System.out:}
2019-11-28 13:11:29.3724788-4788/a.so59080227 I/System.out:>转储游标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:您只需要使用SQLiteOpenHelper的onUpgrade方法,如下所示。可能您的第二个表现在没有创建因此,对于通用解决方案,您需要增加数据库版本(AdminSQLiteOpenHelper构造函数的第四个字段),并添加以下代码,而不是onUpgrade方法。

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 if not exists zaps(matricula text primary 
key,color text,talla text)";
    String sql2 = "create table if not exists 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) {
  String sql1 = "create table if not exists zaps(matricula text primary 
key,color text,talla text)";
    String sql2 = "create table if not exists 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);
}
}

我真的不明白这里的问题;请至少发布您的LogCat输出,并指出代码在哪一行崩溃(如果确实崩溃)。