java.sql.SQLException: near "on": 语法错误问题

java.sql.SQLException: near "on": syntax error problem

在 Sqlite 3.6.0 中,我想更新记录(如果存在)或 insert.But 我收到一个错误

java.sql.SQLException: near "on": syntax error

我的sql查询如下;

INSERT INTO tx (
               _id,
               amount,
               fee,
               prev_hash,
               nonce,
               action_time,
               completion_time,
               _from,
               _to,
               asset,
               hash,
               block,
               seq,
               [desc]
           )
           VALUES (
               ?,
               ?,
               ?,
               ?,
               ?,
               ?,
               ?,
               ?,
               ?,
               ?,
               ?,
               ?,
               ?,
               ?
           )
           ON CONFLICT (
               _id
           )
           DO UPDATE SET amount = 1.0,
           fee = 0.001,
           prev_hash = 'e6d0ca4b71488972cb149eef427ffbeb6132449c045c88db89ddaa8ab0c3611f',
           nonce = 1544773280,
           action_time = 1544773276808,
           completion_time = 1544773276808,
           _from = 'SKK1P5eQqoN7FBKnughbvp3UBK2CyjRQhHBp',
           _to = 'SKK1KuAwe4kR1SfdoXDiQStVtRPvdTVaKy2ry',
           asset = 'SKK',
           hash = '',
           block = 100,
           seq = 15288,
           [desc] = 'denemee'

我的代码块如下;

sql = "insert into tx (_id,amount,fee,prev_hash,nonce,action_time,completion_time,_from,_to,asset,hash,block,seq,desc) "
                                    + "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?) on conflict (_id) do update set "
                                    + "amount =" + tx.amount + ",fee=" + tx.fee + ",prev_hash='" + tx.prev_hash
                                    + "'" + ",nonce=" + tx.nonce + ",action_time=" + tx.action_time
                                    + ",completion_time=" + tx.action_time + ",_from='" + tx.wallet + "'" + ",_to='"
                                    + tx.to + "'" + ",asset='" + tx.asset + "'" + ",hash=''" + ",block=100"
                                    + ",seq=" + tx.seq + ",desc='" + tx.desc + "'";
                            PreparedStatement pstmt = con.prepareStatement(sql);
                            pstmt.setString(1, tx._id);
                            pstmt.setString(2, String.valueOf(tx.amount));
                            pstmt.setString(3, String.valueOf(tx.fee));// fee
                            pstmt.setString(4, tx.prev_hash);// prev_hash
                            pstmt.setString(5, tx.nonce);// nonce
                            pstmt.setString(6, String.valueOf(tx.action_time));// action time
                            pstmt.setString(7, "");// completion_time
                            pstmt.setString(8, tx.wallet);// from
                            pstmt.setString(9, tx.to); // to
                            pstmt.setString(10, tx.asset);// asset
                            pstmt.setString(11, tx.hash);// hash
                            pstmt.setString(12, "");// block
                            pstmt.setInt(13, tx.seq);// seq
                            pstmt.setString(14, tx.desc);// desc
                            pstmt.executeUpdate();

原始查询在 sqlitestudio 成功运行,但我得到一个 error.I 知道 sqlite 版本 3.6.0 支持更新插入 event.Where 我在做什么wrong.How我能不能妥善处理这个问题

您的问题是由于

I know that sqlite version 3.6.0 supports upsert event.

INSERT..... ON CONFLICT .... DO....(称为 UPSERT)的使用仅在 3.24.0 版本中可用。

尝试使用 UPSERT (DO.....),在 3.24.0 之前的版本中将导致 syntax error at ON

根据

UPSERT is a special syntax addition to INSERT that causes the INSERT to behave as an UPDATE or a no-op if the INSERT would violate a uniqueness constraint. UPSERT is not standard SQL. UPSERT in SQLite follows the syntax established by PostgreSQL. UPSERT syntax was added to SQLite with version 3.24.0 (2018-06-04).

An UPSERT is an ordinary INSERT statement that is followed by the special ON CONFLICT clause shown above.

SQL As Understood By SQLite - upsert

您需要使用 3.24.0 或更高版本的 SQLite,或者寻找替代方法。