在sqlite 3.6.0中,我想更新记录,如果存在或插入。但是我遇到了一个错误
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'
和我的代码块作为follws;
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();
RAW查询成功地在SQLitestudio上工作,但是我遇到了一个错误。我知道SQLite版本3.6.0支持UPSERT事件。我在哪里做错了。我可以正确处理此问题。
您的问题是
我知道SQLite版本3.6.0支持UPSERT事件。
使用INSERT..... ON CONFLICT .... DO....
(称为 UPSERT )的使用仅可从版本3.24.0获得。
尝试使用 UPSERT (do .....),在3.24.0之前的版本中将导致 syntax error at ON
根据
UpSert是引起插入物的插入物的特殊语法添加 如果插入物违反 独特的约束。UPSERT不是标准SQL。在sqlite中升级 遵循PostgreSQL建立的语法。添加了upsert语法 到具有3.24.0版本的SQLite(2018-06-04)。
upsert是一个普通的插入语句,随后是 有关上面显示的冲突条款的特殊条款。
sql,sqlite -upsert
您需要使用3.24.0或更高的SQLite版本或找到替代方法。