无法使用(开始交易)方法删除SQlite列

时间:2019-01-13 05:38:04

标签: android android-studio sqlite android-sqlite

在Android Studio SQlite数据库中,我有一个表“ title”,其中有两列(名称,id),我想删除id列,我使用了Transaction方法,但它不起作用。 将名称列复制到新表后,我希望新表应具有其自身的id列。

     try {
            SQLiteDatabase sqLiteDatabase = this.openOrCreateDatabase("db", MODE_PRIVATE, null);

    sqLiteDatabase.execSQL("BEGIN TRANSACTION");
            sqLiteDatabase.execSQL("CREATE TEMPORARY TABLE t1(name VARCHAR)");
            sqLiteDatabase.execSQL("INSERT INTO t1 SELECT name FROM title");
            sqLiteDatabase.execSQL("DROP TABLE title");
            sqLiteDatabase.execSQL("CREATE TABLE title (name VARCHAR, id INTEGER PRIMARY KEY)");
            sqLiteDatabase.execSQL("INSERT INTO title SELECT name FROM t1");
            sqLiteDatabase.execSQL("DROP TABLE t1");
    sqLiteDatabase.execSQL("COMMIT");

           } catch (Exception e) {

        }

我也尝试过

 try {
            SQLiteDatabase sqLiteDatabase = this.openOrCreateDatabase("db", MODE_PRIVATE, null);

    sqLiteDatabase.execSQL("BEGIN TRANSACTION");
            sqLiteDatabase.execSQL("CREATE TEMPORARY TABLE t1(name VARCHAR,id INTEGER PRIMARY KEY)");
            sqLiteDatabase.execSQL("INSERT INTO t1 SELECT name FROM title");
            sqLiteDatabase.execSQL("DROP TABLE title");
            sqLiteDatabase.execSQL("CREATE TABLE title (name VARCHAR, id INTEGER PRIMARY KEY)");
            sqLiteDatabase.execSQL("INSERT INTO title SELECT name,id FROM t1");
            sqLiteDatabase.execSQL("DROP TABLE t1");
    sqLiteDatabase.execSQL("COMMIT");

           } catch (Exception e) {

        }

1 个答案:

答案 0 :(得分:1)

您的问题是由于捕获的异常:-

def make_plots(self):
    p1 = self.make_plot(...
    p2 = self.make_plot(...
    p3 = self.make_plot(...
    p4 = self.make_plot(...

    c1 = MakeControls(self)

    tab1 = Panel(child=p1, title=self.type_labels[1])
    tab2 = Panel(child=p2, title=self.type_labels[2])
    tab3 = Panel(child=p3, title=self.type_labels[3])
    tab4 = Panel(child=p4, title=self.type_labels[4])
    tabs = Tabs(tabs=[tab1, tab2, tab3, tab4])

    p = Column(c1,tabs)

    return p

即您没有为 id 列提供值。

更改为使用

SQLiteException: table title has 2 columns but 1 values were supplied (code 1 SQLITE_ERROR): , while compiling: INSERT INTO title SELECT name FROM t1

sqLiteDatabase.execSQL("INSERT INTO title SELECT name, null FROM t1");