db.insert()在android中返回-1,虽然我觉得可以写得正确

时间:2016-01-09 05:46:08

标签: android android-sqlite

我看过无数的教程和之前提出的问题,我觉得我的代码应该可行。但是,每当我去插入时,db.insert都会返回错误代码-1。

数据库合同

public final class DatabaseContract {


private static final String TEXT_TYPE          = " TEXT not null";
private static final String TEXT_TYPE_NULL     = "TEXT";
private static final String INT_TYPE           = "INTEGER not null";
private static final String INT_TYPE_NULL           = "INTEGER";
private static final String COMMA_SEP          = ",";

// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
private DatabaseContract() {}

public static abstract class Client implements BaseColumns {
    public static final String TABLE_NAME       = "client";
    public static final String COLUMN_NAME_COL1 = "fullname";
    public static final String COLUMN_NAME_COL2 = "phone";
    public static final String COLUMN_NAME_COL3 = "photo";


    public static final String CREATE_TABLE = "CREATE TABLE " +
            TABLE_NAME + " (" +
            _ID + " INTEGER PRIMARY KEY autoincrement," +
            COLUMN_NAME_COL1 + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_COL2 + INT_TYPE + COMMA_SEP +
            COLUMN_NAME_COL3 + TEXT_TYPE + " )";
    public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
}

public static abstract class Drink implements BaseColumns {
    public static final String TABLE_NAME       = "drink";
    public static final String COLUMN_NAME_COL1 = "name";
    public static final String COLUMN_NAME_COL2 = "desc";
    public static final String COLUMN_NAME_COL3 = "upc";
    public static final String COLUMN_NAME_COL4 = "price";


    public static final String CREATE_TABLE = "CREATE TABLE " +
            TABLE_NAME + " (" +
            _ID + " INTEGER PRIMARY KEY autoincrement," +
            COLUMN_NAME_COL1 + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_COL2 + TEXT_TYPE_NULL + COMMA_SEP +
            COLUMN_NAME_COL3 + INT_TYPE_NULL + COMMA_SEP +
            COLUMN_NAME_COL4 + INT_TYPE +" )";
    public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
}

}

DatabaseHelper

public class DatabaseHelper extends SQLiteOpenHelper {
public static final  int    DATABASE_VERSION   = 1;
public static final  String DATABASE_NAME      = "drinkpos.db";

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

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DatabaseContract.Client.CREATE_TABLE);
    db.execSQL(DatabaseContract.Drink.CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

mainActivity

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



    DatabaseHelper dbHelper = new DatabaseHelper(this);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(DatabaseContract.Drink.COLUMN_NAME_COL1, "fanta");
    values.put(DatabaseContract.Drink.COLUMN_NAME_COL2, "orange");
    values.put(DatabaseContract.Drink.COLUMN_NAME_COL3, "100141166");
    values.put(DatabaseContract.Drink.COLUMN_NAME_COL4, "100");
    // Insert the new row, returning the primary key value of the new row
    long newRowId;
    newRowId = db.insert(
            DatabaseContract.Drink.TABLE_NAME,
            null,
            values);

我尝试删除任何不必要的代码。 在运行上面的代码之后,我尝试从数据库中读取,并且它告诉我没有这样的第三列(即在客户表的情况下饮料或照片的情况下的desc)。

非常感谢任何想法。

1 个答案:

答案 0 :(得分:0)

private static final String TEXT_TYPE          = " TEXT not null";
private static final String TEXT_TYPE_NULL     = "TEXT";
private static final String INT_TYPE           = "INTEGER not null";
private static final String INT_TYPE_NULL           = "INTEGER";

您在TEXT_TYPE_NULLINT_TYPEINT_TYPE_NULL前面缺少空格。您声明的列

COLUMN_NAME_COL2 + INT_TYPE

获取列名称和类型连接在一起,如descINTEGER not null,当然不会成为列desc

解决问题后,您可以卸载应用以重新创建数据库。