使用长总是返回-1

时间:2019-03-27 21:10:38

标签: java android android-sqlite

我无法找到代码中缺少的内容。我正在尝试插入数据,但是“ 长结果== db.insert ”在我的DatabaseHelper类中始终返回-1”。我无法指出我在做什么错。请帮助。任何想法将不胜感激。

我的DatabaseHelper类:

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "drivefinal.db";
public static final String TABLE_NAME = "drive_table";
public static final String ID = "ID";
public static final String NUMBER = "Number";
public static final String FNAME = "First Name";
public static final String LNAME = "Last Name";
public static final String COORDINATE = "Coordinate";
public static final String ADDRESS = "Address";
public static final String NOTES = "Notes";

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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table if not exists " + TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT ,NUMBER TEXT,FNAME TEXT,LNAME TEXT,COORDINATE TEXT,ADDRESS TEXT,NOTES TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String number, String fName, String lName, String coordinate, String address, String notes){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(NUMBER, number);
    contentValues.put(FNAME, fName);
    contentValues.put(LNAME, lName);
    contentValues.put(COORDINATE, coordinate);
    contentValues.put(ADDRESS, address);
    contentValues.put(NOTES, notes);

    long result = db.insert(TABLE_NAME,null ,contentValues);
        if(result == -1 )
            return false;
        else
            return true;
}

我的主要活动文件:

public void addData(){
    btnSave.setOnClickListener(
        new View.OnClickListener(){
            @Override
            public void onClick(View v){
                boolean isInserted = myDb.insertData(editTextnumber.getText().toString(),
                        editTextfname.getText().toString(),
                        editTextlname.getText().toString(),
                        editTextcoordinate.getText().toString(),
                        editTextaddress.getText().toString(),
                        editTextnotes.getText().toString());

                        if(isInserted)
                            Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
            }
        }

        );
}

}

1 个答案:

答案 0 :(得分:1)

您的表定义创建的列名与您要在插入时尝试使用的列名不同。

例如您的表中的列名称为 FNAME ,而不是名字(依此类推)。

第二,您会遇到尝试使用名为 Firstname 的列的问题,因为该列包含空格,必须将其括起来,例如 [名字]

我建议使用:-

public static final String NUMBER = "Number";
public static final String FNAME = "FirstName"; //<<<<<<<<<< space removed
public static final String LNAME = "LastName"; //<<<<<<<<<< space removed
public static final String COORDINATE = "Coordinate";
public static final String ADDRESS = "Address";
public static final String NOTES = "Notes";

伴随:-

db.execSQL("create table if not exists " + TABLE_NAME +"(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + NUMBER + " TEXT," + FNAME + " TEXT," + LNAME + " TEXT," + COORDINATE + " TEXT," + ADDRESS + " TEXT," + NOTES + " TEXT)");

要引入更改(例如,要运行onCreate方法,因为它仅在创建数据库时自动运行),您必须执行以下操作之一:-

  • 删除应用程序的数据。
  • 卸载应用。
  • 更改数据库版本号,例如将super(context, DATABASE_NAME, null, 1);更改为super(context, DATABASE_NAME, null, 2);

,然后重新运行该应用程序。