いろいろなサイトから、接続のコードをコピペしたり、参考書や、自分で考えるなどして、このようなコードになったのですが、エラーが多く動きそうにもありません。
やりたいこととしては、Todoリストアプリで、データベースにtitle(題名)、content(内容)、data(期限)を入力して、それを、data(期限)の新しい順にlistで表示するというものを作りたいです。
参考書や、ネットを見てもやることが多すぎて、どうすればいいのかがよく分からない状況です。このようなアプリをH2Databaseで作ったときは、こんなに手こずらなかったのですが、どなたか助けていただけないでしょうか?
どうぞよろしくお願いします。
java
1package com.example.android.sample.memo; 2 3import android.content.ContentValues; 4import android.content.Context; 5import android.database.Cursor; 6import android.database.sqlite.SQLiteDatabase; 7import android.database.sqlite.SQLiteOpenHelper; 8 9/** 10 * Created by endoutaichi on 2017/04/02. 11 */ 12 13public class MemoDBHelper extends SQLiteOpenHelper { 14 15 16 //データベース名 17 private static final String DB_NAME = "memo.db"; 18 19 //データベースバージョン 20 private static final int DB_VERSION = 1; 21 //テーブル名 22 private static final String TABLE_NAME = "memo"; 23 //IDカラム 24 public static final String _ID = "_id"; 25 //ファイル名カラム 26 public static final String TITLE = "title"; 27 //内容 28 public static final String CONTENT = "content"; 29 //作成日時 30 public static final String DATA = "date"; 31 32 private SQLiteDatabase db = null; 33 private MemoDBHelper dbHelper = null; // DBHelper 34 protected Context context; // Context 35 36 37 public MemoDBHelper(Context context) { 38 super(context, DB_NAME, null, DB_VERSION); 39 } 40 41 42 public MemoDBHelper openDB() { 43 db = dbHelper.getWritableDatabase(); // DBの読み書き 44 return this; 45 } 46 47 /** 48 * DBを閉じる 49 * closeDB() 50 */ 51 public void closeDB() { 52 db.close(); // DBを閉じる 53 db = null; 54 } 55 56 57 @Override 58 public void onCreate(SQLiteDatabase db) { 59 String createTable = "CREATE TABLE" + TABLE_NAME + "(" + 60 _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 61 TITLE + "TEXT," + 62 CONTENT + "TEXT," + 63 DATA + "INTEGER NOT NULL," + 64 ")"; 65 66 db.execSQL(createTable); 67 68 } 69 70 //インスタンスを作る 71 MemoDBHelper mDbHelper = new MemoDBHelper(getContext()); 72 73 /** 74 * DBのレコードへ登録 75 * saveDB() 76 * 77 * @param title 題名 78 * @param content 本文 79 * @param data 締め切り 80 * @param id id 81 */ 82 83 public void saveDB(String id, String title, int content, int data) { 84 85 //ここには、メソッドを呼び出すときに、情報をtextから持ってきて引数で渡す。 86 db.beginTransaction(); // トランザクション開始 87 88 try { 89 ContentValues values = new ContentValues(); // ContentValuesでデータを設定していく 90 values.put(_ID, id); 91 values.put(TITLE, title); 92 values.put(CONTENT, content); 93 values.put(DATA, data); 94 95 96 SQLiteDatabase sdb = mDbHelper.getReadableDatabase(); 97 98 // DBから取得部分 99 Cursor db = sdb.query("TABLE_NAME", 100 new String[]{"title","content","data"}, 101 null, 102 null, 103 null, 104 null, 105 "values"); 106 107 108 db.insert(TABLE_NAME, null, values); // レコードへ登録 109 110 db.setTransactionSuccessful(); // トランザクションへコミット 111 } catch (Exception e) { 112 e.printStackTrace(); 113 } finally { 114 db.endTransaction(); // トランザクションの終了 115 } 116 } 117 118 119 /** 120 * DBのデータを取得 121 * getDB() 122 * 123 * @param columns String[] 取得するカラム名 nullの場合は全カラムを取得 124 * @return DBのデータ 125 */ 126 public Cursor cursor(String[] columns) { 127 128 // queryメソッド DBのデータを取得 129 // 第1引数:DBのテーブル名 130 // 第2引数:取得するカラム名 131 // 第3引数:選択条件(WHERE句) 132 // 第4引数:第3引数のWHERE句において?を使用した場合に使用 133 // 第5引数:集計条件(GROUP BY句) 134 // 第6引数:選択条件(HAVING句) 135 // 第7引数:ソート条件(ODERBY句) 136 137 while (cursor.moveToNext()) { 138 //対応する方のgetメソッドで取得する 139 //INTEGER型のカラム 140 long id = cursor.getLong(cursor.getColumnIndex(_ID)); 141 //TEXT型のカラム 142 String content = cursor.getString(cursor.getColumnIndex(CONTENT)); 143 String title = cursor.getString(cursor.getColumnIndex(TITLE)); 144 String data = cursor.getString(cursor.getColumnIndex(DATA)); 145 146 } 147 148 //必ずとじる 149 cursor.close(); 150 151 152 return db.query(TABLE_NAME, columns, null, null, null, null, null); 153 } 154 155 156 @Override 157 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 158 159 //バージョン管理をここで行う 160 } 161 162 163 public Context getContext() { 164 return context; 165 } 166 167} 168 169 SQLiteDatabase db = mDbHelper.getReadableDatabase(); 170 171 // Define a projection that specifies which columns from the database 172 // you will actually use after this query. 173 String[] projection = { 174 TABLE_NAME._ID, 175 TABLE_NAME.TITLE, 176 TABLE_NAME.CONTENT, 177 TABLE_NAME.DATA 178 }; 179 180 // Filter results WHERE "title" = 'My Title' 181 String selection = TABLE_NAME._ID + " = ?"; 182 String[] selectionArgs = {"My Title"}; 183 184 // How you want the results sorted in the resulting Cursor 185 String sortOrder = 186 TABLE_NAME.DATA + " DESC"; 187 188 189 Cursor c = db.query( 190 TABLE_NAME, // The table to query// The columns to return 191 title, // The columns for the WHERE clause 192 content, 193 data,// The values for the WHERE clause 194 null, // don't group the rows 195 null, // don't filter by row groups 196 sortOrder // The sort order 197 ); 198
回答1件
あなたの回答
tips
プレビュー