SQLite Android Studio 插入时出现无此列错误。

huangapple go评论86阅读模式
英文:

No such column error while inserting in SQLite Android Studio

问题

ERROR:
tableMon has no column named notes in "INSERT INTO tableMon (notes) VALUES ( notes=hello )"

错误:
tableMon 表中没有名为 notes 的列在 "INSERT INTO tableMon (notes) VALUES ( notes=hello )" 中。

英文:

CODE:

//creating an sqlite query and setting column names 
along with data types.
String query = "CREATE TABLE tableMon (notes TEXT)";

//calling a exec sql method to execute sql query
db.execSQL(query);

//after adding....passing content values to the table
String insert = "INSERT INTO tableMon (notes) VALUES ( 
" + values + " )";
db.execSQL(insert);

ERROR:
tableMon has no column named notes in "INSERT INTO tableMon (notes) VALUES ( notes=hello )"

I have tried adding and removing spaces near the column name and adding variables instead direct use of table name and column name.
Though! didn't get any expected result.

答案1

得分: -1

你的问题是要插入的值是非数值的,因此必须用单引号括起来。所以:

String insert = "INSERT INTO tableMon (notes) VALUES ('" + values + "' )";

然而,上面的代码容易受到SQL注入攻击,所以最好使用:

String insert = "INSERT INTO tableMon (notes) VALUES (?)";
db.execSQL(insert, new String[]{values});

在这种情况下,当SQLite绑定时,值将适当地括起来。

另一种方法是使用SQLiteDatabse的insert方法。这有以下优点:

  • 它构建底层的SQL语句,
  • 绑定值,
  • 并返回插入行的rowid,如果无法插入行则返回-1。

在你的情况下,你可以这样使用:

ContentValues cv = new ContentValues();
cv.put("notes", values);
long result = db.insert("tableMon", null, cv);

以下是一个可工作的演示:

public class MainActivity extends AppCompatActivity {
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("example.db").getPath(), null);
        db.execSQL("CREATE TABLE IF NOT EXISTS tableMon (notes TEXT)");
        String values = "notes=hello";
        db.execSQL("INSERT INTO tableMon (notes) VALUES('" + values + "')");
        db.execSQL("INSERT INTO tableMon (notes) VALUES(?)", new String[]{values});
        ContentValues cv = new ContentValues();
        cv.put("notes", values);
        db.insert("tableMon", null, cv);
    }
}

请注意使用了CREATE TABLE IF NOT EXISTS,如果表已经存在,它将避免错误。

结果,通过Android Studio的App Inspection,当安装和运行(第一次运行,后续运行将添加另一组3行):

SQLite Android Studio 插入时出现无此列错误。

即使用每个修复插入了3行。

英文:

Your issue is that the value being inserted is non-numeric and thus must be enclosed in single quotes. So:-

String insert = "INSERT INTO tableMon (notes) VALUES ('" + values + "' )";

However, the above code is susceptible to SQL Injection So it would be better to use:-

String insert = "INSERT INTO tableMon (notes) VALUES (?)";
db.execSQL(insert,new String[]{values};

In this case the value will be suitable enclosed when it is bound by SQLite binding.

However, another approach is to use the SQLiteDatabse insert method. This has the advantages that

  • it builds the underlying SQL, and
  • binds values,
  • and returns the rowid of the inserted row or -1 if the row could not be inserted.

In your case you could use:-

ContentValues cv = new ContentValues();
cv.put("notes",values);
long result = db.insert("tableMon",null,cv);

Here is a working demo:-

public class MainActivity extends AppCompatActivity {
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("example.db").getPath(),null);
        db.execSQL("CREATE TABLE IF NOT EXISTS tableMon (notes TEXT)");
        String values = "notes=hello";
        db.execSQL("INSERT INTO tableMon (notes) VALUES('" + values + "')");
        db.execSQL("INSERT INTO tableMon (notes) VALUES(?)",new String[]{values});
        ContentValues cv = new ContentValues();
        cv.put("notes",values);
        db.insert("tableMon",null,cv);
    }
}
  • Note the use of CREATE TABLE IF NOT EXISTS, which will circumvent the error if the table already exists.

The result, via Android Studio's App Inspection ,when installed and run (first time, subsequent runs would add another set of 3 rows):-

SQLite Android Studio 插入时出现无此列错误。

i.e. 3 rows have been inserted using each of the fixes.

huangapple
  • 本文由 发表于 2023年2月19日 02:33:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定