0

我从 Logcat 收到以下错误堆栈跟踪:

 SqliteDatabaseCpp(1355): sqlite returned: error code = 1

 msg = table my_table has no column named check_box_status,
 db=/data/data/com.example.dbtester/databases/my_database

 Error inserting check_box_status=1 price=210 check_box_label=testlabel

 table my_table has no column named check_box_status: , while compiling:
 INSERT INTO my_table(check_box_status,price,check_box_label) VALUES (?,?,?)

我猜这与下面显示的 insert() 方法的中间参数为空有关,

  public void insertNewRow(int checkBoxStatus, String checkBoxLabel, int price){
          ContentValues contentValues = new ContentValues();
          contentValues.put(KEY_CHECKBOX_STATUS, checkBoxStatus);
          contentValues.put(KEY_CHECKBOX_LABEL, checkBoxLabel);
          contentValues.put(KEY_PRICE, price);
          sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); // <---
          }

我为 sqLiteDatabase.insert() 的中间参数值设置了 null,但是我收到错误和上面显示的堆栈跟踪的原因可能是该变量不应该为 null。

从下面显示的信息中,我应该在那个现在为空的中间参数变量中放入什么?sqLiteDatabase.insert(MYDATABASE_TABLE, ?, contentValues);

请注意,我在用于创建表的字符串中明确声明了一个主键“ID INTEGER PRIMARY KEY AUTOINCREMENT”,我认为这与错误有关。

数据库初始化代码:

    public static final String MYDATABASE_NAME = "my_database";
    public static final String MYDATABASE_TABLE = "my_table";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_CHECKBOX_STATUS = "check_box_status";
    public static final String KEY_CHECKBOX_LABEL = "check_box_label";
    public static final String KEY_PRICE = "price"; 

  //create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE =
     "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
    "KEY_CHECKBOX_STATUS INTEGER, " + "KEY_CHECKBOX_LABEL TEXT, " + " KEY_PRICE INTEGER" + ");";

    SQLiteDatabase sqLiteDatabase;
    SQLiteHelper sqLiteHelper;
    Context context;

    public Database(Context c){
          context = c;
          sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
         }
4

3 回答 3

2

在您的创建 SQL 中,您将KEY_CHECKBOX_STATUS作为列名而不是值 ( check_box_status)..这就是为什么告诉table my_table has no column named check_box_status

所以创建应该是:

private static final String SCRIPT_CREATE_DATABASE =
 "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
KEY_CHECKBOX_STATUS + " INTEGER, " + KEY_CHECKBOX_LABEL +" TEXT, " +  KEY_PRICE +" INTEGER" + ");";
于 2013-04-12T07:53:14.453 回答
2

您的 Create Table 语法错误,因为您在引号中传递变量名称。所以像这样改变你的创建表

  "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
            KEY_CHECKBOX_STATUS+" INTEGER, " + KEY_CHECKBOX_LABEL+" TEXT, " +  KEY_PRICE +" INTEGER" + ");";
于 2013-04-12T07:57:28.803 回答
1

改变

private static final String SCRIPT_CREATE_DATABASE =
 "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + " (" + "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  
KEY_CHECKBOX_STATUS + " INTEGER, " + KEY_CHECKBOX_LABEL + " TEXT, " +  KEY_PRICE + " INTEGER);";
于 2013-04-12T07:57:40.580 回答