无法执行此操作,因为在Android中复制多个文件时不会显示当前事务

时间:2017-01-17 09:10:07

标签: android sqlite

我是SQLite和Android的初学者。我需要将一个db(例如:DB1)的表复制到另一个db(例如:DB2)。根据我的代码,在创建时创建一个数据库和相关表。当按下按钮时,我需要创建另一个表并将数据从一个表复制到另一个表。两个表都在创建,但数据不是复制。我在logcat中没有出现当前事务。这里有类似的问题,但我没有得到任何有关此问题的解决方案。请帮我解决这个问题。

   class Object
      def method_missing(*args)

        puts 'Module1'
        super
      end
    end

logcat的

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

// Database Name
public static final String DATABASE_NAME = "TempSignageDBManager";

// table names
private static final String TABLE_SCHEDULE = "Schedule";
private static final String TABLE_IMAGETIME = "ImageTime";
private static final String TABLE_REBOOT = "Reboot";
private static final String TABLE_RSS = "RSS";
private static final String TABLE_HEADER = "Header";
private static final String TABLE_SCROLL = "Scroll";
private static final String TABLE_WEBSITE = "Website";
private static final String TABLE_VIDEOS = "Videos";
private static final String TABLE_IMAGES = "Images";
private static final String TABLE_HEADERS = "Headers";
private static final String TABLE_SCROLLS = "Scrolls";
private static final String TABLE_WEBSITES = "Websites";

// Common column names
private static final String KEY_ID = "id";
private static final String KEY_CREATED_AT = "created_at";

// Schedule Table - Columns names
private static final String KEY_START_TIME = "StartTime";
private static final String KEY_END_TIME = "EndTime";
private static final String KEY_START_DATE = "StartDate";
private static final String KEY_END_DATE = "EndDate";
private static final String KEY_TEMPLATE = "Template";
private static final String KEY_PLAYLIST = "PlayList";

// ImageTime Table - Columns names
private static final String KEY_FILE_PATH = "FilePath";
private static final String KEY_IMAGE_TIME = "ImageTime";

// Reboot  Table - Columns names
private static final String KEY_REBOOT_TIME = "RebootTime";

// RSS  Table - Columns names
private static final String KEY_RSS_TIME = "RssTime";

// Header Table - Columns names
private static final String KEY_HEADER_NAME = "thname";
private static final String KEY_HEADER_FONT = "thfont";
private static final String KEY_HEADER_SIZE = "thsize";
private static final String KEY_HEADER_STYLE = "thstyle";
private static final String KEY_HEADER_COLOR = "thcolor";

// Scroll Table - Columns names
private static final String KEY_SCROLL_NAME = "txname";
private static final String KEY_SCROLL_FONT = "txfont";
private static final String KEY_SCROLL_SIZE = "txsize";
private static final String KEY_SCROLL_STYLE = "txstyle";
private static final String KEY_SCROLL_COLOR = "txcolor";
private static final String KEY_SCROLL_SPEED = "txspeed";

// Website  Table - Columns names
private static final String KEY_WEBSITE_NAME = "wname";

// Videos Table - Columns names
private static final String KEY_VIDEO_PLAYLIST = "PlayList";
private static final String KEY_VIDEO_TEMPLATE = "Template";
private static final String KEY_VIDEO_NAME = "VideoName";

// Images Table - Columns names
private static final String KEY_IMAGE_PLAYLIST = "PlayList";
private static final String KEY_IMAGE_TEMPLATE = "Template";
private static final String KEY_IMAGE_NAME = "ImageName";
private static final String KEY_IMAGE_ZONE = "ImageZone";

// Headers Table - Columns names
private static final String KEY_HEADER_PLAYLIST = "PlayList";
private static final String KEY_HEADER_TEMPLATE = "Template";
private static final String KEY_HEADER = "Header";

// Scrolls Table - Columns names
private static final String KEY_SCROLLER_PLAYLIST = "PlayList";
private static final String KEY_SCROLLER_TEMPLATE = "Template";
private static final String KEY_SCROLLER = "Scroll";

