gpt4 book ai didi

android - 带约束的 SQL 插入语句

转载 作者:行者123 更新时间:2023-11-30 00:00:58 25 4
gpt4 key购买 nike

我正忙着在 android studio 中编写插入语句,它首先必须检查数据库中是否存在数据,如果不存在,则必须插入它。

    if (name.trim().equals("") || supp.trim().equals("") || range.trim().equals("") || size.equals("") || price.equals(""))
z = "Please enter all fields";
else {
try {
con = connectionclass(); // Connect to database
if (con == null) {
z = "Check Your Internet Access!";
} else {
String query = "BEGIN" +
"IF NOT EXISTS(Select * from cc_lining)" +
"BEGIN " +
"INSERT INTO cc_lining (lining_id,lining_name, lining_range, lining_supplier, lining_width, lining_unit_price) VALUES ('" + id.toString() + "','" + name.toString() + "','" + range.toString() + "','" + supp.toString() + "','" + size.toString() + "','" + price.toString() + "')" +
"END" +
"END";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
isSuccess = true;
con.close();
} else {
z = "Invalid!";
isSuccess = false;
}
}
} catch (Exception ex) {
isSuccess = false;
z = ex.getMessage();
}
}
return z;

当我运行代码时,我收到一条错误消息:Incorrect Syntex near NOT

最佳答案

Incorrect Syntex near NOT

逻辑

首先运行关于主键的SELECT语句,比如lining_id

演示

 Cursor cursor = null;
String sql ="SELECT * FROM cc_lining WHERE lining_id=" + id + ";
cursor= db.rawQuery(sql,null);

if(cursor.getCount()>0){
// Data Fount. Query for UPDATE
}else{
// Data Not Found. You should INSERT your Record.

}
cursor.close();

关于android - 带约束的 SQL 插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49917710/

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