下記のようにデータベースを使おうと思っているのですが
getMoney()の cursor = database.query(の行でno such table; moneyとエラーが出てしまいます。
どこがおかしいでしょうか?
よろしくお願いします。
import android.provider.BaseColumns; public class SQLContract { static abstract class Item implements BaseColumns { static final String TABLE_NAME = "item"; static final String _ID = "_id"; static final String ITEM = "item"; static final String GENRE = "genre"; static final String NUMBER = "number"; } static abstract class Money implements BaseColumns { static final String TABLE_NAME = "money"; static final String _ID = "_id"; static final String MONEY = "money"; static final String YEAR = "year"; static final String MONTH = "month"; static final String DATE = "date"; static final String ITEM = "item"; } } コード
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SQLMoneyOpenHelper extends SQLiteOpenHelper { SQLMoneyOpenHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } public static final String DB_NAME = "Money.db"; public static final int DB_VERSION = 1; public static final String CREATE_TABLE = "create table " + SQLContract.Money.TABLE_NAME + "(" + SQLContract.Money._ID + " integer primary key autoincrement," + SQLContract.Money.ITEM + " text," + SQLContract.Money.MONEY + " integer,"+ SQLContract.Money.YEAR + " integer,"+ SQLContract.Money.MONTH + " integer,"+ SQLContract.Money.DATE + " integer)"; public static final String INIT_TABLE = "insert into " + SQLContract.Item.TABLE_NAME + "(" + SQLContract.Money.ITEM + "," + SQLContract.Money.MONEY + ","+ SQLContract.Money.YEAR + ","+ SQLContract.Money.MONTH + ","+ SQLContract.Money.DATE + ") values " + "('item1',10,2018,2,1)"; public static final String DROP_TABLE = "drop table if exists " + SQLContract.Money.TABLE_NAME; @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE); db.execSQL(INIT_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(DROP_TABLE); onCreate(db); } } コード
import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class SQLMoney { Context context; public SQLMoney(Context context) { this.context = context; } //指定項目の金額を取得 public int getMoney(int year, int month, int date, int number) { SQLMoneyOpenHelper openHelper = new SQLMoneyOpenHelper(context); SQLiteDatabase database = openHelper.getWritableDatabase(); String item = searchItem(number); Cursor cursor; cursor = database.query( SQLContract.Money.TABLE_NAME, null, SQLContract.Money.ITEM + " = ? and " + SQLContract.Money.YEAR + " = ? and " + SQLContract.Money.MONTH + " = ? and " + SQLContract.Money.DATE + " = ?", new String[]{item, String.valueOf(year), String.valueOf(month), String.valueOf(date)}, null, null, null, null ); if (cursor.getCount() == 0) { return 0; } cursor.moveToFirst(); int money = cursor.getInt(cursor.getColumnIndex(SQLContract.Item.ITEM)); database.close(); cursor.close(); return money; } //指定項目の金額を上書き public void saveMoney(int year, int month, int date, int number, int money) { SQLMoneyOpenHelper openHelper = new SQLMoneyOpenHelper(context); SQLiteDatabase database = openHelper.getWritableDatabase(); String item = searchItem(number); ContentValues newMoney = new ContentValues(); newMoney.put(SQLContract.Money.MONEY, money); long newId = database.update( SQLContract.Money.TABLE_NAME, newMoney, SQLContract.Money.ITEM + " = ? and " + SQLContract.Money.YEAR + " = ? and " + SQLContract.Money.MONTH + " = ? and " + SQLContract.Money.DATE + " = ?", new String[]{item, String.valueOf(year), String.valueOf(month), String.valueOf(date)} ); database.close(); } public String searchItem(int number) { SQLItemOpenHelper itemOpenHelper = new SQLItemOpenHelper(context); SQLiteDatabase itemDatabase = itemOpenHelper.getReadableDatabase(); Cursor cursor; cursor = itemDatabase.query( SQLContract.Item.TABLE_NAME, null, SQLContract.Item.NUMBER + " = ?", new String[]{String.valueOf(number)}, null, null, null, null ); cursor.moveToFirst(); String item = cursor.getString(cursor.getColumnIndex(SQLContract.Item.ITEM)); itemDatabase.close(); cursor.close(); return item; } } コード
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/06 07:28
2018/02/06 08:03
2018/02/06 09:16
2018/02/06 12:14