// Websites Table - Columns names
private static final String KEY_WEBSITE_PLAYLIST = "PlayList";
private static final String KEY_WEBSITE_TEMPLATE = "Template";
private static final String KEY_WEBSITE = "Website";

public Context cont;

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.cont = context;

}

// Schedule table create statement
String CREATE_TABLE_SCHEDULE = "CREATE TABLE " + TABLE_SCHEDULE + "("
        + KEY_START_TIME + " VARCHAR," + KEY_END_TIME + " VARCHAR," + KEY_START_DATE + " VARCHAR,"
        + KEY_END_DATE + " VARCHAR," + KEY_TEMPLATE + " VARCHAR," + KEY_PLAYLIST + " VARCHAR"  + ")";


// ImageTime table create statement
String CREATE_TABLE_IMAGETIME = "CREATE TABLE " + TABLE_IMAGETIME + "("
        + KEY_FILE_PATH + " VARCHAR," + KEY_IMAGE_TIME + " VARCHAR" + ")";


// Reboot table create statement
String CREATE_TABLE_REBOOT = "CREATE TABLE " + TABLE_REBOOT + "("
        + KEY_REBOOT_TIME + " VARCHAR" + ")";


// RSS table create statement
String CREATE_TABLE_RSS = "CREATE TABLE " + TABLE_RSS + "("
        + KEY_RSS_TIME + " VARCHAR" + ")";


// Header table create statement
String CREATE_TABLE_HEADER = "CREATE TABLE " + TABLE_HEADER + "("
        + KEY_HEADER_NAME + " VARCHAR," + KEY_HEADER_FONT + " VARCHAR," + KEY_HEADER_SIZE + " VARCHAR,"
        + KEY_HEADER_STYLE + " VARCHAR," + KEY_HEADER_COLOR + " VARCHAR" + ")";


// Scroll table create statement
String CREATE_TABLE_SCROLL = "CREATE TABLE " + TABLE_SCROLL + "("
        + KEY_SCROLL_NAME + " VARCHAR," + KEY_SCROLL_FONT + " VARCHAR," + KEY_SCROLL_SIZE + " VARCHAR,"
        + KEY_SCROLL_STYLE + " VARCHAR," + KEY_SCROLL_COLOR + " VARCHAR," + KEY_SCROLL_SPEED + " VARCHAR"  + ")";


// Website table create statement
String CREATE_TABLE_WEBSITE = "CREATE TABLE " + TABLE_WEBSITE + "("
        + KEY_WEBSITE_NAME + " VARCHAR" + ")";


// Videos table create statement
String CREATE_TABLE_VIDEOS = "CREATE TABLE " + TABLE_VIDEOS + "("
        + KEY_VIDEO_PLAYLIST + " VARCHAR," + KEY_VIDEO_TEMPLATE + " VARCHAR," + KEY_VIDEO_NAME + " VARCHAR"
        + ")";


// Images table create statement
String CREATE_TABLE_IMAGES = "CREATE TABLE " + TABLE_IMAGES + "("
        + KEY_IMAGE_PLAYLIST + " VARCHAR," + KEY_IMAGE_TEMPLATE + " VARCHAR," + KEY_IMAGE_NAME + " VARCHAR,"
        + KEY_IMAGE_ZONE + " VARCHAR" + ")";


// Headers table create statement
String CREATE_TABLE_HEADERS = "CREATE TABLE " + TABLE_HEADERS + "("
        + KEY_HEADER_PLAYLIST + " VARCHAR," + KEY_HEADER_TEMPLATE + " VARCHAR," + KEY_HEADER + " VARCHAR"
        + ")";


