2017-04-23 15 views
0

Iveはログを記録して登録するデータベースを作成しましたが、うまくいきましたが、データベースを作成して収益を追加してもエラーは発生しませんでした。トーストメッセージが失敗したと言います。正しい変数は以下のコードを添付します。SQLite Insert Data Failure

DatabseHelperコードinsertIncomeはここに挿入する一つのimtryingのクラスのコードである:

パブリッククラス利益{

:パブリッククラスDatabaseHelperは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; 
    } 






} 

データを挿入しようとしているクラス:

​​

答えて

0

Createテーブルクエリの最後にcommaがありません。

使用:

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

終わりに二重引用符内のカンマを追加 - すべてのテーブルクエリを作成します。

+0

ありがとうございました! :) – chris123

+0

あなたは歓迎されています...答えが答えの隣のティックサインをクリックすることで正しいとマークするのを助けたら... thankx – rafsanahmad007

+0

ごめんなさいありがとうございます:) – chris123

関連する問題