使用select(android sqlite)从一个表插入另一个表

时间:2016-04-20 19:40:15

标签: android mysql sqlite

我有两个表具有完全相同的列,除了第二个表有一些额外的列。

我想复制所有数据,除了第一个表中的_id列到第二个。两个表中列的名称完全相同。

这是第一张表

private static final String TABLE_EXPENSES_CREATE = "create table " + TABLE_EXPENSES + " (" +
      COLUMN_ID + " integer primary key autoincrement, " +
      COLUMN_DATE + " integer not null, " +
      COLUMN_AMOUNT + " real not null, " +
      COLUMN_DESCRIPTION + " text, " +
      COLUMN_REPEAT_ID + " integer," +
      " FOREIGN KEY (" + COLUMN_REPEAT_ID + ") REFERENCES " +
      TABLE_REPEAT_METADATA + "(" + COLUMN_ID + ")" +
      ");";

和第二个

 private static final String TABLE_TRANSACTIONS_CREATE =
      "create table " + TABLE_TRANSACTIONS + " (" +
          COLUMN_ID + " integer primary key autoincrement, " +
          COLUMN_DATE + " integer not null, " +
          COLUMN_AMOUNT + " real not null, " +
          COLUMN_DESCRIPTION + " text, " +
          COLUMN_TYPE + " text not null, " +
          COLUMN_REPEAT_ID + " integer," +
          " FOREIGN KEY (" + COLUMN_REPEAT_ID + ") REFERENCES " +
          TABLE_REPEAT_METADATA + "(" + COLUMN_ID + ")" +
          ");";

我正在尝试使用以下代码将第一个表格中的所有内容(COLUMN_ID除外)复制到第二个表格:

db.rawQuery("insert into "
          + MySQLiteHelper.TABLE_TRANSACTIONS
          + " ("
          + COLUMN_DATE
          + ", "
          + COLUMN_AMOUNT
          + ", "
          + COLUMN_DESCRIPTION
          + ", "
          + COLUMN_REPEAT_ID
          + ") select "
          + COLUMN_DATE
          + ", "
          + COLUMN_AMOUNT
          + ", "
          + COLUMN_DESCRIPTION
          + ", "
          + COLUMN_REPEAT_ID
          + " from "
          + MySQLiteHelper.TABLE_EXPENSES, null);

然而,一旦代码运行就没有效果。日志不显示任何错误。我究竟做错了什么?

1 个答案:

答案 0 :(得分:1)

如果您想要返回一些数据( appClient = ARDAppClient(delegate: self) appClient?.createLocalMediaStream() appClient?.connectToRoomWithId(String(roomId), options: nil) ),则应使用https://perishablepress.com/jquery-accordion-menu-tutorial/
出于其他目的,请考虑使用rawQuery

在您的情况下,如果将Cursor替换为db.rawQuery("insert into "...,它将起作用!


P.S。对于db.execSQL("insert into " ...,您还可以阅读有趣的内容:
“SQL字符串不得为;终止”。
几天前我对此感到很头疼;)