@Override
public void onCreate(SQLiteDatabase db) {

    db.setTransactionSuccessful();
    db.endTransaction();  

    db.execSQL("attach database ? as aa", new String[]{"/data/data/com.example.databasecopy/databases/Signage"});
    db.beginTransaction();  

    db.execSQL(CREATE_TABLE_SCHEDULE);
    db.execSQL(CREATE_TABLE_IMAGETIME);
    db.execSQL(CREATE_TABLE_REBOOT);
    db.execSQL(CREATE_TABLE_RSS);
    db.execSQL(CREATE_TABLE_HEADER);
    db.execSQL(CREATE_TABLE_SCROLL);
    db.execSQL(CREATE_TABLE_WEBSITE);
    db.execSQL(CREATE_TABLE_VIDEOS);
    db.execSQL(CREATE_TABLE_IMAGES);
    db.execSQL(CREATE_TABLE_HEADERS);

    db.execSQL("attach database ? as a", new String[]{"/data/data/com.example.databasecopy/databases/TempSignageDBManager"});


    String sql =

            "INSERT INTO (a.Schedule) SELECT * FROM (aa.tempSchedule)";

    db.execSQL(sql);
    db.endTransaction();    
    db.execSQL("DETACH a"); 
    db.execSQL("DETACH aa");

    db.endTransaction();
}   }

MainActivity Class

    01-17 14:45:09.510: E/AndroidRuntime(5508): FATAL EXCEPTION: main
01-17 14:45:09.510: E/AndroidRuntime(5508): Process: com.example.databasecopy, PID: 5508
01-17 14:45:09.510: E/AndroidRuntime(5508): java.lang.IllegalStateException: Cannot perform this operation because there is no current transaction.
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.database.sqlite.SQLiteSession.throwIfNoTransaction(SQLiteSession.java:915)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.database.sqlite.SQLiteSession.endTransaction(SQLiteSession.java:398)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:538)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at com.example.databasecopy.MainActivity$1.onClick(MainActivity.java:70)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.view.View.performClick(View.java:4449)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.view.View$PerformClick.run(View.java:18483)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.os.Handler.handleCallback(Handler.java:733)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.os.Looper.loop(Looper.java:136)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at android.app.ActivityThread.main(ActivityThread.java:5398)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at java.lang.reflect.Method.invokeNative(Native Method)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at java.lang.reflect.Method.invoke(Method.java:515)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
01-17 14:45:09.510: E/AndroidRuntime(5508):     at dalvik.system.NativeStart.main(Native Method)
01-17 14:45:09.581: E/dalvikvm(1834): heapsize Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize 4194304
01-17 14:45:09.581: E/dalvikvm(1834): heapsize dvmMinimumHeapSize 4194304
01-17 14:45:09.581: E/dalvikvm(1834): heapsize dvmMinimumHeapSize set minimumSize 4194304
01-17 14:45:10.051: E/dalvikvm(1834): heapsize Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize 4194304
01-17 14:45:10.051: E/dalvikvm(1834): heapsize dvmMinimumHeapSize 4194304
01-17 14:45:10.051: E/dalvikvm(1834): heapsize dvmMinimumHeapSize set minimumSize 4194304

2 个答案:

答案 0 :(得分:1)

您在同一个对象上调用了两个endTransaction(),这是不允许的,因此IllegalStateException

只需在第一个db.beginTransaction();

之后添加db.endTransaction();即可
db.execSQL(sql);
db.endTransaction();

db.beginTransaction();  
db.execSQL("DETACH a"); 
db.execSQL("DETACH aa");
db.endTransaction();

或者不要打电话给endTransaction()直到这样结束。

db.execSQL(sql);

db.execSQL("DETACH a"); 
db.execSQL("DETACH aa");
db.endTransaction();

此外,您在setTransactionSuccessful()开始时拨打endTransaction()之前致电beginTransaction()onCreate()。删除这些。

请参阅documentation,了解如何正确使用beginTransaction

答案 1 :(得分:0)

SQLiteOpenHelper围绕您的onCreate() / onUpdate()回调包装事务,并且结束该事务会破坏错误处理,因此您不得在onCreate() /内部访问数据库onUpdate()

可以在onConfigure() callback中执行此操作,但是当您需要执行此操作以响应按键时,onCreate() / onUpdate()是错误的开始位置。 只需将复制代码移动到按钮处理程序中即可。