我正在以这种方式创建DataBase:
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BBDD bbdd=new BBDD(MainActivity.this);
SQLiteDatabase db=bbdd.getWritableDatabase();
}
}
BBDD:
public class BBDD extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "users.db";
public String TABLE_USERS="users";
public String TABLE_SKILLS="skills";
public String TABLE_USERTYPES="tipousuarios";
public Context ctx;
public BBDD(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ctx=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("David", "onCreate de BBDD");
dropTables();
//db=getWritableDatabase();
poblarTablas(db);
}
private void poblarTablas(SQLiteDatabase db) {
Log.i("David", "Creando tablas");
//Creamos las tablas
db.execSQL("CREATE TABLE "+TABLE_USERS+" ("+
"id INTEGER PRIMARY KEY, "+
"nombre TEXT NOT NULL, "+
"apellidos TEXT NOT NULL, "+
"tipoUsuario TEXT NOT NULL, "+
"login TEXT NOT NULL, "+
"password TEXT NOT NULL)");
Log.i("David", "Tabla users creada");
db.execSQL("CREATE TABLE"+TABLE_SKILLS+" ("+
"id INTEGER PRIMARY KEY, "+
"user TEXT FOREIGN KEY REFERENCES "+TABLE_USERS+"(login), "+
"habilidad TEXT NOT NULL)");
Log.i("David", "Tabla skills creada");
db.execSQL("CREATE TABLE "+TABLE_USERTYPES+"( "+
"id INTEGER PRIMARY KEY, "+
"tipousuario TEXT NOT NULL)");
Log.i("David", "Tabla tipos de usuarios creada");
//Insertamos los tipos de usuario
ContentValues valuesTipoUsuario=new ContentValues();
valuesTipoUsuario.put("tipousuario","administrador");
db.insert(TABLE_USERTYPES,null,valuesTipoUsuario);
ContentValues valuesTipoUsuario2=new ContentValues();
valuesTipoUsuario2.put("tipousuario", "técnico");
db.insert(TABLE_USERTYPES, null, valuesTipoUsuario2);
//Insertamos usuarios
ContentValues values=new ContentValues();
values.put("nombre", "Rubén");
values.put("apellidos", "García López");
values.put("tipoUsuario", "administrador");
values.put("login", "rgarcia");
values.put("password", "rgarcia");
db.insert(TABLE_USERS, null, values);
ContentValues values2=new ContentValues();
values2.put("nombre", "Rafael");
values2.put("apellidos", "Martín González");
values2.put("tipoUsuario", "técnico");
values2.put("login", "rmartin");
values2.put("password", "rmartin");
db.insert(TABLE_USERS, null, values2);
ContentValues values3=new ContentValues();
values3.put("nombre", "Sarah");
values3.put("apellidos", "López Martínez");
values3.put("tipoUsuario", "técnico");
values3.put("login", "slopez");
values3.put("password", "slopez");
db.insert(TABLE_USERS, null, values3);
ContentValues values4=new ContentValues();
values3.put("nombre", "David");
values3.put("apellidos", "Fernández Arias");
values3.put("tipoUsuario", "técnico");
values3.put("login", "dfernandez");
values3.put("password", "dfernandez");
db.insert(TABLE_USERS, null, values4);
ContentValues values5=new ContentValues();
values3.put("nombre", "Pilar");
values3.put("apellidos", "García Cabrera");
values3.put("tipoUsuario", "técnico");
values3.put("login", "pgarcia");
values3.put("password", "pgarcia");
db.insert(TABLE_USERS, null, values5);
//Insertamos las habilidades de los usuarios
ContentValues vSkills=new ContentValues();
vSkills.put("user", "rmartin");
vSkills.put("habilidad", "Reponedor de productos");
db.insert(TABLE_SKILLS, null, vSkills);
ContentValues vSkills2=new ContentValues();
vSkills2.put("user", "smartinez");
vSkills2.put("habilidad", "Envolver");
db.insert(TABLE_SKILLS, null, vSkills2);
ContentValues vSkills3=new ContentValues();
vSkills3.put("user", "dfernandez");
vSkills3.put("habilidad", "Envolver");
db.insert(TABLE_SKILLS, null, vSkills3);
ContentValues vSkills4=new ContentValues();
vSkills4.put("user", "dfernandez");
vSkills4.put("habilidad", "Reponedor de productos");
db.insert(TABLE_SKILLS, null, vSkills4);
ContentValues vSkills5=new ContentValues();
vSkills5.put("user", "pgarcia");
vSkills5.put("habilidad", "Cobrar");
db.insert(TABLE_SKILLS, null, vSkills5);
}
private void dropTables() {
Log.i("David", "Eliminando tablas");
ctx.deleteDatabase(DATABASE_NAME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
这样,我收到了例外:
E/SQLiteLog: (1802) os_unix.c:30027: (2) stat(/data/data/com.prueba.sdos.pruebasdos/databases/users.db) -
E/SQLiteLog: (1802) statement aborts at 9: [CREATE TABLE users (id INTEGER PRIMARY KEY, nombre TEXT NOT NULL, apellidos TEXT NOT NULL, tipoUsuario TEXT NOT NULL, login TEXT NOT NULL, password TEXT NOT NULL)]
E/SQLiteLog: (1) statement aborts at 2: [ROLLBACK;] cannot rollback - no transaction is active
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4157fd88)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.prueba.sdos.pruebasdos, PID: 8734
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prueba.sdos.pruebasdos/com.prueba.sdos.pruebasdos.MainActivity}: android.database.sqlite.SQLiteException: cannot rollback - no transaction is active (code 1)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2263)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: cannot rollback - no transaction is active (code 1)
at android.database.sqlite.SQLiteConnection.nativeExecute(Native Method)
at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:555)
at android.database.sqlite.SQLiteSession.endTransactionUnchecked(SQLiteSession.java:439)
at android.database.sqlite.SQLiteSession.endTransaction(SQLiteSession.java:401)
at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:522)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.prueba.sdos.pruebasdos.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5240)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
我在升级DDBB时已经读过关于这个错误的信息,但是第一次执行应用程序时会发生这种情况。
有人可以帮忙吗?
谢谢。