2017-06-12 16 views
-2

AndroidデータをSQLiteデータベースに保存する方法についてお聞きしたいと思います(SQLiteデータベースを初めて使用しています)。私はSQLiteデータベースにデータを格納しようとしていましたが、SQLiteStudioでデータベースを見たときにデータが空でした。 これは私のコードです:AndroidデータがSQLiteデータベースに保存されていません

DatabaseHelper.java

package com.example.toolsmanager; 

    import android.content.Context; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 

    public class DatabaseHelper extends SQLiteOpenHelper { 

    private static final String DATABASE_NAME = "btm.db"; 
    private static final int DATABASE_VERSION = 1; 

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

    public static final String TABLE_1 = "oneroundsingle"; 

    public static final String TABLE1_COLUMN1 = "_ID"; 
    public static final String TABLE1_COLUMN2 = "GAMETYPE"; 
    public static final String TABLE1_COLUMN3 = "PLAYER1"; 
    public static final String TABLE1_COLUMN4 = "PLAYER2"; 
    public static final String TABLE1_COLUMN5 = "SCORE"; 
    public static final String TABLE1_COLUMN6 = "WINNER"; 


    public static final String CREATE_TABLE_ORS = "create table" + TABLE_1 + " (" + 
      TABLE1_COLUMN1 + " integer primary key autoincrement, " + 
      TABLE1_COLUMN2 + " text, " + 
      TABLE1_COLUMN3 + " text, " + 
      TABLE1_COLUMN4 + " text, " + 
      TABLE1_COLUMN5 + " text, " + 
      TABLE1_COLUMN6 + " text " + ");"; 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_TABLE_ORS); 
     db.execSQL(CREATE_TABLE_ORD); 
     db.execSQL(CREATE_TABLE_RS); 
     db.execSQL(CREATE_TABLE_RD); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("drop table if exists" + TABLE_1); 
     db.execSQL("drop table if exists" + TABLE_2); 
     db.execSQL("drop table if exists" + TABLE_3); 
     db.execSQL("drop table if exists" + TABLE_4); 
     onCreate(db); 
    } 
    } 

OR_SingleCount.java

私のコードで間違って何
package com.example.toolsmanager; 

    import android.content.ContentValues; 
    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.support.v7.app.AlertDialog; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class OR_SingleCount extends AppCompatActivity { 

    int player1_score, player2_score, final_score; 
    TextView text_player1_name, text_player2_name; 
    String score, winner; 
    DatabaseHelper databaseHelper; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_or_single_count); 

     text_player1_name = (TextView)findViewById(R.id.player1_name); 
     text_player1_name.setText(getIntent().getExtras().getString("player1")); 

     text_player2_name = (TextView)findViewById(R.id.player2_name); 
     text_player2_name.setText(getIntent().getExtras().getString("player2")); 

    } 

    public void singleCount(View v) { 
     int id = v.getId(); 
     if (id == R.id.player1_plus) { 
      player1_score += 1; 
      TextView text_player1_score = (TextView) findViewById(R.id.player1_score); 
      text_player1_score.setText(String.valueOf(player1_score)); 
     } else if (id == R.id.player1_minus && player1_score != 0) { 
      player1_score -= 1; 
      TextView text_player1_score = (TextView) findViewById(R.id.player1_score); 
      text_player1_score.setText(String.valueOf(player1_score)); 
     } else if (id == R.id.player2_plus) { 
      player2_score += 1; 
      TextView text_player2_score = (TextView) findViewById(R.id.player2_score); 
      text_player2_score.setText(String.valueOf(player2_score)); 
     } else if (id == R.id.player2_minus && player2_score != 0) { 
      player2_score -= 1; 
      TextView text_player2_score = (TextView) findViewById(R.id.player2_score); 
      text_player2_score.setText(String.valueOf(player2_score)); 
     } 
     finalScore(); 
    } 

    public void finalScore(){ 
     if ((player1_score == 21 && player2_score < 20) || (player2_score == 21 && player1_score < 20)) { 
      final_score = 21; 
      getWinner(); 
     } else if (player1_score == 30 || player2_score == 30) { 
      final_score = 30; 
      getWinner(); 
     } else if (player1_score >= 20 && player2_score >= 20) { 
      if (player1_score == player2_score + 2) { 
       final_score = player1_score; 
       getWinner(); 
      } else if (player2_score == player1_score + 2) { 
       final_score = player2_score; 
       getWinner(); 
      } 
     } 
    } 

    public void getWinner() { 
     if (player1_score == final_score){ 
      winner = text_player1_name.getText().toString(); 
      String print_winner = winner + " is the winner"; 
      Toast.makeText(getApplicationContext(),print_winner, Toast.LENGTH_LONG).show(); 
     } else if(player2_score == final_score) { 
      winner = text_player2_name.getText().toString(); 
      String print_winner = winner + " is the winner"; 
      Toast.makeText(getApplicationContext(), print_winner, Toast.LENGTH_LONG).show(); 
     } 

     score = player1_score + " - " + player2_score; 
     String gametype = "Single"; 
     addORSingleData(gametype, 
       text_player1_name.getText().toString(), 
       text_player2_name.getText().toString(), 
       score, 
       winner); 

     AlertDialog repeatdialog= new AlertDialog.Builder(this) 
       .setTitle("Game Finish") 
       .setMessage("Do you want to repeat the game?") 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         player1_score = 0; 
         player2_score = 0; 
         recreate(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Intent i = new Intent(OR_SingleCount.this, OneRoundMatch.class); 
         i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(i); 
         finish(); 
        } 
       }).create(); 
     repeatdialog.show(); 
    } 

    public void addORSingleData(String gametype, String player1, String player2, String score, String winner){ 
     databaseHelper = new DatabaseHelper(this); 
     SQLiteDatabase db = databaseHelper.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(DatabaseHelper.TABLE1_COLUMN2, gametype); 
     contentValues.put(DatabaseHelper.TABLE1_COLUMN3, player1); 
     contentValues.put(DatabaseHelper.TABLE1_COLUMN4, player2); 
     contentValues.put(DatabaseHelper.TABLE1_COLUMN5, score); 
     contentValues.put(DatabaseHelper.TABLE1_COLUMN6, winner); 
     db.insert(DatabaseHelper.TABLE_1, null, contentValues); 
    } 
    } 

+0

アプリが正しく動作していますか? –

+0

はい、何もエラーはありません。何が起こる? – Archenians

答えて

-1

私は私がしたい場合ので、私はその問題を撃つことができ、トラブルはないSQLiteのメーカーを使用したことがありませんが、あなたは、カーソルに設定できます。その後、

Toast.makeText(this, "" + cursor.getCount(), Toast.LENGTH_SHORT).show(); 

を見るために呼び出す

Cursor cursor = db.query(TABLE_NAME,null,null,null,null,null,null); 

を任意のデータを挿入している場合。また、onCreateメソッドの余分なテーブルは何ですか?

+0

ここで私はカーソルの構文を与える必要がありますか?余分なテーブルは、次の機能用です。私はそれを削除する必要がありますか? – Archenians

+0

あなたはあなたの挿入メソッドの最後にそれを置くことができます。そのちょうど自己チェック、またはあなたはそれにログメッセージを入れます。 – Sav

関連する問題