在安卓中使用SQLite,我的数据没有插入到表中。

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

sqlLite in android didnt insert my data to table

问题

我不知道为什么在执行insertGasData之后会返回结果result = -1。在其他类似的结构中,结果应该是!= -1,但这里是= -1。有一个DatabaseHelper类和主类。

public class DatabaseHelper extends SQLiteOpenHelper {
    // ... (省略了其他常量和构造函数)

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table " + TABLE_NAME2 + " (GAS_ID INTEGER PRIMARY KEY AUTOINCREMENT, GAS_DATE TEXT, GAS_MILEAGE TEXT, FUEL_TYPE TEXT, FUEL_COST TEXT, FUEL_VOLUME TEXT)");
    }

    // ... (省略了onUpgrade方法)

    public boolean insertGasData(String date, String mileage, String fuel_type, String fuel_price, String fuel_volume) {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_G_2, date);
        contentValues.put(COL_G_3, mileage);
        contentValues.put(COL_G_4, fuel_type);
        contentValues.put(COL_G_5, fuel_price);
        contentValues.put(COL_G_6, fuel_volume);
        long result = sqLiteDatabase.insert(TABLE_NAME2, null, contentValues);
        if (result == -1)
            return false;
        else return true;
    }
}

这是一个调用方法:

public void AddRefuelrData() {
    bDodaj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // boolean isInsert = myDb.insertGasData(eRefDate.getText().toString(), eRefMileage.getText().toString(), eRefType.getText().toString(), eRefPrice.getText().toString(), eRefVolume.getText().toString());
            boolean isInsert = myDb.insertGasData("dd", "mm", "tt", "pr", "vo");
            if (isInsert == true)
                Toast.makeText(MainActivity.this, "Data inserted", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this, "Data not inserted", Toast.LENGTH_LONG).show();
        }
    });
}

在调用insertGasData方法之后,我得到了返回值false。

英文:

I dont know why i have return result = -1 after execute insertGasData. In other similiar structure the result was != -1, but there is = -1.
There is DatabaseHelper class and main class

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "car.db";
    public static final String TABLE_NAME2 = "car_gas_table";

    public static final String COL_G_1 = "GAS_ID";
    public static final String COL_G_2 = "GAS_DATE";
    public static final String COL_G_3 = "GAS_MILEAGE";
    public static final String COL_G_4 = "FUEL_TYPE";
    public static final String COL_G_5 = "FUEL_COST";
    public static final String COL_G_6 = "FUEL_VOLUME";
   
    public DatabaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table " + TABLE_NAME2 + " (GAS_ID INTEGER PRIMARY KEY AUTOINCREMENT, GAS_DATE TEXT, GAS_MILEAGE TEXT, FUEL_TYPE TEXT, FUEL_COST TEXT, FUEL_VOLUME TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
    }
    
    public boolean insertGasData(String date, String mileage, String fuel_type, String fuel_price, String fuel_volume) {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_G_2, date);
        contentValues.put(COL_G_3, mileage);
        contentValues.put(COL_G_4, fuel_type);
        contentValues.put(COL_G_5, fuel_price);
        contentValues.put(COL_G_6, fuel_volume);
        long result = sqLiteDatabase.insert(TABLE_NAME2, null, contentValues);
        if (result == -1)
            return false;
        else return true;
    }

there is a call method
i use there Strings to eliminate other mistakes but it doesnt help

    public void AddRefuelrData() {
        bDodaj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             //   boolean isInsert = myDb.insertGasData(eRefDate.getText().toString(), eRefMileage.getText().toString(), eRefType.getText().toString(), eRefPrice.getText().toString(), eRefVolume.getText().toString());
                boolean isInsert = myDb.insertGasData("dd","mm", "tt", "pr", "vo");
                if (isInsert == true)
                    Toast.makeText(MainActivity.this, "Data inserted", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(MainActivity.this, "Data not inserted", Toast.LENGTH_LONG).show();
            }
        });
    }

after call method insertGasData i have return false

enter image description here

答案1

得分: 0

你确定表格实际上已经正确创建了吗?
使用设备文件浏览器检查数据库是否已经创建。
这应该有助于缩小问题范围。

英文:

Are you sure the table is actually created properly?
Use Device File Explorer to check if the database is created or not.
that should help with narrowing down the problem.

huangapple
  • 本文由 发表于 2020年8月17日 03:54:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63441362.html
匿名

发表评论

匿名网友

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

确定