android - SQLite insert No such column error on android

标签 android database sqlite error-handling

我在从 CSV 插入数据时遇到问题。 CSV 部分有效,但插入返回以下错误。基本上它说不存在该列。下面是代码,也是数据库的代码。

09-06 17:52:22.725: E/AndroidRuntime(19168): FATAL EXCEPTION: main

09-06 17:52:22.725: E/AndroidRuntime(19168): android.database.sqlite.
SQLiteException: no such column: Ananas (code 1): , while compiling: 
INSERT INTO food (name, sacharides, glycemia, category1) VALUES    
(Ananas, 13, 45,1)

09-06 17:52:22.725: E/AndroidRuntime(19168):    
at android.database.sqlite.SQLiteConnection.nativePrepareStatement
(Native Method)

这是失败的代码。

            FoodDatabase myHelper = new FoodDatabase(getApplicationContext());
            myDatabase = myHelper.getReadableDatabase();

            String[] nextLine;
            try {
                while ((nextLine = reader.readNext()) != null) {
                    // nextLine[] is an array of values from the line
                    System.out.println(nextLine[0]+ " " + nextLine[1]
                            +" "+ nextLine[2]);

                    String sql =   "INSERT INTO food (name, sacharides, glycemia, category1) " +
                            "VALUES (" + nextLine[0] + ", " + nextLine[1] + ", "  + nextLine[2]+ "," + nextLine[3] +")";
                //  myDatabase.rawQuery(sql, null);
                    myDatabase.execSQL(sql);
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

这是数据库的样子。

public class FoodDatabase extends SQLiteOpenHelper {

  public static final String TABLE_FOOD = "food";
  public static final String COLUMN_ID = "id";
  public static final String COLUMN_NAME = "name";
  public static final String COLUMN_SACHARIDES = "sacharides";
  public static final String COLUMN_SACHARIDESPORTION = "sacharides_per_portion";
  public static final String COLUMN_PORTIONSIZE = "portion_size";
  public static final String COLUMN_GLYCEMIA = "glycemia";
  public static final String COLUMN_CATEGORY1 = "category1";
  public static final String COLUMN_CATEGORY2 = "category2";
  public static final String COLUMN_CATEGORY3 = "category3";




  private static final String DATABASE_NAME = "tables.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_FOOD + "(" + COLUMN_ID
      + " integer primary key autoincrement, " + COLUMN_NAME
      + " text," + COLUMN_SACHARIDES
      + " real," + COLUMN_PORTIONSIZE
      + " integer," + COLUMN_SACHARIDESPORTION
      + " real," + COLUMN_GLYCEMIA
      + " integer,"+ COLUMN_CATEGORY1
      + " integer," +COLUMN_CATEGORY2
      + " integer," +COLUMN_CATEGORY3
      + " integer);";

  public FoodDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(FoodDatabase.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOOD);
    onCreate(db);
  }

最佳答案

我不确定,但您可能需要在 VALUES 内的字符串周围加上单引号 ''。 尝试:

String sql = "INSERT INTO food (name, sacharides, glycemia, category1) " +
                            "VALUES ('" + nextLine[0] + "', '" + nextLine[1] + "', '"  + nextLine[2]+ "', '" + nextLine[3] +"' )";

关于android - SQLite insert No such column error on android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18661898/

相关文章:

Android Studio 不允许我导入 SDK

mysql - DC2Type :array comment being added to field when doing a Doctrine migration diff

ruby-on-rails - 在列中存储版本号(例如 1、1.1、1.11、1.11.1)的最佳方法?

php - PHP到sqlite3-无法上传

Android GPS 应用程序未关闭连接

php - 我如何使用改造上传多个文件

java - 这是什么 : while ACTION_MOVE allow for a new ACTION_DOWN?

mysql - NodeJS - 连接到本地 Mysql 数据库

java - 错误显示我的 sqlite 数据库表中不存在特定列

php - 使用 PDO 以只读方式打开 SQLite3?