質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Q&A

解決済

2回答

1627閲覧

Android SQL

uikura

総合スコア37

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

0グッド

0クリップ

投稿2015/06/30 08:30

Android初心者のものです。皆様の知恵をお貸しください。
Androidでデータを取り出して表示させたいと考えています。「wdate」,「memo」,「product」 の3つのカラムが現在あり「wdate」「memo」の二つのデータしか検索して表示させることができません。検索条件文がおかしいと思うのですが書き方がいまいちわかりません。どうすればよいでしょうか?回答の方よろしくお願いします

が検索条件となっています(ソースから抜粋)

// 日報のデータを検索、表示
public void SerchDailyReport() {
int cnt = 0;

//検索条件 String tablename = "report"; String[] cols = { "wdate", "memo","product" }; String selection = "wdate = ?"; String[] selectionArgs = {txtDate.getText().toString()};; String groupBy = null; String having = null; String orderBy = "wdate"; txtPro.setText(""); try { SQLiteDatabase db = SSQLhelper.getReadableDatabase(); //クエリーの実行 Cursor cur = db.query(tablename, cols, selection, selectionArgs, groupBy, having, orderBy); //データがあるまでデータを取得 while (cur.moveToNext()) { txtDate.setText(cur.getString(0)); txtMemo.setText(cur.getString(1)); Log.d("data", cnt + " " + cur.getString(0) + ">" + cur.getString(1)); cnt++;//件数をチェック } //終了処理 cur.close(); db.close(); //結果表示 if (0 < cnt) { txtMess.setText("検索:OK " + cnt + "件"); } else { txtMess.setText("検索:データなし"); } } catch (Exception ex) { txtMess.setText("検索:NG " + ex.toString()); } }

/**

↓ここから本文となっています

public class SampleSQLiteHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "report.db";

public SampleSQLiteHelper(Context context) { // コンストラクタで作成するデータベースの設定 super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // テーブル作成、なければ新規作成 String sql = "CREATE TABLE IF NOT EXISTS report " + "(wdate TEXT PRIMARY KEY, memo TEXT,product TEXT);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // データベースのVersionアップ時の記載 }

}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

public class SampleSQLiteActivity extends Activity implements OnClickListener {

private SampleSQLiteHelper SSQLhelper = null; private TextView txtMess; private EditText txtDate; private EditText txtMemo; private EditText txtPro; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sample_sqlite); Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(this); Button btn2 = (Button) findViewById(R.id.button2); btn2.setOnClickListener(this); Button btn3 = (Button) findViewById(R.id.button3); btn3.setOnClickListener(this); Button btn4 = (Button) findViewById(R.id.button4); btn4.setOnClickListener(this); Button btn5 = (Button) findViewById(R.id.button5); btn5.setOnClickListener(this); Button btn6 = (Button) findViewById(R.id.button6); btn6.setOnClickListener(this); txtMess = (TextView)findViewById(R.id.tvMess); txtDate = (EditText) findViewById(R.id.editText1); txtMemo = (EditText) findViewById(R.id.editText2); txtPro = (EditText) findViewById(R.id.editText3); SSQLhelper = new SampleSQLiteHelper(SampleSQLiteActivity.this); }

// ボタンクリック
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:// 日付
showDatePickerDlg();
break;

case R.id.button2:// 登録 AddDailyReport(); break; case R.id.button3:// 更新 UpDateDailyReport(); break; case R.id.button4:// 削除 DeleteDailyReport(); break; case R.id.button5:// 表示 SerchDailyReport(); break; case R.id.button6:// 全削除 DelteAllDailyReport(); break; } } // 日付のダイアログ public void showDatePickerDlg() { Calendar cal = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog( SampleSQLiteActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker picker, int year,int month, int day) { DecimalFormat df = new DecimalFormat("00"); String ddate = df.format(year) + "/" + df.format(month + 1) + "/" + df.format(day); txtDate.setText(ddate); // データ検索 SerchDailyReport(); } }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); dialog.show(); } // 日報のデータを検索、表示 public void SerchDailyReport() { int cnt = 0; //検索条件 String tablename = "report"; String[] cols = { "wdate", "memo","product" }; String selection = "wdate = ?"; String[] selectionArgs = {txtDate.getText().toString()};; String groupBy = null; String having = null; String orderBy = "wdate"; txtPro.setText(""); try { SQLiteDatabase db = SSQLhelper.getReadableDatabase(); //クエリーの実行 Cursor cur = db.query(tablename, cols, selection, selectionArgs, groupBy, having, orderBy); //データがあるまでデータを取得 while (cur.moveToNext()) { txtDate.setText(cur.getString(0)); txtMemo.setText(cur.getString(1)); Log.d("data", cnt + " " + cur.getString(0) + ">" + cur.getString(1)); cnt++;//件数をチェック } //終了処理 cur.close(); db.close(); //結果表示 if (0 < cnt) { txtMess.setText("検索:OK " + cnt + "件"); } else { txtMess.setText("検索:データなし"); } } catch (Exception ex) { txtMess.setText("検索:NG " + ex.toString()); } }

/**

// 追加 public void AddDailyReport() { if( !chkData("追加")){ return; } ContentValues dat = new ContentValues(); try { SQLiteDatabase db = SSQLhelper.getWritableDatabase(); dat.put("wdate", txtDate.getText().toString()); dat.put("memo", txtMemo.getText().toString()); dat.put("product",txtPro.getText().toString()); long rt = db.insert("report", null, dat); db.close(); if (0 < rt) { txtMess.setText("追加できました=" + rt); } else { txtMess.setText("追加できません=" + rt); } } catch (Exception ex) { txtMess.setText("追加できません" + ex.toString()); } } // 更新 public void UpDateDailyReport() { if( !chkData("更新")){ return; } ContentValues dat = new ContentValues(); String condition = "wdate = '" + txtDate.getText().toString() + "'"; try { SQLiteDatabase db = SSQLhelper.getWritableDatabase(); dat.put("memo", txtMemo.getText().toString()); dat.put("product", txtPro.getText().toString()); long rt = db.update("report", dat, condition, null); db.close(); if (0 < rt) { txtMess.setText("更新できました "+ rt); } else { txtMess.setText("更新できません "+ rt); } } catch (Exception ex) { txtMess.setText("更新できません" + ex.toString()); } } // データ削除 public void DeleteDailyReport() { if( !chkData("削除")){ return; } String condition = "wdate = '" + txtDate.getText().toString() + "'"; try { SQLiteDatabase db = SSQLhelper.getWritableDatabase(); long rt = db.delete("report", condition, null); db.close(); txtDate.setText(""); txtMemo.setText(""); txtPro .setText(""); if (0 < rt) { txtMess.setText("削除できました "+ rt); } else { txtMess.setText("削除できません "+ rt); } //txtMess.setText("削除できました"); } catch (Exception ex) { txtMess.setText("削除できません" + ex.toString()); } } // 全データ削除 public void DelteAllDailyReport() { try { SQLiteDatabase db = SSQLhelper.getWritableDatabase(); db.execSQL("DELETE FROM report;"); db.close(); txtDate.setText(""); txtMemo.setText(""); txtPro. setText(""); txtMess.setText("全削除できました"); } catch (Exception ex) { txtMess.setText("全削除できません" + ex.toString()); } } //文字数チェック private boolean chkData(String mess){ if (10 != txtDate.getText().length()) { txtMess.setText(mess + "できません(データ長さ)"); return false; } else { return true; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_sample_sqlite, menu); return true; }

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

表示させるにあたってwdateとmemoしか出力していないから出力しないのではないでしょうか?

txtDate.setText(cur.getString(0));
txtMemo.setText(cur.getString(1));

出力する領域をtxtProductと仮定すると

txtDate.setText(cur.getString(0));
txtMemo.setText(cur.getString(1));
txtProduct.setText(cur.getString(2));

このようにしたら3つとも表示できると思います。

投稿2015/06/30 08:52

kutsulog

総合スコア985

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

uikura

2015/07/01 08:24

ありがとうございます!解決しました
guest

0

String[] cols = { "wdate", "memo","product" };
//カラムを3つもっている
while (cur.moveToNext()) {
txtDate.setText(cur.getString(0));
txtMemo.setText(cur.getString(1));
//3つ持っているにも関わらず二つしか取り出していなかった
txtPro.setText(cur.getString(2));
//これを追加すれば3つ取り出したことになる

投稿2015/07/01 08:28

uikura

総合スコア37

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問