gpt4 book ai didi

android - 无法执行此操作,因为在Android中复制多个文件时出现没有当前事务

转载 作者:行者123 更新时间:2023-11-29 19:24:28 27 4
gpt4 key购买 nike

我是 SQLite 和 Android 的初学者。我需要将一个数据库(例如:DB1)的表复制到另一个数据库(例如:DB2)。根据我的代码,一个数据库和相关表是在创建时创建的。当按下按钮时,我需要创建另一个表并将数据从一个表复制到另一个表。两个表都在创建,但数据没有复制。我没有得到当前事务出现在 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();
} }

日志

    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

主要 Activity 类

public class MainActivity extends Activity  {
private DatabaseHandler dBHelper;

SQLiteDatabase SQLdb;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.button1);

SQLdb = openOrCreateDatabase("TempSignage",Context.MODE_PRIVATE, null);
SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempSchedule(StartTime VARCHAR,EndTime VARCHAR,StartDate VARCHAR,EndDate VARCHAR,Template VARCHAR,PlayList VARCHAR);");
SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempImageTime(FilePath VARCHAR,Time VARCHAR);");
SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempReboot(Time VARCHAR);");

SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempHeader(thname VARCHAR,thfont VARCHAR,thsize VARCHAR,thstyle VARCHAR,thcolor VARCHAR);");
SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempScroll(txname VARCHAR,txfont VARCHAR,txsize VARCHAR,txstyle VARCHAR,txcolor VARCHAR,txspeed VARCHAR);");
SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempWebsite(wname VARCHAR);");

SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempVideos(PlayList VARCHAR,Template VARCHAR,VideoName VARCHAR);");
SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempImages(PlayList VARCHAR,Template VARCHAR,ImageName VARCHAR,ImageZone VARCHAR);");
SQLdb.execSQL("CREATE TABLE IF NOT EXISTS tempHeaders(PlayList VARCHAR,Template VARCHAR,Header VARCHAR);");


SQLdb.execSQL("insert into tempSchedule values('er','erer','4t','rt','ere','feef')");
SQLdb.execSQL("insert into tempImageTime values('2sfsf','444')");
SQLdb.execSQL("insert into tempReboot values('343434')");

SQLdb.execSQL("insert into tempHeader values('sdd','de','ssert','sdsd','ererer')");
SQLdb.execSQL("insert into tempScroll values('5','fg','45','jyj','tyty','55')");
SQLdb.execSQL("insert into tempWebsite values('sdsdds')");


SQLdb.execSQL("insert into tempVideos values('sdwr','efefe','rtrt')");
SQLdb.execSQL("insert into tempImages values('yyyy','hiiioo','ukjf','oioior')");
SQLdb.execSQL("insert into tempHeaders values('6','eete','ftytf')");


SQLdb.execSQL("insert into tempWebsite values('6','efefeef','rtrt')");*/

btn.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {
dBHelper = new DatabaseHandler(getBaseContext());
dBHelper.getWritableDatabase();


}
});
}}

最佳答案

您正在调用两个连续的 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适本地。

关于android - 无法执行此操作,因为在Android中复制多个文件时出现没有当前事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693036/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com