SQLite插入数据失败

时间:2017-04-23 17:28:41

标签: android sqlite android-sqlite

我已经创建了一个日志和注册的数据库,并且工作正常,但是当我创建数据库以添加收入时我没有错误只返回toast消息说失败,我不能理解为什么因为我通过正确的变量通过我将附上以下代码。

DatabseHelper代码insertIncome是一个值得插入到此处的类是代码:public class DatabaseHelper extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 16;

    //Database Name
    private static final String DATABASE_NAME = "User.db";

    //Table Created
    private static final String TABLE_USER = "user";

    //Columns in Table
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_PASS = "password";
    SQLiteDatabase db;

    private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "(" + COLUMN_ID + " INTEGER PRIMARY KEY, " + COLUMN_USERNAME + " TEXT, " +
            COLUMN_EMAIL + " TEXT, " + COLUMN_PASS + " TEXT " + ")";

    private String DROP_USER_TABLE = " DROP TABLE IF EXISTS " + TABLE_USER;
    //table created
    private static final String TABLE_INCOME = "income";
    //columns in table
    private static final String COL1 = "id";
    private static final String COL2 = "iAmount";
    private static final String COL3 = "iMethod";
    private static final String COL4 = "date";

    private String CREATE_INCOME_TABLE = "CREATE TABLE " + TABLE_INCOME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " FIRSTNAME TEXT, LASTNAME TEXT, FAVFOOD TEXT)";

    private String DROP_INCOME_TABLE = " DROP TABLE IF EXISTS " + TABLE_INCOME;

    //table created
    private static final String TABLE_EXPENSE = "expense";
    //columns in table
    private static final String COLUMN_EID = "id";
    private static final String COLUMN_EAMOUNT = "eAmount";
    private static final String COLUMN_EMETHOD = "eMethod";
    private static final String COLUMN_EDATE = "date";

    private String CREATE_EXPENSE_TABLE = "CREATE TABLE " + TABLE_EXPENSE + "(" + COLUMN_EID + " INTEGER PRIMARY KEY, " + COLUMN_EAMOUNT + " TEXT, " +
            COLUMN_EMETHOD + " TEXT, " + COLUMN_EDATE + "TEXT " + ")";

    private String DROP_EXPENSE_TABLE = " DROP TABLE IF EXISTS " + TABLE_EXPENSE;



    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_USER_TABLE);
        db.execSQL(CREATE_INCOME_TABLE);
        db.execSQL(CREATE_EXPENSE_TABLE);
        this.db = db;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       db.execSQL(DROP_USER_TABLE);
        db.execSQL(DROP_INCOME_TABLE);
        db.execSQL(DROP_EXPENSE_TABLE);
        this.onCreate(db);

    }

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

    public void insertUser(User user){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        String query = "select * from " + TABLE_USER;
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();
        values.put(COLUMN_ID, count);
        values.put(COLUMN_USERNAME, user.getUsername());
        values.put(COLUMN_EMAIL, user.getEmail());
        values.put(COLUMN_PASS, user.getPassword());

        db.insert(TABLE_USER, null, values);
        db.close();
    }
    public boolean insertIncome(int iAmount, String iMethod, String iDate) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, iAmount);
        contentValues.put(COL3, iMethod);
        contentValues.put(COL4, iDate);

        long result = db.insert(TABLE_INCOME, null, contentValues);

        if(result == -1){
            return false;
        }else {
            return true;
        }
    }

    public void insertExpense(Income income){


    }
    public String checkUser(String username){
        db = this.getReadableDatabase();
        String query = "SELECT username, password from user";
        Cursor cursor = db.rawQuery(query, null);

        String a, b;
        b = "error";
        if(cursor.moveToFirst()){
            do{
                a = cursor.getString(0);


                if(a.equals(username)){
                    b = cursor.getString(1);
                    break;
                }
            }while(cursor.moveToNext());
        }

        return b;

    }

    public Cursor getList(){
        db = this.getWritableDatabase();
        Cursor query = db.rawQuery("SELECT * FROM " + TABLE_INCOME, null);
        return query;
    }




}

模型类:

公共课收入{

    public int iAmount;
    public String iMethod;
    public String iDate;

    public Income(int iAmount, String iMethod, String iDate) {
        iAmount = iAmount;
        iMethod = iMethod;
        iDate = iDate;
    }



    public int getiAmount() {
        return iAmount;
    }

    public void setiAmount(int incAmount) {
        this.iAmount = incAmount;
    }

    public String getiMethod() {
        return iMethod;
    }

    public void setiMethod(String iMethod) {
        this.iMethod = iMethod;
    }

    public String getiDate() {
        return iDate;
    }

    public void setDate(String iDate) {
        iDate = iDate;
    }






}

尝试插入数据的类:

public class incomePlus extends AppCompatActivity {

    public TextView transactionIncome;
    public Button incomeSubmit;
    public EditText incomeAmount;
    public EditText incomeDate;
    public Spinner Category;
    DatabaseHelper db = new DatabaseHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_plus);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        transactionIncome = (TextView) findViewById(R.id.transactionIncome);
        incomeSubmit = (Button) findViewById(R.id.incomeSubmit);
        incomeAmount = (EditText)findViewById(R.id.incomeAmount);
        incomeDate = (EditText) findViewById(R.id.incomeDate);
        Category = (Spinner) findViewById(R.id.Category);

        List<String> incomeCat = new ArrayList<String>();
        incomeCat.add("Salary");
        incomeCat.add("Lotto");
        incomeCat.add("Gift");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, incomeCat);
        Category.setAdapter(adapter);

        incomeSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                String iAM = incomeAmount.getText().toString();
                int iAmount = Integer.parseInt(iAM);
                String iDate = incomeDate.getText().toString();
                String iMethod = Category.getSelectedItem().toString();

                if (incomeAmount.getText().toString().trim().length() > 0 && incomeDate.getText().toString().trim().length() > 0 && Category.getSelectedItem().toString().trim().length() > 0 ) {
                    Intent homeScreen = new Intent(incomePlus.this, MainActivity.class);
                    startActivity(homeScreen);

                    addData(iAmount, iMethod, iDate);

                }
                else {
                    Toast.makeText(incomePlus.this, "Please Fill In All The Fields", Toast.LENGTH_LONG).show();
                }

            }
        });



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_income, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.menuMain : startActivity (new Intent(this, MainActivity.class));
                break;
        }
        switch (item.getItemId())
        {
            case R.id.menuExpense : startActivity (new Intent(this, ExpenseIncome.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void addData(int iAmount, String iMethod, String iDate){
        boolean insertData = db.insertIncome(iAmount, iMethod, iDate);

        if(insertData==true){
            Toast.makeText(incomePlus.this, "Data Entered", Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(incomePlus.this, "Failure", Toast.LENGTH_LONG).show();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您在Create table query的末尾缺少comma

使用:

private String CREATE_INCOME_TABLE = "CREATE TABLE " + TABLE_INCOME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
        " FIRSTNAME TEXT, LASTNAME TEXT, FAVFOOD TEXT);";

在结尾的双引号内添加逗号 - 对于所有创建表查询。