2017-06-12 138 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的工作室,所以我不能麻煩拍攝這個問題,如果我想,但你可以建立一個光標:

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

然後調用

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

看如果你插入任何數據。另外,你的onCreate方法中額外的表是什麼?

+0

我應該給光標語法?額外的表格用於下一個功能。我應該刪除它嗎? – Archenians

+0

你可以把它放在你的插入方法的末尾。它只是一個自我檢查,或者你把它放在一個日誌消息; – Sav